これについて他の質問が寄せられたことは知っていますが、これまでのところ解決策を提供していないか、まさに私が抱えている問題です.
以下のクラスは、文字列の暗号化と復号化を処理します。渡されるキーとベクトルは常に同じです。
暗号化および復号化される文字列は常に数字であり、ほとんどの場合は機能しますが、復号化時に失敗することがあります (ただし、運用サーバーでのみ)。ローカル環境と運用環境の両方が Windows Server 2003 上の IIS6 にあり、クラスを使用するコードは .ashx ハンドラーにあることに注意してください。本番サーバーで失敗する例は「0000232668」
エラーメッセージは
System.Security.Cryptography.CryptographicException: パディングが無効であり、削除できません。System.Security.Cryptography.RijndaelManagedTransform.DecryptData (Byte[] inputBuffer、Int32 inputOffset、Int32 inputCount、Byte[]& outputBuffer、Int32 outputOffset、PaddingMode paddingMode、Boolean fLast) で
そして、コードのために
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;
}
編集:ご協力いただきありがとうございますが、あなたの回答では問題が明らかにならず、愚かなほど単純なものであることが判明しました。あるサーバーで暗号化された文字列を生成し、それを別のサーバーのハンドラーに渡して復号化と処理を行っていましたが、異なるサーバーで実行すると暗号化の結果が異なることが判明したため、受信サーバーはそれを復号化できませんでした。答えの 1 つが偶然このヒントに出くわしたので、それを受け入れました。