對於OpenSSL來說,我還有什麼新意,任何人都可以告訴我如何從C文件初始化AES CTR模式。我知道這是該方法的簽名,但我遇到了參數問題,沒有多少文檔,也沒有一個清晰的例子來說明如何進行簡單的加密。如果有人能夠舉例說明這種方法,我將不勝感激。提前致謝!AES CTR 256在OpenSSL上的加密操作模式
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);
嗨咖啡館我真的很感激你快速回答它已經真正有用的,並且defenetly最好的例子我已經在網上找到。我試圖打開一個長度不確定的文件,對它進行加密並用生成的密文寫入另一個文件,然後打開加密文件並恢復明文。我需要使用大量MB的文件,因爲我想基準測試CPU的性能。不過,Im在解密時仍然有問題。不知何故,當解密一個相當大的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;
}
每個加密和解密功能,被稱爲在不同的運行,從而一切都擁有相同的值總是初始化。再次感謝您提前給我提示的提示& Regards !!!
你的問題是你在每塊後重新初始化計數器。這是錯誤的 - 在加密和解密時,將'init_ctr()'調用移到'while()'循環之外。 'indata'和'outdata'也不一定是'AES_BLOCK_SIZE'的長度 - 它們可以大得多。 – caf 2010-08-06 00:24:55
你應該*不*使用'AES_encrypt'和朋友。這是一個純軟件實現,所以你不會喜歡硬件支持,比如AES-NI。您應該使用'EVP_ *'功能。請參閱OpenSSL wiki上的[EVP Symmetric Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption)。事實上,您應該使用經過身份驗證的加密,因爲它提供了*機密性和真實性。請參閱OpenSSL wiki上的[EVP Authenticated Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption)。 – jww 2016-05-28 13:15:46
如果使用'EVP_ *'函數,那麼感興趣的密碼是'EVP_aes_128_ctr','EVP_aes_192_ctr'和'EVP_aes_256_ctr'。 – jww 2016-05-28 13:27:08