RSA 加密解密

 
  1. #include "randpool.h"

  2. #include "rsa.h"

  3. #include "hex.h"

  4. #include "files.h"

  5.  
  6.  
  7. using namespace std;

  8. using namespace CryptoPP;

  9. //#pragma comment(lib, "cryptlib.lib")

  10.  
  11. //------------------------

  12. // 函数声明

  13. //------------------------

  14. void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);

  15. string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);

  16. string RSADecryptString(const char *privFilename, const char *ciphertext);

  17. RandomPool & GlobalRNG();

  18.  
  19. //------------------------

  20. // 主程序

  21. //------------------------

  22. void main(){

  23. char priKey[128] = { 0 };

  24. char pubKey[128] = { 0 };

  25. char seed[1024] = { 0 };

  26.  
  27. // 生成 RSA 密钥对

  28. strcpy_s(priKey, "pri"); // 生成的私钥文件

  29. strcpy_s(pubKey, "pub"); // 生成的公钥文件

  30. strcpy_s(seed, "seed");

  31. cout << "priKey: " << priKey << endl;

  32. cout << "pubKey: " << pubKey << endl;

  33. GenerateRSAKey(1024, priKey, pubKey, seed);

  34.  
  35. // RSA 加解密

  36. char message[1024] = { 0 };

  37. cout << "Origin Text:\t" << "Hello World!" << endl << endl;

  38. strcpy_s(message, "Hello World!");

  39. string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密

  40. cout << "Encrypted Text:\t" << encryptedText << endl << endl;

  41. string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA 解密

  42. cout << "Decrypted Text:\t" << decryptedText << endl << endl;

  43. system("pause");

  44. }

  45.  
  46. //------------------------

  47. // 生成RSA密钥对

  48. //------------------------

  49. void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed){

  50. RandomPool randPool;

  51. randPool.IncorporateEntropy((byte *)seed, strlen(seed));

  52.  
  53. RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);

  54. HexEncoder privFile(new FileSink(privFilename));

  55. priv.AccessMaterial().Save(privFile);

  56. privFile.MessageEnd();

  57.  
  58. RSAES_OAEP_SHA_Encryptor pub(priv);

  59. HexEncoder pubFile(new FileSink(pubFilename));

  60. pub.AccessMaterial().Save(pubFile);

  61. pubFile.MessageEnd();

  62. }

  63.  
  64. //------------------------

  65. // RSA加密

  66. //------------------------

  67. string RSAEncryptString(const char *pubFilename, const char *seed, const char *message){

  68.  
  69. FileSource pubFile(pubFilename, true, new HexDecoder);

  70. RSAES_OAEP_SHA_Encryptor pub(pubFile);

  71.  
  72. RandomPool randPool;

  73. randPool.IncorporateEntropy((byte *)seed, strlen(seed));

  74.  
  75. std::string result;

  76. StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));

  77. return result;

  78. }

  79.  
  80. //------------------------

  81. // RSA解密

  82. //------------------------

  83. string RSADecryptString(const char *privFilename, const char *ciphertext){

  84. FileSource privFile(privFilename, true, new HexDecoder);

  85. RSAES_OAEP_SHA_Decryptor priv(privFile);

  86.  
  87. std::string result;

  88. StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));

  89. return result;

  90. }

  91.  
  92. //------------------------

  93. // 定义全局的随机数池

  94. //------------------------

  95. RandomPool & GlobalRNG(){

  96. static RandomPool randomPool;

  97. return randomPool;

  98. }

http://blog.sina.com.cn/s/blog_607787d30102x91p.html

 

https://blog.csdn.net/phker/article/details/5056288

还可以参考这里

 

转载地址

https://blog.csdn.net/Oyasimi1412/article/details/82657151

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值