我知道其他問題已被提出,但迄今爲止都沒有提供解決方案,或者確實是我的問題。RijndaelManaged「填充無效且無法刪除」僅在生產中解密時纔會發生
下面的類處理字符串的加密和解密,傳入的鍵和向量始終是相同的。
被加密和解密的字符串總是數字,大部分工作,但偶爾在解密時(但只在生產服務器上)失敗。我應該提到,本地和生產環境都在Windows Server 2003的IIS6中,使用該類的代碼位於.ashx處理程序中。生產服務器上失敗的例子是「0000232668」
該錯誤消息是
System.Security.Cryptography.CryptographicException:填充是無效的,不能刪除。 在System.Security.Cryptography.RijndaelManagedTransform.DecryptData(字節[] INPUTBUFFER,的Int32 inputOffset,的Int32 inputCount,字節[] & OutputBuffer中,的Int32 outputOffset,PaddingMode paddingMode,布爾式Flash)
而對於代碼
public class Aes
{
private byte[] Key;
private byte[] Vector;
private ICryptoTransform EncryptorTransform, DecryptorTransform;
private System.Text.UTF8Encoding UTFEncoder;
public Aes(byte[] key, byte[] vector)
{
this.Key = key;
this.Vector = vector;
// our encyption method
RijndaelManaged rm = new RijndaelManaged();
rm.Padding = PaddingMode.PKCS7;
// create an encryptor and decyptor using encryption method. key and vector
EncryptorTransform = rm.CreateEncryptor(this.Key, this.Vector);
DecryptorTransform = rm.CreateDecryptor(this.Key, this.Vector);
// used to translate bytes to text and vice versa
UTFEncoder = new System.Text.UTF8Encoding();
}
/// Encrypt some text and return a string suitable for passing in a URL.
public string EncryptToString(string TextValue)
{
return ByteArrToString(Encrypt(TextValue));
}
/// Encrypt some text and return an encrypted byte array.
public byte[] Encrypt(string TextValue)
{
//Translates our text value into a byte array.
Byte[] bytes = UTFEncoder.GetBytes(TextValue);
Byte[] encrypted = null;
//Used to stream the data in and out of the CryptoStream.
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write))
{
cs.Write(bytes, 0, bytes.Length);
}
encrypted = memoryStream.ToArray();
}
return encrypted;
}
/// The other side: Decryption methods
public string DecryptString(string EncryptedString)
{
return Decrypt(StrToByteArray(EncryptedString));
}
/// Decryption when working with byte arrays.
public string Decrypt(byte[] EncryptedValue)
{
Byte[] decryptedBytes = null;
using (MemoryStream encryptedStream = new MemoryStream())
{
using (CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write))
{
decryptStream.Write(EncryptedValue, 0, EncryptedValue.Length);
}
decryptedBytes = encryptedStream.ToArray();
}
return UTFEncoder.GetString(decryptedBytes);
}
/// Convert a string to a byte array. NOTE: Normally we'd create a Byte Array from a string using an ASCII encoding (like so).
// System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
// return encoding.GetBytes(str);
// However, this results in character values that cannot be passed in a URL. So, instead, I just
// lay out all of the byte values in a long string of numbers (three per - must pad numbers less than 100).
public byte[] StrToByteArray(string str)
{
if (str.Length == 0)
throw new Exception("Invalid string value in StrToByteArray");
byte val;
byte[] byteArr = new byte[str.Length/3];
int i = 0;
int j = 0;
do
{
val = byte.Parse(str.Substring(i, 3));
byteArr[j++] = val;
i += 3;
}
while (i < str.Length);
return byteArr;
}
// Same comment as above. Normally the conversion would use an ASCII encoding in the other direction:
// System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
// return enc.GetString(byteArr);
public string ByteArrToString(byte[] byteArr)
{
byte val;
string tempStr = "";
for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
{
val = byteArr[i];
if (val < (byte)10)
tempStr += "00" + val.ToString();
else if (val < (byte)100)
tempStr += "0" + val.ToString();
else
tempStr += val.ToString();
}
return tempStr;
}
編輯:謝謝你的所有幫助,但你的答案並沒有解決問題,結果是愚蠢的簡單。我在一臺服務器上生成一個加密的字符串,並將其交給另一臺服務器上的一個處理程序進行解密和處理,但事實證明,在不同服務器上運行加密的結果不同,因此接收服務器無法解密它。其中一個答案絆倒了這個意外,這就是爲什麼我接受它
對於這些意見,感到抱歉,這是來自其他問題的「借來的代碼」。 「 」它來自哪裏「 字符串或代碼? – 2010-01-22 10:43:26
字符串。很高興它是借來的代碼,我不必小心翼翼... – 2010-01-22 10:44:14
此外,爲什麼它會在當地環境中工作,如果是這樣的話?不質疑你的答案,看起來很奇怪 – 2010-01-22 10:44:34