linux - Linux 内核中用于 AES 错误的加密 api

标签 linux kernel aes

我在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/

相关文章:

linux - 在 bash 脚本的 shebang 中使用 screen session

c++ - 编译错误VTK,C++

linux - OpenGL:Linux 上的渲染时间限制

c - 关于Android内核的几个问题

c - AES key 表

python - 为什么 PyCrypto 不允许使用私钥编码和使用公钥解码来验证发送者身份

c - 调试器如何从代码行中查找表达式

c - 从内核获取当前驱动器路径

linux - 驱动程序打开中的 inode 参数

java - 如何使用 JCE 中的 PBEWITHHMACSHA256ANDAES_256 算法