当前位置:   article > 正文

C++ 使用OpenSSL_c++ openssl

c++ openssl

最近在研究C++使用OpenSSL,前面的文章介绍了编译OpenSSL源码的过程,这里随便写下加密解密的用法。

MD5:

  1. #include <openssl\md5.h>
  2. #include <iostream>
  3. using namespace std;
  4. int md5_encrypt(const void* data, size_t len, unsigned char* md5)
  5. {
  6. if (data == NULL || len <= 0 || md5 == NULL) {
  7. printf("Input param invalid!\n");
  8. return -1;
  9. }
  10. MD5_CTX ctx;
  11. MD5_Init(&ctx);
  12. MD5_Update(&ctx, data, len);
  13. MD5_Final(md5, &ctx);
  14. return 0;
  15. }

AES:

  1. #include <openssl\aes.h>
  2. int aes_encrypt(const unsigned char* data, const size_t len, const unsigned char* key, unsigned char* ivec, unsigned char* encrypt_data)
  3. {
  4. AES_KEY aes_key;
  5. memset(&aes_key, 0x00, sizeof(AES_KEY));
  6. if (AES_set_encrypt_key(key, 128, &aes_key) < 0)
  7. {
  8. fprintf(stderr, "Unable to set encryption key in AES...\n");
  9. return -1;
  10. }
  11. AES_cbc_encrypt(data, encrypt_data, len, &aes_key, ivec, AES_ENCRYPT);
  12. return 0;
  13. }
  14. int aes_decrypt(const unsigned char* encrypt_data, const size_t len, const unsigned char* key, unsigned char* ivec, unsigned char* decrypt_data)
  15. {
  16. AES_KEY aes_key;
  17. memset(&aes_key, 0x00, sizeof(AES_KEY));
  18. if (AES_set_decrypt_key(key, 128, &aes_key) < 0)
  19. {
  20. fprintf(stderr, "Unable to set decryption key in AES...\n");
  21. return -1;
  22. }
  23. AES_cbc_encrypt(encrypt_data, decrypt_data, len, &aes_key, ivec, AES_DECRYPT);
  24. return 0;
  25. }

EVP:

  1. #include <openssl\evp.h>
  2. int evp_aes_cbc_encrypt(const unsigned char* data, const int len, const unsigned char* key, unsigned char* ivec, unsigned char* encrypt_data, int& encrypt_len)
  3. {
  4. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  5. ASSERT(ctx);
  6. int out_len;
  7. int ret = EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, ivec);
  8. if (ret != 1)
  9. {
  10. return -1;
  11. }
  12. EVP_CIPHER_CTX_set_key_length(ctx, EVP_MAX_KEY_LENGTH);
  13. EVP_CIPHER_CTX_set_padding(ctx, 5);
  14. ret = EVP_EncryptUpdate(ctx, encrypt_data, &out_len, data, len);
  15. if (ret != 1)
  16. {
  17. return -1;
  18. }
  19. encrypt_len = out_len;
  20. ret = EVP_EncryptFinal_ex(ctx, encrypt_data + encrypt_len, &out_len);
  21. if (ret != 1)
  22. {
  23. return -1;
  24. }
  25. encrypt_len += out_len;
  26. EVP_CIPHER_CTX_free(ctx);
  27. return 0;
  28. }
  29. int evp_aes_cbc_decrypt(const unsigned char* encrypt_data, const int len, const unsigned char* key, unsigned char* ivec, unsigned char* decrypt_data, int& decrypt_len)
  30. {
  31. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  32. ASSERT(ctx);
  33. int out_len;
  34. int ret = EVP_DecryptInit(ctx, EVP_aes_128_cbc(), key, ivec);
  35. if (ret != 1)
  36. {
  37. return -1;
  38. }
  39. EVP_CIPHER_CTX_set_key_length(ctx, EVP_MAX_KEY_LENGTH);
  40. ret = EVP_DecryptUpdate(ctx, decrypt_data, &out_len, encrypt_data, len);
  41. if (ret != 1)
  42. {
  43. return -1;
  44. }
  45. decrypt_len = out_len;
  46. ret = EVP_DecryptFinal_ex(ctx, decrypt_data + decrypt_len, &out_len);
  47. if (ret != 1)
  48. {
  49. return -1;
  50. }
  51. decrypt_len += out_len;
  52. EVP_CIPHER_CTX_free(ctx);
  53. return 0;
  54. }

 

RSA:

  1. #include <openssl\rsa.h>
  2. #include <openssl\pem.h>
  3. #include <openssl\rand.h>
  4. #include <iostream>
  5. #define PUBLIC_KEY_FILE "rsapub.key"
  6. #define PRIVATE_KEY_FILE "rsapriv.key"
  7. #define RSA_KEY_LENGTH 1024
  8. #define RSA_PRIKEY_PSW "123"
  9. int rsa_test(const unsigned char* data, size_t& len, char* encrypt_data, size_t& encrypt_data_len, char* decrypt_data, size_t& decrypt_data_len)
  10. {
  11. generate_key_files(PUBLIC_KEY_FILE, PRIVATE_KEY_FILE,(const unsigned char *)RSA_PRIKEY_PSW, strlen(RSA_PRIKEY_PSW));
  12. EVP_PKEY *pub_key = open_public_key(PUBLIC_KEY_FILE);
  13. EVP_PKEY *pri_key = open_private_key(PRIVATE_KEY_FILE, (const unsigned char *)RSA_PRIKEY_PSW);
  14. int ret = rsa_key_encrypt(pub_key, data, len, (unsigned char *)encrypt_data, encrypt_data_len);
  15. if (ret != 0)
  16. {
  17. printf("rsa encrypt failed!\n");
  18. return ret;
  19. }
  20. else
  21. {
  22. printf("rsa encrypt success!\n");
  23. }
  24. ret = rsa_key_decrypt(pri_key, (const unsigned char *)encrypt_data, encrypt_data_len, (unsigned char *)decrypt_data, decrypt_data_len);
  25. if (ret != 0)
  26. {
  27. printf("rsa decrypt failed!\n");
  28. }
  29. else
  30. {
  31. printf("rsa decrypt success!\n");
  32. printf("encrypt data:%s\n", encrypt_data);
  33. printf("decrypt data:%s\n", decrypt_data);
  34. }
  35. return ret;
  36. }
  37. int generate_key_files(const char* pub_key_file, const char* priv_key_file, const unsigned char* passwd, int passwd_len)
  38. {
  39. RSA *rsa = NULL;
  40. rsa = RSA_generate_key(RSA_KEY_LENGTH, RSA_F4, NULL, NULL);
  41. if (rsa == NULL)
  42. {
  43. printf("RSA_generate_key error!\n");
  44. return -1;
  45. }
  46. //生成公钥文件
  47. BIO* bio_pub = BIO_new(BIO_s_file());
  48. if (NULL == bio_pub)
  49. {
  50. printf("generate_key bio file new error!\n");
  51. return -1;
  52. }
  53. if (BIO_write_filename(bio_pub, (void *)pub_key_file) <= 0)
  54. {
  55. printf("BIO_write_filename error!\n");
  56. return -1;
  57. }
  58. if (PEM_write_bio_RSAPublicKey(bio_pub, rsa) != 1)
  59. {
  60. printf("PEM_write_bio_RSAPublicKey error!\n");
  61. return -1;
  62. }
  63. printf("Create public key ok!\n");
  64. BIO_free_all(bio_pub);
  65. // 生成私钥文件
  66. BIO* bio_priv = BIO_new_file(priv_key_file, "w+");
  67. if (NULL == bio_priv)
  68. {
  69. printf("generate_key bio file new error2!\n");
  70. return -1;
  71. }
  72. if (PEM_write_bio_RSAPrivateKey(bio_priv, rsa, EVP_des_ede3_ofb(), (unsigned char *)passwd, passwd_len, NULL, NULL) != 1)
  73. {
  74. printf("PEM_write_bio_RSAPublicKey error!\n");
  75. return -1;
  76. }
  77. printf("Create private key ok!\n");
  78. BIO_free_all(bio_priv);
  79. RSA_free(rsa);
  80. return 0;
  81. }
  82. EVP_PKEY* open_public_key(const char* pub_key_file)
  83. {
  84. EVP_PKEY* key = NULL;
  85. RSA *rsa = NULL;
  86. OpenSSL_add_all_algorithms();
  87. BIO *bio_pub = BIO_new(BIO_s_file());;
  88. BIO_read_filename(bio_pub, pub_key_file);
  89. if (NULL == bio_pub)
  90. {
  91. printf("open_public_key bio file new error!\n");
  92. return NULL;
  93. }
  94. rsa = PEM_read_bio_RSAPublicKey(bio_pub, NULL, NULL, NULL);
  95. if (rsa == NULL)
  96. {
  97. printf("open_public_key failed to PEM_read_bio_RSAPublicKey!\n");
  98. BIO_free(bio_pub);
  99. RSA_free(rsa);
  100. return NULL;
  101. }
  102. printf("open_public_key success to PEM_read_bio_RSAPublicKey!\n");
  103. key = EVP_PKEY_new();
  104. if (NULL == key)
  105. {
  106. printf("open_public_key EVP_PKEY_new failed\n");
  107. RSA_free(rsa);
  108. return NULL;
  109. }
  110. EVP_PKEY_assign_RSA(key, rsa);
  111. return key;
  112. }
  113. EVP_PKEY* open_private_key(const char* priv_key_file, const unsigned char *passwd)
  114. {
  115. EVP_PKEY* key = NULL;
  116. RSA *rsa = RSA_new();
  117. OpenSSL_add_all_algorithms();
  118. BIO* bio_priv = NULL;
  119. bio_priv = BIO_new_file(priv_key_file, "rb");
  120. if (NULL == bio_priv)
  121. {
  122. printf("open_private_key bio file new error!\n");
  123. return NULL;
  124. }
  125. rsa = PEM_read_bio_RSAPrivateKey(bio_priv, &rsa, NULL, (void *)passwd);
  126. if (rsa == NULL)
  127. {
  128. printf("open_private_key failed to PEM_read_bio_RSAPrivateKey!\n");
  129. BIO_free(bio_priv);
  130. RSA_free(rsa);
  131. return NULL;
  132. }
  133. printf("open_private_key success to PEM_read_bio_RSAPrivateKey!\n");
  134. key = EVP_PKEY_new();
  135. if (NULL == key)
  136. {
  137. printf("open_private_key EVP_PKEY_new failed\n");
  138. RSA_free(rsa);
  139. return NULL;
  140. }
  141. EVP_PKEY_assign_RSA(key, rsa);
  142. return key;
  143. }
  144. int rsa_key_encrypt(EVP_PKEY *key, const unsigned char* data, size_t len, unsigned char *encrypt_data, size_t &encrypt_data_len)
  145. {
  146. EVP_PKEY_CTX *ctx = NULL;
  147. OpenSSL_add_all_ciphers();
  148. ctx = EVP_PKEY_CTX_new(key, NULL);
  149. if (NULL == ctx)
  150. {
  151. printf("ras_pubkey_encryptfailed to open ctx.\n");
  152. EVP_PKEY_free(key);
  153. return -1;
  154. }
  155. if (EVP_PKEY_encrypt_init(ctx) <= 0)
  156. {
  157. printf("ras_pubkey_encryptfailed to EVP_PKEY_encrypt_init.\n");
  158. EVP_PKEY_free(key);
  159. return -1;
  160. }
  161. if (EVP_PKEY_encrypt(ctx, encrypt_data, &encrypt_data_len, data, len) <= 0)
  162. {
  163. printf("ras_pubkey_encryptfailed to EVP_PKEY_encrypt.\n");
  164. EVP_PKEY_CTX_free(ctx);
  165. EVP_PKEY_free(key);
  166. return -1;
  167. }
  168. EVP_PKEY_CTX_free(ctx);
  169. EVP_PKEY_free(key);
  170. return 0;
  171. }
  172. int rsa_key_decrypt(EVP_PKEY *key, const unsigned char *encrypt_data, size_t encrypt_data_len, unsigned char *decrypt_data, size_t &decrypt_data_len)
  173. {
  174. EVP_PKEY_CTX *ctx = NULL;
  175. OpenSSL_add_all_ciphers();
  176. ctx = EVP_PKEY_CTX_new(key, NULL);
  177. if (NULL == ctx)
  178. {
  179. printf("ras_prikey_decryptfailed to open ctx.\n");
  180. EVP_PKEY_free(key);
  181. return -1;
  182. }
  183. if (EVP_PKEY_decrypt_init(ctx) <= 0)
  184. {
  185. printf("ras_prikey_decryptfailed to EVP_PKEY_decrypt_init.\n");
  186. EVP_PKEY_free(key);
  187. return -1;
  188. }
  189. if (EVP_PKEY_decrypt(ctx, decrypt_data, &decrypt_data_len, encrypt_data, encrypt_data_len) <= 0)
  190. {
  191. printf("ras_prikey_decryptfailed to EVP_PKEY_decrypt.\n");
  192. EVP_PKEY_CTX_free(ctx);
  193. EVP_PKEY_free(key);
  194. return -1;
  195. }
  196. EVP_PKEY_CTX_free(ctx);
  197. EVP_PKEY_free(key);
  198. return 0;
  199. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/990965
推荐阅读
相关标签
  

闽ICP备14008679号