OpenSSL AES256 cbc 加密

发布于 2024-12-22 07:28:57 字数 1030 浏览 3 评论 0原文

几天来我试图在互联网上找到合适的例子,但没有成功。我尝试用密钥加密简单的字符串(Hello World),然后解密结果。 但解密结果与原文无关。有人能给我指个方向吗?

我制作的代码:

AES_KEY aes_decryptKey;
AES_KEY aes_encryptKey;

const unsigned char mykey[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa};
unsigned char encrypted ;
unsigned char iv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};    
unsigned char decrypted;

AES_set_encrypt_key(mykey, 256, &aes_encryptKey);
AES_set_decrypt_key(mykey, 256, &aes_decryptKey);

const unsigned char original[]  = {0x48,0x65,0x6c,0x6c,0x6f,0x2c,0x77,0x6f,0x72,0x6c,0x64,0x21};

AES_cbc_encrypt(original, &encrypted, sizeof(original), &aes_encryptKey, iv, 0);

AES_decrypt( &encrypted, &decrypted, &aes_decryptKey);

NSLog(@"ORIGINAL: \"%s\"\n",original);
NSLog(@"ENCRYPTED: \"%s\"\n",&encrypted);
NSLog(@"DECRYPTED: \"%s\"\n",&decrypted);

I tried to find a proper example on the Internet for days but did not succeed. I try to encrypt simple string (Hello World) with a key, then to decrypt the result.
However the decrypted result has nothing to do with the original text. Can anyone point me to a direction please?

The code I made:

AES_KEY aes_decryptKey;
AES_KEY aes_encryptKey;

const unsigned char mykey[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa};
unsigned char encrypted ;
unsigned char iv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};    
unsigned char decrypted;

AES_set_encrypt_key(mykey, 256, &aes_encryptKey);
AES_set_decrypt_key(mykey, 256, &aes_decryptKey);

const unsigned char original[]  = {0x48,0x65,0x6c,0x6c,0x6f,0x2c,0x77,0x6f,0x72,0x6c,0x64,0x21};

AES_cbc_encrypt(original, &encrypted, sizeof(original), &aes_encryptKey, iv, 0);

AES_decrypt( &encrypted, &decrypted, &aes_decryptKey);

NSLog(@"ORIGINAL: \"%s\"\n",original);
NSLog(@"ENCRYPTED: \"%s\"\n",&encrypted);
NSLog(@"DECRYPTED: \"%s\"\n",&decrypted);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-12-29 07:28:57

您正在解密两次 - AES_cbc_encrypt 的最后一个 0 参数应该是 1 或 AES_ENCRYPT。

此外,您正在覆盖您的加密和解密字符,这些字符应该是足够大的数组来容纳原始的加密大小。而不是:

unsigned char encrypted;
...
AES_cbc_encrypt(original, &encrypted, ...

使用类似:

unsigned char encrypted[32];
...
AES_cbc_encrypt(original, encrypted, ...

也类似:

unsigned char decrypted[32];
....
AES_decrypt(encrypted, decrypted, &aes_decryptKey);

查看此链接: http:// /marc.info/?l=openssl-users&m=122919878204439。我还不能保证这一切——等我有时间的时候我会回来编辑我的答案。

You are decrypting twice -- that last 0 parameter for AES_cbc_encrypt should be a 1 or AES_ENCRYPT.

Also, you are overwriting your encryption and decryption chars, which should instead be arrays big enough to hold the encrypted size of original. Instead of:

unsigned char encrypted;
...
AES_cbc_encrypt(original, &encrypted, ...

use something like:

unsigned char encrypted[32];
...
AES_cbc_encrypt(original, encrypted, ...

And also something like:

unsigned char decrypted[32];
....
AES_decrypt(encrypted, decrypted, &aes_decryptKey);

Check out this link: http://marc.info/?l=openssl-users&m=122919878204439. I can't vouch for it all yet -- I'll come back and edit my answer later when I have time.

如梦初醒的夏天 2024-12-29 07:28:57

吉姆,谢谢你的帮助。

看来我必须提出问题才能找到答案。
经过很多天的努力,这就是我想出的结果:

    unsigned char inbuf[1024]="Hello,world!";
unsigned char encbuf[1024];

unsigned char key32[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa};
unsigned char deckey32[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa}
;
unsigned char iv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};    
unsigned char deciv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};    

AES_KEY aeskey;
AES_KEY aesdeckey;

//Now enrypt
memset(encbuf, 0, sizeof(encbuf));
AES_set_encrypt_key(key32, 32*8, &aeskey);
AES_cbc_encrypt(inbuf, encbuf, 16, &aeskey, iv, AES_ENCRYPT);

//Now decrypt
unsigned char decbuf[1024];
memset(decbuf, 0, sizeof(decbuf));

AES_set_decrypt_key(deckey32, 32*8, &aesdeckey);
AES_cbc_encrypt(encbuf, decbuf, 16, &aesdeckey, deciv, AES_DECRYPT);


//Display the results
NSLog(@"ORIGINAL: \"%s\"\n", inbuf);
NSLog(@"ENCRYPTED: \"%s\"\n", encbuf);
NSLog(@"DECRYPTED: \"%s\"\n", decbuf);

return;

感谢这些人(在吉姆之后):http://www.mail-archive.com/[email protected]/msg50142.html

关键是使用 AES_cbc_encrypt 进行解密。

Jim, thanks for your help.

Seems I had to raise a question to find the answer.
After struggling many days this is what I came up with:

    unsigned char inbuf[1024]="Hello,world!";
unsigned char encbuf[1024];

unsigned char key32[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa};
unsigned char deckey32[] = {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa}
;
unsigned char iv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};    
unsigned char deciv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};    

AES_KEY aeskey;
AES_KEY aesdeckey;

//Now enrypt
memset(encbuf, 0, sizeof(encbuf));
AES_set_encrypt_key(key32, 32*8, &aeskey);
AES_cbc_encrypt(inbuf, encbuf, 16, &aeskey, iv, AES_ENCRYPT);

//Now decrypt
unsigned char decbuf[1024];
memset(decbuf, 0, sizeof(decbuf));

AES_set_decrypt_key(deckey32, 32*8, &aesdeckey);
AES_cbc_encrypt(encbuf, decbuf, 16, &aesdeckey, deciv, AES_DECRYPT);


//Display the results
NSLog(@"ORIGINAL: \"%s\"\n", inbuf);
NSLog(@"ENCRYPTED: \"%s\"\n", encbuf);
NSLog(@"DECRYPTED: \"%s\"\n", decbuf);

return;

Credits to these guys (after Jim): http://www.mail-archive.com/[email protected]/msg50142.html

The key was to use AES_cbc_encrypt to decrypt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文