C# flatter和C语言中的加密和解密问题#
我使用链接在颤振中使用加密: 代码是C# flatter和C语言中的加密和解密问题#,c#,encryption,flutter,C#,Encryption,Flutter,我使用链接在颤振中使用加密: 代码是 final plainText = 'some plain text here'; final key = encrypt.Key.fromUtf8('16 characters key'); final iv = IV.fromLength(16); final encrypter = Encrypter(AES(key,mode: AESMode.cbc,padding: 'PKCS7')); final encr
final plainText = 'some plain text here';
final key = encrypt.Key.fromUtf8('16 characters key');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key,mode: AESMode.cbc,padding: 'PKCS7'));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print(decrypted);
print(encrypted.base64);
C代码(用于解密)
我用flatter-dart代码加密了一些明文,但在用c#code解密加密的文本后,我没有得到正确的明文
我收到消息了
填充无效,无法删除
在颤振中使用线下 最终iv=iv.fromUtf8('16个字符键') 这对我很有用: 关键点: 飘动
final plainText = 'Works!';
final key = encrypt.Key.fromUtf8(keySTR);
final iv = encrypt.IV.fromUtf8(ivSTR);
final encrypter = encrypt.Encrypter(encrypt.AES(key,mode: encrypt.AESMode.cbc,padding: 'PKCS7'));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print('decrypted:'+decrypted);
print('encrypted.base64:'+encrypted.base64);
C#
密钥的长度不是16字节,因为它说:)
IV=keyBytes
是可疑的,`final IV=IV.fromLength(16);`也是。不管你在做什么,它都不安全。要了解发生了什么,请以十六进制打印AES的所有输入/输出-主要是key和IV-并检查两侧是否相同。如果明文的大小确实超过16个字符/字节,则密钥很可能是罪魁祸首。不要同时设置密钥和密钥大小。每个更新/重置另一个。
String keySTR = "16 characters"; //16 byte
String ivSTR = "16 characters"; //16 byte
final plainText = 'Works!';
final key = encrypt.Key.fromUtf8(keySTR);
final iv = encrypt.IV.fromUtf8(ivSTR);
final encrypter = encrypt.Encrypter(encrypt.AES(key,mode: encrypt.AESMode.cbc,padding: 'PKCS7'));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print('decrypted:'+decrypted);
print('encrypted.base64:'+encrypted.base64);
using (System.Security.Cryptography.RijndaelManaged rjm =
new System.Security.Cryptography.RijndaelManaged
{
KeySize = 128,
BlockSize = 128,
Key = ASCIIEncoding.ASCII.GetBytes(keySTR),
IV = ASCIIEncoding.ASCII.GetBytes(ivSTR)
}
)
{
Byte[] input = Encoding.UTF8.GetBytes(stringToEncrypt);
Byte[] output = rjm.CreateEncryptor().TransformFinalBlock(input, 0, input.Length);
return Convert.ToBase64String(output);
}
}