【发布时间】: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