2013-07-17 3 views
2

Как я могу расшифровать шифрованный текст, который зашифрован с использованием AES в python.Как расшифровать зашифрованный текст с помощью openssl в C?

Encrypt.py

С помощью этого я сделал шифрованный текст с помощью AES и сцепляются, что с IV и писал, что в в файл file.txt.

from Crypto.Cipher import AES 
import hashlib, os 
Plain = 'This is a string'    #String to Encrypt 
key = 'mysecretpassword'  
#key = hashlib.md5(key).digest()  #Key 
IV = 'InitializationVector' 
IV = hashlib.md5(IV).digest()   #Initialization Vector 
print len(IV) 
Obj1 = AES.new(key, AES.MODE_CBC, IV) 
Cipher = Obj1.encrypt(Plain)   #Cipher text 
File = open('file.txt','w') 
File.write(Cipher+IV)  #Concatenated the string and IV and 
          #wrote that to a file file.txt 
File.close() 

Decrypt.c

Используя это теперь я получил шифрованный текст и IV от file.txt. Теперь, как я могу расшифровать шифр с помощью openssl или любых других библиотек?

#include <stdio.h> 
#include <string.h> 
int main() 
{ 
    char filename[] = "file.txt"; 
    FILE *file = fopen (filename, "r"); 
    char key[] = "mysecretpassword"; 
    if (file != NULL) { 
    char line [1000]; 
    char *p = line; 
    char *array = line; 
    while(fgets(line,sizeof line,file)!= NULL) { 
     fprintf(stdout,"%s\n",line); 
     char otherString[strlen(line)-15]; 
     strncpy(otherString, p, strlen(line)-16); 
     otherString[strlen(otherString)-1] = '\0'; 
     printf("%s\n", otherString);//Here I got the Encrypted string 
     array=array+(strlen(array)-16); 
     printf("%s\n",array);//Here I got the IV 
     //Here how to decrypt the Cipher text using "IV" and "key" 
    } 
    fclose(file); 
    } 
    else { 
    perror(filename); 
    } 
    return 0; 
} 

Действительно я новичок. Пожалуйста, простите за ошибки в моем вопросе и, пожалуйста, не стесняйтесь помогать мне, это было бы вашей самой добротой. Заранее большое спасибо.

ответ

1
#include "openssl/aes.h" 

    char buffer[1000]; 
    AES_KEY dec_key; 
    AES_set_decrypt_key(key, 128, &dec_key); 
    AES_cbc_encrypt(otherString, buffer, strlen(line) - 16, 
        &dec_key, array, AES_DECRYPT); 
//AES_KEY dec_key; 
//AES_set_decrypt_key(key, keySize, &dec_key); 
//AES_cbc_encrypt(ciphertext, result, textLen, &dec_key, iv, AES_DECRYPT); 

Это работает для меня. 128 - бит для 16-байтового ключа.

Но я верю, что у вас также есть ошибка где-то в -1 -15 -16 длина строки. Эта часть в то время как цикл может быть изменен, чтобы решить эту проблему:

int strLen = strlen(line) - 16; 

    char otherString[strLen + 1]; 
    strncpy(otherString, p, strLen); 
    otherString[strLen] = '\0'; 

    array = array + strLen; 

Здесь также хорошо AES CBC шифрование/дешифрование example code и your working code.

Смежные вопросы