【发布时间】:2015-05-20 04:54:29
【问题描述】:
我正在使用linux内核中cryptoAPI的AES算法进行加密解密。如果在加密后立即进行解密,则以下代码可以正常工作。但我希望稍后再做这件事,然后它就给垃圾了。我正在存储加密密钥以供以后解密。
代码:
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;
}
}
【问题讨论】:
-
它总是给我垃圾。我在加密方法的末尾调用了解密方法。任何想法?当您说“如果在加密后立即进行解密就可以正常工作”时,您是如何做到的
-
是的,我也遇到了同样的情况。你能解决“加密后才能解密”的问题吗?