我在linux内核中使用cryptoAPI的AES算法进行加密解密。如果在加密后立即进行解密,则以下代码可以正常工作。但我希望稍后再做,然后它会产生垃圾。我正在存储加密 key 以供以后解密。
代码:
void encrypt(char *buf,u8 *key1)
{
struct crypto_cipher *tfm;
int i,count,div,modd;
div=strlen(buf)/AES_BLOCK_SIZE;
modd=strlen(buf)%AES_BLOCK_SIZE;
if(modd>0)
div++;
count=div;
tfm=crypto_alloc_cipher("aes", 0, 16);
crypto_cipher_setkey(tfm,key1,16);
for(i=0;i<count;i++)
{
crypto_cipher_encrypt_one(tfm,buf,buf);
buf=buf+AES_BLOCK_SIZE;
}
crypto_free_cipher(tfm);
}
和:
void decrypt(char *buf,u8 *key1)
{
struct crypto_cipher *tfm;
int i,count,div,modd;
div=strlen(buf)/AES_BLOCK_SIZE;
modd=strlen(buf)%AES_BLOCK_SIZE;
if(modd>0)
div++;
count=div;
tfm=crypto_alloc_cipher("aes", 0, 16);
crypto_cipher_setkey(tfm,key1,16);
for(i=0;i<count;i++)
{
crypto_cipher_decrypt_one(tfm,buf,buf);
buf=buf+AES_BLOCK_SIZE;
}
}
最佳答案
我觉得这个api
crypto_cipher_encrypt_one(tfm,dst,src);
必须将 dst 与 src 区分开来。也许你可以尝试分配 dst[16]。
我尝试 diff,并成功。
关于linux - Linux 内核中用于 AES 错误的加密 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9218491/