2012-02-09 62 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 ]。

我嘗試差異化和成功。