Python 如何在C中使用openssl解密密文?
如何在python中解密使用AES加密的密文 Encrypt.py 使用这个,我使用AES生成密码文本,并将其与IV连接起来 并将其写入file.txt文件Python 如何在C中使用openssl解密密文?,python,c,encryption,cryptography,openssl,Python,C,Encryption,Cryptography,Openssl,如何在python中解密使用AES加密的密文 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()
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()
解密
现在我用它从file.txt中获取密码文本和IV。现在我该怎么做呢
使用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;
}
#包括
#包括
int main()
{
char filename[]=“file.txt”;
FILE*FILE=fopen(文件名,“r”);
char key[]=“mysecretpassword”;
如果(文件!=NULL){
字符行[1000];
char*p=行;
字符*数组=行;
while(fgets(行、行大小、文件)!=NULL){
fprintf(标准输出,“%s\n”,行);
char otherString[strlen(line)-15];
strncpy(其他字符串,p,strlen(行)-16);
otherString[strlen(otherString)-1]='\0';
printf(“%s\n”,otherString);//这里是加密字符串
数组=数组+(strlen(数组)-16);
printf(“%s\n”,数组);//我得到了IV
//这里介绍如何使用“IV”和“key”解密密文
}
fclose(文件);
}
否则{
perror(文件名);
}
返回0;
}
真的,我是个新手。请原谅我问题中的错误,请随时帮助我,那将是你最大的仁慈。先谢谢你
#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字符串长度的地方也有bug。可以更改while循环的这一部分以修复该问题:
int strLen = strlen(line) - 16;
char otherString[strLen + 1];
strncpy(otherString, p, strLen);
otherString[strLen] = '\0';
array = array + strLen;
这里还有一个很好的AES CBC加密/解密和