c语言如何加密数组,在使用OpenSSL-nosalt加密文件的C中解密AES;AES需要一个16字节的数组IV?...

我有一个用这个OpenSSL命令加密的文件(无盐):

openssl enc -nosalt -aes-128-cbc -in my_file.txt -out my_file.txt.enc -p -pass pass:hello_this_is_pass

key=2F77B7A1D3BBAA3304E53D791819958A

iv =9DD22E07DD38AF129D42E8CF3689EADD

一旦进入VS,这些数据就被读入字节数组中:

byte[] key = Encoding.ASCII.GetBytes("2F77B7A1D3BBAA3304E53D791819958A");

byte[] iv = Encoding.ASCII.GetBytes("9DD22E07DD38AF129D42E8CF3689EADD");

var encodedBytes = File.ReadAllBytes("myEncFile.txt.enc");

将文件读入一个字节数组:

这将从文档传递到reading方法:

static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)

{

// Declare the string used to hold

// the decrypted text.

string plaintext = null;

// Create an Aes object

// with the specified key and IV.

using (Aes aesAlg = Aes.Create())

{

aesAlg.Key = Key;

aesAlg.IV = IV; // **This is the line that errors**

// Create a decryptor to perform the stream transform.

ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for decryption.

using (MemoryStream msDecrypt = new MemoryStream(cipherText))

{

using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))

{

using (StreamReader srDecrypt = new StreamReader(csDecrypt))

{

// Read the decrypted bytes from the decrypting stream

// and place them in a string.

plaintext = srDecrypt.ReadToEnd();

}

}

}

}

return plaintext;

}

设置

aesAlg.IV

System.Security.Cryptography.CryptographicException: 'Specified initialization vector (IV) does not match the block size for this algorithm.'

它看起来像的默认值埃萨尔格四世是字节[16]。我知道这意味着我提供的IV是它应该的两倍大,所以我认为我一定是使用了错误的解密设置

密钥大小是byte[32],在分配之前我已经尝试过配置实例埃萨尔格四世,但这些变化似乎没有影响:

aesAlg.Mode = CipherMode.CBC;

aesAlg.IVSize = 128;

aesAlg.BlockSize = 128;

aesAlg.FeedbackSize = 128;

aesAlg.Padding = PaddingMode.None;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值