我使用.NET加密文件。
接下来,我尝试使用OpenSSL对其进行解密,但是我遇到了一个问题:
bad decrypt
4294956672:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:529:
我简化了示例:
所以
1)我从OpenSSL获得IV和密钥
openssl enc -aes-256-cbc -pass pass:myPassword -P -nosalt
key=69BD0330B47FF638C3471005819C28F7B938830888101C9135EF41D8641F2709
iv =71C334727A6C5DE704C21965E9BAC0F8
2)我加密了文件:
调用:
key = new byte[]{0x69, 0xBD, 0x03, 0x30, 0xB4, 0x7F, 0xF6, 0x38, 0xC3, 0x47, 0x10, 0x05, 0x81, 0x9C, 0x28, 0xF7, 0xB9, 0x38, 0x83, 0x08, 0x88, 0x10, 0x1C, 0x91, 0x35, 0xEF, 0x41, 0xD8, 0x64, 0x1F, 0x27, 0x09};
iv = new byte[]{0x71, 0xC3, 0x34, 0x72, 0x7A, 0x6C, 0x5D, 0xE7, 0x04, 0xC2, 0x19, 0x65, 0xE9, 0xBA, 0xC0, 0xF8};
byte[] response = await EncryptString(textWriter.ToString(), key, iv);
功能:
public async Task<byte[]> EncryptString(string plainText, byte[] key, byte[] iv)
{
Aes encryptor = Aes.Create();
encryptor.Mode = CipherMode.CBC;
encryptor.Key = key;
encryptor.Padding = PaddingMode.PKCS7;
encryptor.BlockSize = 128;
encryptor.IV = iv;
encryptor.KeySize = 256;
// Convert the plainText string into a byte array
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
using (MemoryStream memoryStream = new MemoryStream())
{
ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write))
{
await cryptoStream.WriteAsync(plainBytes, 0, plainBytes.Length);
}
return memoryStream.ToArray();
}
}
3)我将字节数组写入文件(必须使用FTP)
public async Task UploadFileAsync(Uri uri, string login, string password, byte[] content)
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
SetFtpCredentials(ftpRequest, login, password);
using (var stream = new BinaryWriter(ftpRequest.GetRequestStream()))
{
stream.Write(content,0,content.Length);
}
using (var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
{
using (Stream ftpStream = ftpResponse.GetResponseStream())
{
}
}
}
4)我尝试解密文件
openssl.exe aes-256-cbc -in input.xml -d -out output.xml -md sha256 -nosalt -debug -K 69BD0330B47FF638C3471005819C28F7B938830888101C9135EF41D8641F2709 -iv 71C334727A6C5DE704C21965E9BAC0F8
响应:
BIO[0x600060ee0]: ctrl(108) - FILE pointer
BIO[0x600060ee0]: ctrl return 1
BIO[0x600060f60]: ctrl(108) - FILE pointer
BIO[0x600060f60]: ctrl return 1
BIO[0x600061080]: ctrl(6) - cipher
BIO[0x600060f60]: ctrl(6) - FILE pointer
BIO[0x600060f60]: ctrl return 0
BIO[0x600061080]: ctrl return 0
BIO[0x600060ee0]: read(0,8192) - FILE pointer
BIO[0x600060ee0]: read return 1328
BIO[0x600061080]: write(0,1328) - cipher
BIO[0x600060f60]: write(0,1312) - FILE pointer
BIO[0x600060f60]: write return 1312
BIO[0x600061080]: write return 1328
BIO[0x600060ee0]: read(0,8192) - FILE pointer
BIO[0x600060ee0]: read return 0
BIO[0x600061080]: ctrl(11) - cipher
BIO[0x600061080]: ctrl return 0
bad decrypt
4294956672:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:529:
BIO[0x600060ee0]: Free - FILE pointer
BIO[0x600060f60]: Free - FILE pointer
BIO[0x600061080]: Free - cipher
我应该解决什么?
答案 0 :(得分:0)
我发现了错误:
encryptor.KeySize = 256;//This line should be removed.
更改KeySize值将重置密钥并生成一个新的随机密钥。每当调用KeySize属性设置器(包括为其分配相同的值)时,就会发生这种情况。
因此,我不应该手动设置KeySize。