Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的MCrypt,为什么不是';加密不是将明文更改为哈希/加密数据吗?_C_Mcrypt - Fatal编程技术网

C中的MCrypt,为什么不是';加密不是将明文更改为哈希/加密数据吗?

C中的MCrypt,为什么不是';加密不是将明文更改为哈希/加密数据吗?,c,mcrypt,C,Mcrypt,我有一段代码,我从中获得,用mcrypt加密和解密数据,但问题是,我想把明文加密成加密生成的文本,这样我可以在加密后传递要解密的文本 但是当我执行下面的代码时,我得到了以下输出: plain: myText encrypt: myText decrypt: myText 它没有改变任何东西,从普通到加密,我做错了什么吗?如何获取加密生成的值,以便返回函数,使其随后解密。 我想要一个大的散列序列,使它更安全,可以用我用来加密它的相同私钥解密 int encrypt(void* buffer

我有一段代码,我从中获得,用mcrypt加密和解密数据,但问题是,我想把明文加密成加密生成的文本,这样我可以在加密后传递要解密的文本

但是当我执行下面的代码时,我得到了以下输出:

plain:   myText
encrypt: myText
decrypt: myText
它没有改变任何东西,从普通到加密,我做错了什么吗?如何获取加密生成的值,以便返回函数,使其随后解密。 我想要一个大的散列序列,使它更安全,可以用我用来加密它的相同私钥解密

 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;
    }

    int main()
    {
        MCRYPT td, td2;
        char * plaintext = "myText";
        char* IV = "AAAAAAAAAAAAAAAA";
        char *key = "0123456789abcdef";
        int keysize = 16; /* 128 bits */
        char* buffer;
        int buffer_len = 6;


        buffer = calloc(1, buffer_len);
        strncpy(buffer, plaintext, buffer_len);

        printf("==C==\n");
        printf("plain:   %s\n", plaintext);

        encrypt(buffer, buffer_len, IV, key, keysize);
        printf("encrypt: %s\n", buffer);

        decrypt(buffer, buffer_len, IV, key, keysize);
        printf("decrypt: %s\n", buffer);

        return 0;
    }
int加密(void*buffer,int buffer\u len,char*IV,char*key,int key\u len)
{
MCRYPT td=MCRYPT模块打开(“rijndael-128”,空,“cbc”,空);
int blocksize=mcrypt\u enc\u get\u block\u size(td);
int n_blocks=缓冲区长度/块大小;
int i=0;
如果(缓冲区长度%blocksize!=0)
返回1;
mcrypt_generic_init(td,key,key_len,IV);
对于(i=0;i
在代码中,使用
int buffer\u len=6
您从未在
加密
中执行
mcrypt\u generic
。具体来说,在
加密中有以下内容:

int n_blocks = buffer_len / blocksize;
...
mcrypt_generic_init (td, key, key_len, IV);
for (i = 0; i < n_blocks; i++)
mcrypt_generic (td, ((unsigned char *) buffer) + (i * blocksize),
        blocksize);
buffer\u len
(6)不是块大小(16字节)的精确倍数,这是它没有被加密/解密的一个很好的原因。
./bin/mcry
==C==
plain:   myText
encrypt: '���J�Ꮽ
decrypt: myText