ファイル(jsonOut.crypto)の暗号化にopensslライブラリをうまく使用しました。しかし、私は正常にその暗号化されたファイルをchar配列に読み込み、以下のC++コードで解読することができません。OpenSSLとJSONファイルの暗号化と復号化
ファイルの内容を文字列(char配列)に読み込み、16バイトのバッファを解読しながら16ビットを配列に挿入し、その配列をファイルオブジェクトに挿入しながらその配列を反復処理します。
ただし、復号化されたファイルは不器用なようです。暗号化されたファイルとは異なりますが、それでも暗号化されていないようです。誰かが明白なエラーのために自分のコードを調べることができますか?
別のライブラリを提案しないでください。これは最高水準の暗号を可能にするためのプロダクションコードではなく、何かを素早く組み合わせるだけです。
ありがとうございます!これはあなたの暗号化されたファイルであると推定される
stringstream encryptedStreamStr;
encryptedStreamStr << inEncryptedFile.rdbuf();
:
int main()
{
// read json file into memory
ifstream inFile;
inFile.open("file.json");
stringstream strStream;
strStream << inFile.rdbuf();
string str = strStream.str();
// create buffer and buffer input/output
int len = strlen(str.c_str());
unsigned char encryptedbuffer[128];
// CODE FOR ENCRYPTION
unsigned char oneKey[] = "abcdefghijklmnop";
AES_KEY key;
// encryption - could make into function?
AES_set_encrypt_key(oneKey,128,&key);
ofstream outFile;
outFile.open("jsonOut.crypto");
for(int i = 0; i <= len; i += 16) {
AES_encrypt((const unsigned char *) str.c_str() + i,encryptedbuffer,&key);
for (int k = 0; k <= 16; k++) {
outFile << encryptedbuffer[k];
}
}
outFile.close();
inFile.close();
// read encrypted file into memory
ifstream inEncryptedFile;
inEncryptedFile.open("jsonOut.crypto");
stringstream encryptedStreamStr;
encryptedStreamStr << inEncryptedFile.rdbuf();
string encryptedStr = encryptedStreamStr.str();
// create buffer and buffer input/output
int lenDecrypt = strlen(encryptedStr.c_str());
unsigned char outbufferDecrypt[lenDecrypt];
AES_set_decrypt_key(oneKey,128,&key);
ofstream outFileDecrypt;
outFileDecrypt.open("jsonOut.decrypt");
for(int j = 0; j <= lenDecrypt; j += 16){
AES_decrypt((const unsigned char *) encryptedStr.c_str() + j,outbufferDecrypt,&key);
for (int k = 0; k <= 16; k++) {
outFileDecrypt << outbufferDecrypt[k];
}
}
outFileDecrypt.close();
inEncryptedFile.close();
return 0;
}
[openssl json aes decryption](https://www.google.com/search?q=openssl+json+aes+decryption+site%3Astackoverflow.com)を見てください。また、 'AES_encrypt'や友達を使うべきではありません。これはソフトウェアのみの実装なので、AES-NIなどのハードウェアサポートは受けられません。あなたは 'EVP_ *'関数を使っていなければなりません。 OpenSSL wikiの[EVP Symmetric Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption)を参照してください。 – jww
これはBIOとEVP_xxxx対称暗号を使って簡単に設定できます(例えば、 'EVP_aes_128_cbc()')。 C++のishメカニズムではありませんが、libcryptoもありません。 – WhozCraig