2011-01-04 4 views
0

Est-ce que quelqu'un sait quels sont les paramètres pour le sel, nrounds pour le cryptage/décryptage dans .net C#? J'ai besoin de ces informations pour openssl. Je suis en train de créer un aes_128 avec le cryptage/décryptage cbc. Pour créer la clé, je dois aussi connaître ces vaslues: nrounds et salt. Thxaes_cbc_128 crypter/décrypter dans C# visual studio

+0

Je pense que vous avez défini la IV dans le contexte Crypto. Le nombre de tours n'est-il pas spécifié par taille de clé? – Rup

+0

c'est pourquoi je demande .. je ne sais pas exactement. Comme j'ai compris les rounds, le salt, les key_data créent une clé spécifique utilisée au chiffrement. Voici comment les choses fonctionnent dans openssl Linux: d. Je voudrais comparer le résultat de mu avec ceux sur .net. Et je voudrais savoir quoi mettre à la valeur de nrounds, au sel afin de créer la bonne description. – elisa

+0

Mes résultats sous linux: Ubuntu ne correspond pas aux résultats sur .net. Pourquoi? Parce que je suppose que je n'ai pas réglé correctement les tours. Aidez-moi. Avez-vous une idée de cryptage et de décryptage dans linux en utilisant aes 128 et cbc et la clé = "test" sans donner nécessairement les nrounds et salt ... afin de créer le vecteur de clé char [] utilisé au cryptage? L'IV est 0. Thx – elisa

Répondre

0

ici est le code pour aes 128 octets avec le cryptage et 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

comparé avec la resut dans .net - un cryptage simple utilisant même aes 128, cbc mode de broyeur, padk padding, la même clé = "bonjour" et le même iv mis à 0 le résultat est différent. POURQUOI?! Comme je le présente le problème est la valeur du sel et nrounds qui dans mon code sont fixés par le développeur. – she