C# 在CBC中生成密钥
我是C的新手# 我生成了一个密钥C# 在CBC中生成密钥,c#,class,key,aes,C#,Class,Key,Aes,我是C的新手# 我生成了一个密钥 myRijndaelManaged.GenerateIV(); myRijndaelManaged.GenerateKey() 课堂上 public string EncryptText(string plainText) { using (myRijndael = new RijndaelManaged()) { RijndaelManaged myRijndaelManaged = new
myRijndaelManaged.GenerateIV();
myRijndaelManaged.GenerateKey()代码>
课堂上
public string EncryptText(string plainText)
{
using (myRijndael = new RijndaelManaged())
{
RijndaelManaged myRijndaelManaged = new RijndaelManaged();
myRijndaelManaged.Mode = CipherMode.CBC;
myRijndaelManaged.Padding = PaddingMode.PKCS7;
myRijndaelManaged.GenerateIV();
myRijndaelManaged.GenerateKey();
string newKey = ByteArrayToHexString(myRijndaelManaged.Key);
string newinitVector = ByteArrayToHexString(myRijndaelManaged.IV);
byte[] encrypted = EncryptStringToBytes(plainText, myRijndael.Key, myRijndael.IV);
string encString = Convert.ToBase64String(encrypted);
return encString;
}
}
如何在课堂上给出相同的答案
public string DecryptText(string encryptedString)
{
using (myRijndael = new RijndaelManaged())
{
myRijndael.Key =newKey;
myRijndael.IV = newinitVector;
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
Byte[] ourEnc = Convert.FromBase64String(encryptedString);
string ourDec = DecryptStringFromBytes(ourEnc, myRijndael.Key, myRijndael.IV);
return ourDec;
}
}
当我给另一把钥匙时,我有一个问题
System.Security.Cryptography.CryptographyException:“填充无效,无法删除。”虽然我不擅长加密,但对我来说还是有意义的。我的意思是,您应该将生成的iv和密钥传递给解密函数。
例如:
public string DecryptText(string encryptedString, string Iv, string Key)
{
using (myRijndael = new RijndaelManaged())
{
myRijndael.Key = Key;
myRijndael.IV = Iv;
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
Byte[] ourEnc = Convert.FromBase64String(encryptedString);
string ourDec = DecryptStringFromBytes(ourEnc, myRijndael.Key, myRijndael.IV);
return ourDec;
}
}
嗯,我不确定Iv和Key的数据类型是什么,请根据自己的情况进行更改