【问题标题】:Crypto api in linux kernel for AES error用于 AES 错误的 Linux 内核中的加密 API
【发布时间】: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;  
    }  
}  

【问题讨论】:

  • 它总是给我垃圾。我在加密方法的末尾调用了解密方法。任何想法?当您说“如果在加密后立即进行解密就可以正常工作”时,您是如何做到的
  • 是的,我也遇到了同样的情况。你能解决“加密后才能解密”的问题吗?

标签: linux kernel aes


【解决方案1】:

我觉得这个api

crypto_cipher_encrypt_one(tfm,dst,src);

必须区分 dst 和 src。也许你可以尝试分配 dst[16]。

我尝试区分,然后成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    相关资源
    最近更新 更多