誰でも塩の設定を知っていますか?ネットC#の暗号化/復号化のnroundsですか?私はopensslのためにこれらの情報が必要です。私はcbcの暗号化/復号化を使ってaes_128を作成しようとしています。キーを作成するには、これらのvasluesも知っている必要があります:nroundsと塩。 THXaes_cbc_128 C#ビジュアルスタジオの暗号化/復号化
0
A
答えて
0
ここでは、私はあなたが暗号コンテキストでIVを設定すると思うCBCとPKCS7
#include <iostream>
#include <sstream>
#include <cstring>
#include <vector>
#include <stdlib.h>
#include <openssl/evp.h>
#define AES_BLOCK_SIZE 128 //256
#include "base64.h"
#include "base64.cpp"
using namespace std;
int aes_init(unsigned char *key_data, int key_data_len, unsigned char *salt, EVP_CIPHER_CTX *e_ctx,
EVP_CIPHER_CTX *d_ctx)
{
int i, nrounds = 6;
unsigned char key[16], iv[16];
/*
* Gen key & IV for AES 256 CBC mode. A SHA1 digest is used to hash the supplied key material.
* nrounds is the number of times the we hash the material. More rounds are more secure but
* slower.
*/
i = EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(), salt, key_data, key_data_len, nrounds, key, iv);
if (i != 16) {
std::cout<<"Key size is: - should be 256 bits\n "<<i<<std::endl;
return -1;
}
for(int x = 0; x<16; ++x)
{
std::cout<<"Key: and iv: \n"<< key[x] <<std::endl;
iv[x]=0;
}
for(int x = 0; x<8; ++x)
{std::cout<<"salt: \n"<< salt[x]<<std::endl;
salt[x]=0; }
EVP_CIPHER_CTX_init(e_ctx);
EVP_CIPHER_CTX_set_padding(e_ctx,7);
EVP_EncryptInit_ex(e_ctx, EVP_aes_128_cbc(), NULL, key, iv);
EVP_CIPHER_CTX_init(d_ctx);
EVP_DecryptInit_ex(d_ctx, EVP_aes_128_cbc(), NULL, key, iv);
return 0;
}
/*
* Encrypt *len bytes of data
* All data going in & out is considered binary (unsigned char[])
*/
unsigned char *aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len)
{
if (EVP_CIPHER_CTX_set_padding(e,7)!= 1) std::cout<<"no padding?"<<std::endl;
/* max ciphertext len for a n bytes of plaintext is n + AES_BLOCK_SIZE -1 bytes */
int c_len = *len + AES_BLOCK_SIZE - 1, f_len = 0;
unsigned char *ciphertext = (unsigned char *)malloc(c_len);
/* allows reusing of 'e' for multiple encryption cycles */
if(!EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL)){
std::cout<<"ERROR in EVP_EncryptInit_ex \n"<<std::endl;
return NULL;
}
/* update ciphertext, c_len is filled with the length of ciphertext generated,
*len is the size of plaintext in bytes */
if(!EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len)){
std::cout<<"ERROR in EVP_EncryptUpdate \n"<<std::endl;
return NULL;
}
/* update ciphertext with the final remaining bytes */
if(!EVP_EncryptFinal_ex(e, ciphertext+c_len, &f_len)){
std::cout<<"ERROR in EVP_EncryptFinal_ex \n"<<std::endl;
return NULL;
}
*len = c_len + f_len;
return ciphertext;
}
/*
* Decrypt *len bytes of ciphertext
*/
unsigned char *aes_decrypt(EVP_CIPHER_CTX *e, unsigned char *ciphertext, int *len)
{
/* plaintext will always be equal to or lesser than length of ciphertext*/
int p_len = *len, f_len = 0;
unsigned char *plaintext = (unsigned char *)malloc(p_len);
if(!EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL)){
std::cout<<"ERROR in EVP_DecryptInit_ex \n"<<std::endl;
return NULL;
}
if(!EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len)){
std::cout<<"ERROR in EVP_DecryptUpdate\n"<<std::endl;
return NULL;
}
if(!EVP_DecryptFinal_ex(e, plaintext+p_len, &f_len)){
std::cout<<"ERROR in EVP_DecryptFinal_ex\n"<<std::endl;
return NULL;
}
*len = p_len + f_len;
return plaintext;
}
int main(int argc, char **argv)
{
/* "opaque" encryption, decryption ctx structures that libcrypto uses to record
status of enc/dec operations */
EVP_CIPHER_CTX en, de;
/* The salt paramter is used as a salt in the derivation: it should point to an 8 byte buffer or NULL if no salt is used. */
//unsigned char salt[] = {1,2,3,4,5,6,7,8};
unsigned char salt[]={0,0,0,0,0,0,0,0};
unsigned char *key_data;
int key_data_len, i;
char *input[] = {"convert this string?",
NULL};
/* the key_data is read from the argument list */
//key_data = (unsigned char *)argv[1];
//key_data_len = strlen(argv[1]);
key_data = (unsigned char *) ("hello");
key_data_len = strlen("hello");
/* gen key and iv. init the cipher ctx object */
if (aes_init(key_data, key_data_len, salt, &en, &de)) {
std::cout<<"Couldn't initialize AES cipher\n"<<std::endl;
return -1;
}
/* encrypt and decrypt each input string and compare with the original */
for (i = 0; input[i]; i++) {
char *plaintext;
unsigned char *ciphertext;
int olen, len;
/* The enc/dec functions deal with binary data and not C strings. strlen() will
return length of the string without counting the '\0' string marker. We always
pass in the marker byte to the encrypt/decrypt functions so that after decryption
we end up with a legal C string */
olen = len = strlen(input[i])+1;
ciphertext = aes_encrypt(&en, (unsigned char *)input[i], &len);
// plaintext = (char *)aes_decrypt(&de, ciphertext, &len);
/**/
std::cout<<"OK: enc ok for: "<< ciphertext<<std::endl;
std::string encoded_base = base64_encode(ciphertext,strlen((const char*)ciphertext));
std::cout<<"the encr with base64: "<< encoded_base<<std::endl;
/* DECODING */
std::string decode = base64_decode(encoded_base);
std::cout <<"the decode woth base64: "<<decode<<std::endl;
unsigned char *ciphertext1= (unsigned char *)(decode.c_str());
std::cout<<"chipertext1: "<<ciphertext1<<std::endl;
//reinterpret_cast< const unsigned char*>*/
char *plaintext1 = (char *)aes_decrypt(&de, ciphertext1, &len);
if (strncmp(plaintext1, input[i], olen))
std::cout<<"FAIL: enc/dec failed for: "<< input[i]<<std::endl;
else
{
// std::cout<<"OK: dec ok for: "<< plaintext<<std::endl; // \"%s\"\n
std::cout<<"the decr with base64: "<<plaintext1<<std::endl;
}
free(ciphertext);
free(plaintext1);
}
EVP_CIPHER_CTX_cleanup(&de);
EVP_CIPHER_CTX_cleanup(&en);
return 0;
}
+0
と比較すると、同じエース128、チッパーモードcbc、パディングpkcs、同じキー= "hello"、同じivを0とした単純な暗号化は結果が異なります。なぜ?!私はこの問題は塩価と私のコードでは、開発者によって設定されてnroundsです。 – she
関連する問題
- 1. C++の暗号化と復号化
- 2. C++暗号化と復号化のソースコード
- 3. C#/ Java | AES256暗号化/復号化
- 4. シーザー暗号化と復号化C++
- 5. 暗号化C#復号化AES CBC 256
- 6. RSA暗号化/復号化
- 7. 暗号化と復号化
- 8. AES暗号化/復号化
- 9. 暗号化/復号化soll
- 10. 復号化、暗号化
- 11. 暗号ブロックチェーンの復号XOR「暗号化」
- 12. カスタム暗号化暗号化/復号化ストリーム
- 13. perlの暗号化、delphi7の復号化
- 14. AES256暗号化/復号化の速度
- 15. SWIFTの暗号化と復号化
- 16. 別ファイルのXML暗号化/復号化
- 17. Muleコミュニティ版の暗号化/復号化
- 18. RSA暗号化/復号化の実装
- 19. ファイルの暗号化と復号化
- 20. パスワードの暗号化/復号化スプリングセキュリティ
- 21. ESP8266 AESの暗号化 - 復号化
- 22. DataTableオブジェクトの暗号化と復号化
- 23. ASP.net ViewStateの暗号化と復号化
- 24. ESAPIの暗号化と復号化
- 25. Md5ハッシュの暗号化と復号化
- 26. Java暗号化(PBEWithMD5AndDES)の復号化(PBEWithMD5AndDES)
- 27. ファイルの復号化と暗号化
- 28. SQL Serverの暗号化と復号化
- 29. DES、RSAの暗号化と復号化
- 30. Yii URLの暗号化/復号化
とAES 128バイトの暗号化のためのコードです。鍵サイズで指定されたラウンドの数ではありませんか? – Rup
だからこそ私は尋ねている。正確には分からない。ラウンド、塩を理解しているように、key_dataは暗号化に使用される特定のキーを作成します。これは、openssl Linuxでの動作です:d。私はmuの結果を.netのものと比較したいと思います。そして、正しいencriptionを作成するために、塩でnrounds値を設定する方法を知りたいと思います。 – elisa
linuxの私の結果:Ubuntuは.netの結果には対応していません。どうして?私は私が正しくラウンドを設定していないと思うので。助けて。暗号化に使用するchar []キーベクトルを作成するために、aes 128とcbcとkey = "test"を使ってlinuxで暗号化と復号化を作成するという理想がありますか? IVは0です。Thx – elisa