开发者

Rijndael encryption/decryption

I have the following code for encryption and decryption. The problem is that at decryption besides the decrypted text i have some "aaaaa" after the text. why? need some help. THX!

public static byte[] Encrypt(byte[] PlainTextBytes, string key , string InitialVector)
        {
            try
            {
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
               Byte[] KeyBytes = encoding.GetBytes(key);
                byte[] InitialVectorBytes = encoding.GetBytes(InitialVector); 
                RijndaelManaged SymmetricKey = new RijndaelManaged();
                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();

               return CipherTextBytes;

//decrytion

public static string Decrypt(byte[] PlainTextBytes1, string key, string InitialVector)
    {
        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        Byte[] KeyBytes = encoding.GetBytes(key);
        RijndaelManaged SymmetricKey = new RijndaelManaged();
        byte[] InitialVectorBytes = Encoding.UTF8.GetBytes(InitialVector);

        ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes);
        MemoryStream MemStre开发者_如何学Pythonam1 = new MemoryStream(PlainTextBytes1);
        CryptoStream CryptoStream = new CryptoStream(MemStream1, Decryptor, CryptoStreamMode.Read);
        Byte[] pltxt = new byte[PlainTextBytes1.Length];
        CryptoStream.Read(pltxt, 0, pltxt.Length);

         ASCIIEncoding textConverter = new ASCIIEncoding();

         round = textConverter.GetString(pltxt);
          return round;

    }

where am i wrong?


In your decrypt function you have:

Byte[] pltxt = new byte[PlainTextBytes1.Length];

This is wrong because the cypher text is longer than the plain text, because it's padded to get a multiple of the block size.

 CryptoStream.Read(pltxt, 0, pltxt.Length);

Read returns how many bytes were actually returned. It doesn't guarantee that it will return as many bytes as you requested.


And then there are multiple other defects/bad style things:

  1. The parameter passed into Decrypt is called PlainTextBytes1 it should be called cyphertext.
  2. The way you create the key/initvec bytes from the string. ASCII encoding is a bad choice here. ASCII can't represent any byte string. Perhaps you want to hex en/decode a string of twice the size of the key instead?
  3. ASCII en/decoding the plaintext will only work for ASCII characters and silently corrupt all other characters. Why don't you use UTF-8 instead?
  4. .net naming conventions say you should use lowercase names for parameters
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜