【问题标题】:Can MCrypt check to see if the hash created was modified?MCrypt 可以检查创建的哈希是否被修改?
【发布时间】:2014-11-08 02:31:31
【问题描述】:

我有 2 个函数来加密和解密一个字符数组(变量称为:缓冲区),然后我将字符保存在一个文件中,以便稍后解密它,但我注意到如果我修改任何密文中的字符,mcrypt 不警告任何错误,它只是在一段文本中显示不同的疯狂字符,其余看起来很完美,我想说密文字符串有错误。

这是一种方法吗?

当文本很小时这并不明显,但因为我散列了 168k 个字符的文本。

我的变量传递给函数:buffer_len = 32; 我的IV 和key 每个都是 16 位字符的列表。

int encrypt(void* buffer, int buffer_len, char* IV, char* key, int key_len)
{
    MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
    int blocksize = mcrypt_enc_get_block_size(td);
    int n_blocks = buffer_len / blocksize;
    int i = 0;
    if (buffer_len % blocksize != 0)
        return 1;

    mcrypt_generic_init(td, key, key_len, IV);
    for (i = 0; i < n_blocks; i++)
        mcrypt_generic(td, ((unsigned char*)buffer) + (i * blocksize), blocksize);
    mcrypt_generic_deinit(td);
    mcrypt_module_close(td);

    return 0;
}

int decrypt(void* buffer, int buffer_len, char* IV, char* key, int key_len){
    MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
    int blocksize = mcrypt_enc_get_block_size(td);
    int n_blocks = buffer_len / blocksize;
    int i = 0;

    if (buffer_len % blocksize != 0)
        return 1;

    mcrypt_generic_init(td, key, key_len, IV);
    for (i = 0; i < n_blocks; i++)
        mdecrypt_generic(td, ((unsigned char *)buffer) + (i * blocksize), blocksize);
    mcrypt_generic_deinit(td);
    mcrypt_module_close(td);

    return 0;
}

【问题讨论】:

  • 这是一个php页面,我知道它可能也适用于C,但是它是怎么做的呢?你能给我举个例子吗?为什么 HMAC 会帮助它?
  • 我的错,我什至没有看你的代码。

标签: c encryption mcrypt


【解决方案1】:

mcrypt 的最后一个错误修复是从 2007 年开始的。使用实际维护的库并使用 HMAC 功能添加身份验证标签(这也应该提供完整性)。不要忘记在计算中包含 IV。

【讨论】:

  • 您能推荐一些用 C 语言实现的简单方法吗?
  • 试用 OpenSSL 的 EVP API。
猜你喜欢
  • 2021-06-25
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 2011-06-16
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
相关资源
最近更新 更多