2
採取以下代碼:DES加密在C#
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
ICryptoTransform encryptor = des.CreateEncryptor();
// encrypt
byte[] x = UTF8Encoding.UTF8.GetBytes("thisIsATEST");
byte[] enc = encryptor.TransformFinalBlock(x, 0, x.Length);
string savedValue = Convert.ToBase64String(enc);
DESCryptoServiceProvider des1 = new DESCryptoServiceProvider();
des1.Key = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
ICryptoTransform decryptor = des1.CreateDecryptor();
byte[] y = Convert.FromBase64String(savedValue);
// decrypt
byte[] originalAgain = decryptor.TransformFinalBlock(y, 0, y.Length);
System.Text.ASCIIEncoding e = new System.Text.ASCIIEncoding();
string str = e.GetString(originalAgain);
現在這不起作用解密但是如果des1.CreateDecryptor();更改爲des.CreateDecryptor();它工作正常,我不確定爲什麼如果我使用完全相同的密鑰。
它沒有引發異常,它只是不正確地轉換字符串。