2011-04-22 7 views
3

'해독 할 데이터의 길이가 유효하지 않습니다.'라는 메시지가 나타납니다. 문자열의 암호를 해독하려고하면 오류가 발생합니다. 이 사이트에서이 오류에 대한 여러 가지 다른 참조를 살펴본 후 제안 된 여러 가지 방법을 시도했지만 지금까지 아무 것도 작동하지 않았습니다.다른 '해독 할 데이터의 길이가 유효하지 않습니다.' 오류

나는 근본적인 것이 빠져 있다고 확신하지만, 그것이 무엇인지는 알 수 없습니다.

암호화 및 암호 해독시 동일한 키와 IV를 사용하고 있습니다. 암호화 및 암호 해독시 FlushFinalBlock() 호출을 추가했습니다. 심지어 encryptStream.Position = 0을 설정하려고 시도했지만 그 값은 ObjectDisposedException입니다.

문제를 설명하는 콘솔 앱을 만들었습니다. 이 코드의 전체 내용은 다음과 같습니다.

using System; 
using System.IO; 
using System.Security.Cryptography; 
using System.Text; 

namespace Crypto 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      _CryptoString = AESStringEncryption(CRYPTO_STRING); 
      _CryptoString = AESStringDecryption(_CryptoString); 
     } 

     private const string CRYPTO_STRING = "The quick brown fox jumped over the lazy dog."; 

     private static byte[] _KY = { 47, 53, 94, 65, 243, 197, 42, 80, 125, 255, 144, 41, 130, 76, 2, 142, 43, 1, 120, 124, 255, 248, 232, 139, 170, 42, 243, 52, 4, 17, 60, 174 }; 
     private static byte[] _VI = { 68, 42, 157, 47, 99, 8, 174, 169, 119, 255, 144, 218, 8, 30, 60, 42 }; 

     private static string _CryptoString; 

     /// <summary> 
     /// Applies .NET Framework AES (Advanced Encryption Standard) Level encryption to the provided string parameter. 
     /// Utilizies the UTF8 encoding stardard for the conversion from string to byte[]. 
     /// </summary> 
     public static string AESStringEncryption(string unencryptedString) 
     { 
      byte[] unencryptedBytes = Encoding.UTF8.GetBytes(unencryptedString); 
      byte[] encryptedBytes = AESByteArrayEncryption(unencryptedBytes); 
      string encryptedString = Encoding.UTF8.GetString(encryptedBytes); 

      return encryptedString; 
     } 

     /// <summary> 
     /// Applies .NET Framework AES (Advanced Encryption Standard) Level decryption to the provided string parameter. 
     /// Utilizies the UTF8 encoding stardard for the conversion from byte[] to string. 
     /// </summary> 
     public static string AESStringDecryption(string encryptedString) 
     { 
      byte[] encryptedBytes = Encoding.UTF8.GetBytes(encryptedString); 
      byte[] decryptedBytes = AESByteArrayDecryption(encryptedBytes); 
      string decryptedString = Encoding.UTF8.GetString(decryptedBytes); 

      return decryptedString; 
     } 

     /// <summary> 
     /// Applies .NET Framework AES (Advanced Encryption Standard) Level encryption to the provided byte array parameter. 
     /// </summary> 
     public static byte[] AESByteArrayEncryption(byte[] unencryptedBytes) 
     { 
      using (var rm = new RijndaelManaged()) 
      { 
       var encryptor = rm.CreateEncryptor(_KY, _VI); 
       using (var encryptStream = new MemoryStream()) 
       { 
        using (var cryptoStream = new CryptoStream(encryptStream, encryptor, CryptoStreamMode.Write)) 
        { 
         cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length); 
         cryptoStream.FlushFinalBlock(); 
        } 

        //encryptStream.Position = 0; 

        byte[] encryptedBytes = encryptStream.ToArray(); 

        return encryptedBytes; 
       } 
      } 
     } 

     /// <summary> 
     /// Applies .NET Framework AES (Advanced Encryption Standard) Level decryption to the provided byte array parameter. 
     /// </summary> 
     public static byte[] AESByteArrayDecryption(byte[] encryptedBytes) 
     { 
      using (var rm = new RijndaelManaged()) 
      { 
       var decryptor = rm.CreateDecryptor(_KY, _VI); 
       using (var decryptStream = new MemoryStream()) 
       { 
        using (var cryptoStream = new CryptoStream(decryptStream, decryptor, CryptoStreamMode.Write)) 
        { 
         cryptoStream.Write(encryptedBytes, 0, encryptedBytes.Length); 
         cryptoStream.FlushFinalBlock(); 
        } 

        //decryptStream.Position = 0; 

        byte[] decryptedBytes = decryptStream.ToArray(); 

        return decryptedBytes; 
       } 
      } 
     } 
    } 
} 

답변

7

이진 배열을 UTF-8로 변환 할 수 없습니다. 동일한 것은 아닙니다. 대신 Base64를 사용하십시오. 암호화 방법 내부

가, 마지막 줄에 두 번째는해야한다 :

string encryptedString = Convert.ToBase64String(encryptedBytes); 

그리고 해독 방법, 첫 번째 줄은 다음과 같습니다

byte[] encryptedBytes = Convert.FromBase64String(encryptedString); 
+0

큰 일했다! 감사. 나는 그것이 단순해야만한다는 것을 알았다. –

+0

고마워요, 매우 간단합니다 –

+0

잘 했어 .. Thanks @ Weltonv3.56 –

관련 문제