爲什麼我在使用openssl aes命令工具和openssl AES apis時會得到不同的密文?當我使用OpenSSL AES命令行工具和OpenSSL AES API時,會出現不同的密文?
我已經使用三種類型的加密:在javax.cryto
- A型)的OpenSSL命令行工具
- B型)類
- C型)的OpenSSL C API。
使用類型(a)和(b),我得到了相同的密文。但是當使用(c)時我得到了不同的密文。
我想在使用方法c和方法a/b時得到相同的密文。 我認爲c類型有問題,但找不到它。請注意,我在上述三種方法中使用了相同的KEY,IV對。
鍵入:
openssl enc -aes-128-cbc -e -a -in pt.txt -out ct.txt -K 01010101010101010101010101010101 -iv 01010101010101010101010101010101 -p
b型:
Java代碼使用javax.crypto中。我不會粘貼代碼,因爲這樣我得到了與Type a相同的密文。
鍵入c: C代碼使用OpenSSL API:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/aes.h>
int main(int argc, char** argv) {
AES_KEY aes;
unsigned char key[AES_BLOCK_SIZE]; // AES_BLOCK_SIZE = 16
unsigned char iv[AES_BLOCK_SIZE]; // init vector
unsigned char* input_string;
unsigned char* encrypt_string;
unsigned char* decrypt_string;
unsigned int len; // encrypt length (in multiple of AES_BLOCK_SIZE)
unsigned int i;
// check usage
if (argc != 2) {
fprintf(stderr, "%s <plain text>\n", argv[0]);
exit(-1);
}
// set the encryption length
len = 0;
if (strlen(argv[1])>=AES_BLOCK_SIZE ||
(strlen(argv[1]) + 1) % AES_BLOCK_SIZE == 0) {
len = strlen(argv[1]) + 1;
} else {
len = ((strlen(argv[1]) + 1)/AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
}
// set the input string
input_string = (unsigned char*)calloc(len, sizeof(unsigned char));
if (input_string == NULL) {
fprintf(stderr, "Unable to allocate memory for input_string\n");
exit(-1);
}
strncpy((char*)input_string, argv[1], strlen(argv[1]));
// Generate AES 128-bit key
memset(key, 0x01, AES_BLOCK_SIZE);
// Set encryption key
memset(iv, 0x01, AES_BLOCK_SIZE);
if (AES_set_encrypt_key(key, 128, &aes) < 0) {
fprintf(stderr, "Unable to set encryption key in AES\n");
exit(-1);
}
// alloc encrypt_string
encrypt_string = (unsigned char*)calloc(len, sizeof(unsigned char));
if (encrypt_string == NULL) {
fprintf(stderr, "Unable to allocate memory for encrypt_string\n");
exit(-1);
}
// encrypt (iv will change)
AES_cbc_encrypt(input_string, encrypt_string, len, &aes, iv, AES_ENCRYPT);
/////////////////////////////////////
// alloc decrypt_string
decrypt_string = (unsigned char*)calloc(len, sizeof(unsigned char));
if (decrypt_string == NULL) {
fprintf(stderr, "Unable to allocate memory for decrypt_string\n");
exit(-1);
}
// Set decryption key
memset(iv, 0x01, AES_BLOCK_SIZE);
if (AES_set_decrypt_key(key, 128, &aes) < 0) {
fprintf(stderr, "Unable to set decryption key in AES\n");
exit(-1);
}
// decrypt
AES_cbc_encrypt(encrypt_string, decrypt_string, len, &aes, iv,
AES_DECRYPT);
// print
printf("input_string =%s\n", input_string);
printf("encrypted string =");
for (i=0; i<len; ++i) {
printf("%u ", encrypt_string[i]);
}
printf("\n");
printf("decrypted string =%s\n", decrypt_string);
return 0;
}
有什麼能爲不同的輸出的原因嗎?
什麼是輸入和不同的輸出?它們是完全不同的,還是隻在最後一塊? – 2012-02-09 23:11:53
我在這裏無法過去我的結果,bcz這個網站抱怨這種格式!所以我把結果通過另一個網站,請參閱http://topic.csdn.net/u/20120207/16/48183c46-8afd-47d9-a260-7a5eb571f877.html?seed=661539550&r=77510138#r_77510138中文deveplop bbs ,請參閱分組四。謝謝! – user1194299 2012-02-10 05:40:17