简体   繁体   English

C#AES-256加密

[英]C# AES-256 Encryption

I am using RijndaelManaged to make a simple encryption/decryption utility. 我正在使用RijndaelManaged制作一个简单的加密/解密实用程序。 This is working fine, but I am trying to get it integrated with another program which is created in Unix (Oracle). 这工作正常,但是我试图将其与在Unix(Oracle)中创建的另一个程序集成在一起。 My problem is, for all smaller input string, i am getting the exact same encrypted hex as the Unix code is generation, but for longer strings, half of my encrypted hex is same, but the other half is different: 我的问题是,对于所有较小的输入字符串,我将获得与Unix代码生成时完全相同的加密十六进制,但是对于较长的字符串,我的加密十六进制中的一半是相同的,但另一半是不同的:

Unix Output: Unix输出:

012345678901234 - 00984BBED076541E051A239C02D97117 
0123456789012345678 - A0ACE158AD8CF70CEAE8F76AA27F62A30EA409ECE2F7FF84F1A9AF50817FC0C4

Windows Output (my code): Windows输出(我的代码):

012345678901234 - 00984BBED076541E051A239C02D97117 (same as above)
0123456789012345678 - A0ACE158AD8CF70CEAE8F76AA27F62A3D9A1B396A614DA2C1281AA1F48BC3EBB (half exactly same as above)

My Windows code is: 我的Windows代码是:

public string Encrypt(byte[] PlainTextBytes, byte[] KeyBytes, string InitialVector)
        {
            byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
            RijndaelManaged SymmetricKey = new RijndaelManaged();
            SymmetricKey.Mode = CipherMode.ECB;
            SymmetricKey.Padding = PaddingMode.PKCS7;
            ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes);
            MemoryStream MemStream = new MemoryStream();
            CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write);
            CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
            CryptoStream.FlushFinalBlock();
            byte[] CipherTextBytes = MemStream.ToArray();
            MemStream.Close();
            CryptoStream.Close();
            return ByteToHexConversion(CipherTextBytes);
        }

Unix (PL/SQL) code: Unix(PL / SQL)代码:

FUNCTION Encrypt_Card (plain_card_id  VARCHAR2)
    RETURN RAW AS
        num_key_bytes      NUMBER := 256/8;        -- key length 256 bits (32 bytes)
        encrypted_raw      RAW (2000);             -- stores encrypted binary text
        encryption_type    PLS_INTEGER :=          -- total encryption type
                        DBMS_CRYPTO.ENCRYPT_AES256
                      + DBMS_CRYPTO.CHAIN_CBC
                      + DBMS_CRYPTO.PAD_PKCS5;

        key_bytes_raw  RAW(64) :=my_hex_key;
    BEGIN



     encrypted_raw := DBMS_CRYPTO.ENCRYPT
           (
              src => UTL_I18N.STRING_TO_RAW (plain_card_id, 'AL32UTF8'),
              typ => encryption_type,
              key => key_bytes_raw
           );


      RETURN encrypted_raw;
    EXCEPTION
    WHEN OTHERS THEN
    dbms_output.put_line (plain_card_id || ' - ' || SUBSTR(SQLERRM,1,100) );
    RETURN HEXTORAW ('EEEEEE');

The only difference i see is use of PKCS5 and PCKS7. 我看到的唯一区别是使用PKCS5和PCKS7。 But, .NET doesn't have PCKS5. 但是,.NET没有PCKS5。

What abc said and also you don't seem to have any IV (Initialization Vector) in you PL/SQL code at all. abc所说的话,您的PL / SQL代码中似乎根本没有IV(初始化向量)。

The fact that the first part are the same has to do with the different modes (ECB and CBC). 第一部分相同的事实与不同的模式(ECB和CBC)有关。 ECB encrypts each block separately while CBC uses the previous block when encrypting the next one. ECB分别加密每个块,而CBC在加密下一个块时使用前一个块。

What happens here is that since you use CBC and do not set an IV the IV is all zeroes. 这里发生的是,由于您使用CBC且未设置IV,所以IV全部为零。
That means that the first block of ECB encryption and CBC encryption will be the same. 这意味着ECB加密和CBC加密的第一块将是相同的。
(Since A XOR 0 = A). (因为A XOR 0 = A)。

You need to make sure you use the same encryption mode in both systems and if you decide on CBC make sure you use the same IV. 您需要确保在两个系统中使用相同的加密模式,并且如果决定使用CBC,请确保使用相同的IV。

在一种情况下使用ECB,在另一种情况下使用CBC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM