3DES に組み込まれている C# を使用して、3DES の暗号化/復号化コードを作成しようとしています。私は次のように構成しています:
this.tDes = new TripleDESCryptoServiceProvider ();
this.tDes.Key = new byte[]{97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112};
this.tDes.IV = new byte[8];
this.tDes.Mode = CipherMode.CBC;
そして、これは私のデクリプト関数です:
public string Decrypt(string CipherText)
{
byte[] cipherData = System.Text.Encoding.UTF8.GetBytes(CipherText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, this.tDes.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.FlushFinalBlock();
byte[] ClearBytes = ms.ToArray();
ms.Close();
cs.Close();
string decriptedData = Convert.ToBase64String(ClearBytes);
return decriptedData;
}
これは私の暗号化機能です:
public string Encrypt(string InputText)
{
byte[] clearData = System.Text.Encoding.UTF8.GetBytes(InputText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, this.tDes.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearData, 0, clearData.Length);
cs.FlushFinalBlock();
byte[] CipherBytes = ms.ToArray();
ms.Close();
cs.Close();
string EncryptedData = Convert.ToBase64String(CipherBytes);
return EncryptedData;
}
しかし、それを復号化しようとすると、C# は "cs.FlushFinalBlock();" 行でエラーをスローします。間違った長さを言っています。正しいサイズは?何が問題ですか?