2012-02-09 91 views
2

我在linux内核中使用cryptoAPI的AES算法进行加密解密。如果解密在加密后立即完成,以下代码正常工作。但我希望稍后再做,然后给它垃圾。我正在存储加密密钥以供以后解密。加密api在linux内核中出现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; 
    } 
} 
+0

它总是给我垃圾。我只是在加密方法结束时调用解密方法。任何想法?当你说“如果解密是在加密之后立即完成的话,工作正常”,你是怎么回避的? – 2015-09-29 01:48:36

+0

是的,我也一样。你能解决'只能在加密后解密'的问题吗? – 2015-09-29 03:58:51

回答

0

我觉得这个API

crypto_cipher_encrypt_one(tfm,dst,src);

从src.Maybe必须的差异DST可以尝试的alloc DST [16 ]。

我尝试差异化和成功。