- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
以下示例在 64 位桌面 Ubuntu 16.04 上正确地加密和解密为相同的原始字符串,但是当相同的代码在 Raspberry Pi (ARM)(以及另一个自定义 Linux ARM 板)上编译和运行时,它会失败解密为原始字符串。 Raspberry Pi 和其他 ARM 板都解密为相同但不正确的值。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/crypto.h>
/* AES key for Encryption and Decryption */
const static unsigned char aes_key[]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
/* Print Encrypted and Decrypted data packets */
void print_data(const char *tittle, const void* data, int len);
int main( )
{
/* Input data to encrypt */
unsigned char aes_input[]={0x0,0x1,0x2,0x3,0x4,0x5};
fprintf(stderr,"%s\n",SSLeay_version(SSLEAY_VERSION));
/* Init vector */
unsigned char iv[AES_BLOCK_SIZE];
memset(iv, 0x00, AES_BLOCK_SIZE);
/* Buffers for Encryption and Decryption */
unsigned char enc_out[sizeof(aes_input)];
unsigned char dec_out[sizeof(aes_input)];
/* AES-128 bit CBC Encryption */
AES_KEY enc_key, dec_key;
AES_set_encrypt_key(aes_key, sizeof(aes_key)*8, &enc_key);
AES_cbc_encrypt(aes_input, enc_out, sizeof(aes_input), &enc_key, iv, AES_ENCRYPT);
/* AES-128 bit CBC Decryption */
memset(iv, 0x00, AES_BLOCK_SIZE); // don't forget to set iv vector again, else you can't decrypt data properly
AES_set_decrypt_key(aes_key, sizeof(aes_key)*8, &dec_key); // Size of key is in bits
AES_cbc_encrypt(enc_out, dec_out, sizeof(aes_input), &dec_key, iv, AES_DECRYPT);
/* Printing and Verifying */
print_data("\n Original ",aes_input, sizeof(aes_input)); // you can not print data as a string, because after Encryption its not ASCII
print_data("\n Encrypted",enc_out, sizeof(enc_out));
print_data("\n Decrypted",dec_out, sizeof(dec_out));
return 0;
}
void print_data(const char *tittle, const void* data, int len)
{
printf("%s : ",tittle);
const unsigned char * p = (const unsigned char*)data;
int i = 0;
for (; i<len; ++i)
printf("%02X ", *p++);
printf("\n");
}
Ubuntu 结果:
OpenSSL 1.0.1f 6 Jan 2014
Original : 00 01 02 03 04 05
Encrypted : D5 40 D0 BB 16 1D
Decrypted : 00 01 02 03 04 05
树莓派结果:
OpenSSL 1.0.2l 25 May 2017
Original : 00 01 02 03 04 05
Encrypted : D5 40 D0 BB 16 1D
Decrypted : D3 87 81 20 2B B9
自定义看板结果:
OpenSSL 1.1.0f 25 May 2017
Original : 00 01 02 03 04 05
Encrypted : D5 40 D0 BB 16 1D
Decrypted : D3 87 81 20 2B B9
自定义板(更新 OpenSSL 以匹配 Ubuntu):
OpenSSL 1.0.1f 6 Jan 2014
Original : 00 01 02 03 04 05
Encrypted : D5 40 D0 BB 16 1D
Decrypted : D3 87 81 20 2B B9
为什么开源 libcrypto 在 Ubuntu 和 2 台不同的 ARM 机器上表现不同?
最佳答案
通常在 CBC 模式下,您使用的缓冲区大小是密码 block 大小的倍数。这是 CBC 的本质,在 CBC 类型例程的每个 openssl 手册页中都提到了它(不幸的是,我找不到手册页或任何关于 AES_cbc_encrypt
的文档)。
幸运的是,错误的缓冲区大小似乎在某些平台上有效。要查看实际情况,请为 enc_out
多分配一个字节,并在加密后将该额外字节清零。解密会失败。
unsigned char enc_out[sizeof(aes_input) + 1];
...
AES_cbc_encrypt(aes_input, enc_out, sizeof(aes_input), &enc_key, iv, AES_ENCRYPT);
enc_out[sizeof(aes_input)] = 0;
修改后的代码在我的机器上的输出:
Original : 00 01 02 03 04 05
Encrypted : D5 40 D0 BB 16 1D 00
Decrypted : 89 FB 06 F4 CD 6A
未经修改的代码会产生“正确”的输出。
关于c - Linux libcrypto AES-128 CBC 加密/解密适用于 Ubuntu 但不适用于 Raspberry Pi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46223376/
我在Linux上安装了Openssl,Openssl-dev。配置Thrift时,出现以下错误: 正在检查-lcrypto中的BN_init ...否 配置:错误:“错误:需要libcrypto。”
当我尝试编译使用 openssl 'crypto' 库函数和命令行 -lcrypto 和 gcc 4.4.3 的 C 代码时> 它给出了一个错误 `@ubu:$ gcc -ggdb aes_m.c -
我正在尝试在 XCODE 4.2 上为 mac 编译一个较旧的 objective-c 应用程序 尝试编译时出现链接错误 无法直接与架构 x86_64 的/usr/lib/libssl.0.9.7.d
我正在遵循以下位置的代码示例:http://www.openssl.org/docs/crypto/sha.html# 执行以下操作后: EVP_DigestFinal_ex(&mdctx, md_v
我正在使用 libcrypto++ 进行 aes 加密。我想在我的代码中添加以下方法:EVP_CipherInit_ex()、EVP_CipherUpdate()、EVP_CipherFinal_ex
我正在尝试将一个共享库放在一起,该库向远程服务器发出 HTTP GET 请求。我的库将由另一个应用程序加载,我想将一些统计数据发布到网络服务器 API。我的代码目前看起来像这样: void conne
我以某种方式设法在 Ubuntu 14.04 上安装了 libcrypto++ 库。现在,我在 Eclipse CDT 中有一个项目,我想创建一个可执行文件并使其独立运行在其他 linux 风格和 w
我有以下生成文件,我将以静态方式添加库“libcrypto.a”。我需要这样做,因为目标系统无法安装 openssl 库。 # Environment MKDIR=mkdir
我想尝试一个带有 makefile 的程序,但是当我将 make 放入 shell 时,错误是: g++ -g -DaUNIX -I../../acroname/aInclude -I../../a
我使用以下方法创建了一个 RSA key : RSA_generate_key(2048, RSA_F4, NULL, NULL); 现在我想将公钥导出到另一方 B。现在,我刚刚存储了整个 RSA*
我使用的是 Mac OS X 10.6 SDK,并且我的部署目标设置为 Mac OS 10.5。我链接到 libcrypto(AquaticPrime 需要这个)并发现我的应用程序无法在 Leopar
我自己编译php7。 下载php7源代码。 yum安装libxml2-devel openssl-devel.i686 配置 ./configure --prefix=/usr/local/php7
我看到了这个问题“找不到包‘libcrypto’”。我几个月前修复的相同错误是遵循以下解决方案:https://github.com/scipr-lab/libsnark/issues/99 我今天尝
我需要签署一个字符串,将公钥作为字符串发布,然后在其他地方使用公钥来验证签名的消息。这是消息签名的部分: // RSA keypair generation EVP_P
我正在尝试使用 Debian Wheezy 和 g++4.7 在我的系统上编译一个程序。我希望它能够在另一个带有 Debian Squeeze 的系统上运行(并且没有最近的 g++)。我无法在 Squ
这更像是一个假设,而我正在调试一些代码。假设我有一个应用程序(称为 X)调用一个库以通过 TLS 加密的 SMTP 连接发送电子邮件,同时 X 正在与另一个库交谈,该库通过相同的 libcrypto
我正在为 Android 应用构建一个帮助程序库,我需要为我正在使用的一些支持库提供完整的 OpenSSL 实现。我正在创建一个构建链并用它编译 libssl 和 libcrypto 但在运行时我得到
我有一个 C 文件需要在 Windows7 上编译。我已经为 gcc 安装了 MinGW。我还需要 OpenSSL,因此点击他们网站上的链接并从 Shining Light Productions 下
近 3 天以来,我一直在尝试这样做。它让我发疯。 正如您想象的那样,我对 C++ 还很陌生。 任何人都可以一步一步地指导我使用 openssl 中的函数编写一个 hello world 程序,并从 W
案例: 我正在构建一个使用 libcrypto 和 libssl 的应用程序。我正在尝试使用预构建的 libcrypto.so 和 libssl.so 并编译我的应用程序。 但我不断收到 undefi
我是一名优秀的程序员,十分优秀!