aes_cbc_128在c#visual studio中加密/解密

时间:2011-01-04 13:19:08

标签: .net encryption

有谁知道在.net c#中加密/解密的salt,nrounds的设置是什么?我需要这些信息用于openssl。我想用cbc加密/解密创建一个aes_128。为了创造钥匙我也必须知道这些血管:nrounds和salt。 THX

1 个答案:

答案 0 :(得分:0)

这里是使用cbc和pkcs7

进行128字节加密的代码
#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;
}