2011-10-04 165 views
1

我正在使用AES加密並解密字符串。但我的輸出如下所示:AES字符串加密/解密字符間隔出來

Original text >> HI WORLD 
Decrypted text >> H I W O R L D 

我已經嘗試了很多代碼,但是我沒有找到問題。

問題在哪裏?

class Program 
{ 
    public static void Main(string[] args) 
    { 
     byte[] aesKey = Cryptography.GenerateAes128Key(); 
     Console.WriteLine("AES key >> " + aesKey.Length); 
     string originalText = "HI WORLD"; 
     byte[] myMess = ASCIIEncoding.Unicode.GetBytes(originalText); 
     Console.WriteLine("Original text >> " + ASCIIEncoding.Unicode.GetString(myMess)); 
     byte[] myEcnryptedMess = Cryptography.Encrypt(myMess, aesKey); 
     Console.WriteLine("Encrypted text >> " + ASCIIEncoding.Unicode.GetString(myEcnryptedMess)); 
     Console.WriteLine("Decrypted text >> " + Cryptography.Decrypt(myEcnryptedMess, aesKey)); 
     Console.WriteLine("Press any key to continue . . . "); 
     Console.ReadKey(true); 
    } 

    public static byte[] Encrypt(byte[] plainTextBytes, byte[] Key) 
    { 
     byte[] iv = new byte[Key.Length]; 
     Aes myAes = Aes.Create(); 
     ICryptoTransform encryptor = myAes.CreateEncryptor(Key, iv); 
     MemoryStream memoryStream = new MemoryStream(); 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); 
     cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); 
     cryptoStream.FlushFinalBlock(); 
     byte[] cipherTextBytes = memoryStream.ToArray(); 
     // Close both streams. 
     memoryStream.Close(); 
     cryptoStream.Close(); 
     return cipherTextBytes; 
    } 

    public static string Decrypt(byte[] cipherTextBytes, byte[] Key) 
    { 
     byte[] iv = new byte[Key.Length]; 
     Aes myAes = Aes.Create(); 
     ICryptoTransform decryptor = myAes.CreateDecryptor(Key, iv); 
     MemoryStream memoryStream = new MemoryStream(cipherTextBytes); 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); 
     byte[] plainTextBytes = new byte[cipherTextBytes.Length]; 
     // Start decrypting. 
     int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); 
     // Close both streams. 
     memoryStream.Close(); 
     cryptoStream.Close(); 
     // Convert decrypted data into a string. 
     // Let us assume that the original plaintext string was UTF8-encoded. 
     string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); 
     // Return decrypted string. 
     return plainText; 
    } 
} 
+0

自己做呢? –

+0

您不應該僅僅因爲您已經自己解決了問題而只是刪除正確提問的問題。你在這裏不僅是爲了獲得幫助,這應該是一個信息來源,即使對於來到這裏也有相同(或類似)問題的其他人。 – Mormegil

回答

3

您正在使用不同的編碼來GetBytes會和GetString:

ASCIIEncoding.Unicode.GetBytes(originalText); 

然後

Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount) 

嘗試使用同一個兩個操作。

Kris

1

你正在弄亂你的字符串編碼。基本上,廢話是ASCIIEncoding.Unicode。 (實際上,它與Encoding.Unicode一樣,但是以誤導的方式編寫。)

您想使用哪種編碼? UTF-16,UTF-8或ASCII碼?根據選擇,使用Encoding.Unicode,Encoding.UTF8或,並堅持下去。我會假定UTF-8是最好的選擇,因此,使用此:

// ... 
string originalText = "HI WORLD"; 
byte[] myMess = Encoding.UTF8.GetBytes(originalText); 
Console.WriteLine("Original text >> " + Encoding.UTF8.GetString(myMess)); 
byte[] myEcnryptedMess = Cryptography.Encrypt(myMess, aesKey); 
Console.WriteLine("Encrypted text >> " + Encoding.UTF8.GetString(myEcnryptedMess)); 
Console.WriteLine("Decrypted text >> " + Cryptography.Decrypt(myEcnryptedMess, aesKey));