【发布时间】:2011-03-09 16:24:54
【问题描述】:
我是 OpenSSL 的新手,谁能告诉我如何从 C 文件初始化 AES CTR 模式。我知道这是方法的签名,但我的参数有问题,文档不多,也没有一个清晰的例子,如何进行简单的加密。如果有人可以举例说明对此方法的调用,我将不胜感激。提前致谢!
void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out,
const unsigned long length, const AES_KEY *key,
unsigned char ivec[AES_BLOCK_SIZE],
unsigned char ecount_buf[AES_BLOCK_SIZE],
unsigned int *num);
您好 Caf,我非常感谢您的快速回答,它非常有用,并且无疑是我在网络上找到的最佳示例。我正在尝试打开一个长度不确定的文件,对其进行加密并使用生成的密文写入另一个文件,然后打开加密文件并恢复明文。我需要使用相当大的 MB 文件,因为我想对 CPU 的性能进行基准测试。但是我在解密时仍然遇到问题。不知何故,当解密相当大的 txt 文件(1504KB)时,它不会完全解密,我得到一半是明文,另一半仍然是加密的。我认为这可能与 iv 的大小或我调用柜台的方式有关。这是我到目前为止所拥有的:
#include <openssl/aes.h>
#include <stdio.h>
#include <string.h>
struct ctr_state {
unsigned char ivec[16];
unsigned int num;
unsigned char ecount[16];
};
FILE *fp;
FILE *rp;
FILE *op;
size_t count;
char * buffer;
AES_KEY key;
int bytes_read, bytes_written;
unsigned char indata[AES_BLOCK_SIZE];
unsigned char outdata[AES_BLOCK_SIZE];
unsigned char ckey[] = "thiskeyisverybad"; // It is 128bits though..
unsigned char iv[8] = {0};//This should be generated by RAND_Bytes I will take into consideration your previous post
struct ctr_state state;
int init_ctr(struct ctr_state *state, const unsigned char iv[8]){
state->num = 0;
memset(state->ecount, 0, 16);
memset(state->ivec + 8, 0, 8);
memcpy(state->ivec, iv, 8);
}
void encrypt(){
//Opening files where text plain text is read and ciphertext stored
fp=fopen("input.txt","a+b");
op=fopen("output.txt","w");
if (fp==NULL) {fputs ("File error",stderr); exit (1);}
if (op==NULL) {fputs ("File error",stderr); exit (1);}
//Initializing the encryption KEY
AES_set_encrypt_key(ckey, 128, &key);
//Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext
while (1) {
init_ctr(&state, iv); //Counter call
bytes_read = fread(indata, 1, AES_BLOCK_SIZE, fp);
AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num);
bytes_written = fwrite(outdata, 1, bytes_read, op);
if (bytes_read < AES_BLOCK_SIZE)
break;
}
fclose (fp);
fclose (op);
free (buffer);
}
void decrypt(){
//Opening files where text cipher text is read and the plaintext recovered
rp=fopen("recovered.txt","w");
op=fopen("output.txt","a+b");
if (rp==NULL) {fputs ("File error",stderr); exit (1);}
if (op==NULL) {fputs ("File error",stderr); exit (1);}
//Initializing the encryption KEY
AES_set_encrypt_key(ckey, 128, &key);
//Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext
while (1) {
init_ctr(&state, iv);//Counter call
bytes_read = fread(indata, 1, AES_BLOCK_SIZE, op);
AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num);
bytes_written = fwrite(outdata, 1, bytes_read, rp);
if (bytes_read < AES_BLOCK_SIZE)
break;
}
fclose (rp);
fclose (op);
free (buffer);
}
int main(int argc, char *argv[]){
encrypt();
//decrypt();
system("PAUSE");
return 0;
}
每个加密和解密函数在不同的运行中被调用,所以所有的东西总是用相同的值初始化。再次感谢您可以提前向我提供的提示和问候!!!
【问题讨论】:
-
您的问题是您在每个块之后重新初始化计数器。这是错误的 - 在加密和解密中将
init_ctr()调用移到while()循环之外。indata和outdata也不必是AES_BLOCK_SIZE长度 - 它们可以大得多。 -
你应该不使用
AES_encrypt和朋友。这是一个纯软件实现,因此您不会享受硬件支持,如 AES-NI。您应该使用EVP_*函数。请参阅 OpenSSL wiki 上的 EVP Symmetric Encryption and Decryption。事实上,您可能应该使用经过身份验证的加密,因为它提供 机密性和真实性。请参阅 OpenSSL wiki 上的 EVP Authenticated Encryption and Decryption。 -
如果你使用
EVP_*函数,那么感兴趣的密码是EVP_aes_128_ctr、EVP_aes_192_ctr和EVP_aes_256_ctr。