zoukankan      html  css  js  c++  java
  • rsa

    #include "randpool.h" 
    #include "rsa.h" 
    #include "hex.h" 
    #include "files.h" 
    #include <iostream> 
      
    using namespace std; 
    using namespace CryptoPP; 
      
    #pragma comment(lib, "cryptlib.lib") 
      
    //------------------------ 
    // 函数声明 
    //------------------------ 
    void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed); 
    string RSAEncryptString(const char *pubFilename, const char *seed, const char *message); 
    string RSADecryptString(const char *privFilename, const char *ciphertext); 
    RandomPool & GlobalRNG(); 
      
    //------------------------ 
    // 主程序 
    //------------------------ 
    void main() 
    { 
        char priKey[128] = {0}; 
        char pubKey[128] = {0}; 
        char seed[1024] = {0}; 
      
        // 生成 RSA 密钥对 
        strcpy_s(priKey,sizeof(priKey) / sizeof(priKey[0]), "pri"); // 生成的私钥文件 
        strcpy_s(pubKey,sizeof(pubKey) / sizeof(pubKey[0]),"pub"); // 生成的公钥文件 
        strcpy_s(seed,sizeof(seed) / sizeof(seed[0]),"seed"); 
        GenerateRSAKey(1024, priKey, pubKey, seed); 
      
        // RSA 加解密 
        char message[1024] = {0}; 
        cout<<"Origin Text:t"<<"HelloWorld!"<<endl<<endl; 
        strcpy_s(message,sizeof(message) / sizeof(message[0]),"Hello World!"); 
        string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密 [Page]
        cout<<"Encrypted Text:t"<<encryptedText<<endl<<endl; 
        string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA 解密 
        cout<<"Decrypted Text:t"<<decryptedText<<endl<<endl; 
    } 
      
    //------------------------ 
    // 生成RSA密钥对 
    //------------------------ 
    void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed) 
    { 
           RandomPool randPool; 
           randPool.Put((byte *)seed, strlen(seed)); 
      
           RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength); 
           HexEncoder privFile(new FileSink(privFilename)); 
           priv.DEREncode(privFile); 
           privFile.MessageEnd(); 
      
           RSAES_OAEP_SHA_Encryptor pub(priv); 
           HexEncoder pubFile(new FileSink(pubFilename)); 
           pub.DEREncode(pubFile); 
           pubFile.MessageEnd(); 
    } 
      
    //------------------------ 
    // RSA加密 
    //------------------------ 
    string RSAEncryptString(const char *pubFilename, const char *seed, const char *message) 
    { 
           FileSource pubFile(pubFilename, true, new HexDecoder); 
           RSAES_OAEP_SHA_Encryptor pub(pubFile); 
      
           RandomPool randPool; 
           randPool.Put((byte *)seed, strlen(seed)); 
      
           string result; 
           StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result)))); 
           return result; 
    } 
      
    //------------------------ 
    // RSA解密 
    //------------------------ 
    string RSADecryptString(const char *privFilename, const char *ciphertext) 
    { 
           FileSource privFile(privFilename, true, new HexDecoder);
    
       RSAES_OAEP_SHA_Decryptor priv(privFile); 
      
           string result; 
           StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result)))); 
           return result; 
    } 
      
    //------------------------ 
    // 定义全局的随机数池 
    //------------------------ 
    RandomPool & GlobalRNG() 
    { 
           static RandomPool randomPool; 
           return randomPool; 
    } 
  • 相关阅读:
    【监控笔记】【1.5】事件通知(event Notification)
    【监控笔记】【1.4】Pssdiag和Sqldiag管理器
    【监控笔记】【1.3】监控事件系列——SQL Trace(黑盒跟踪 BlackBox Trace)
    sqlserver数据库大型应用解决方案总结
    如何学习MySQL数据库管理员(OCP)认证(转)
    PL/SQL基本操作
    OracleOraDb11g_home1TNSListener服务无法启动
    sqlplus无法登陆?
    SQL Server中的扩展事件学习系列
    docker容器的服务发现:consul
  • 原文地址:https://www.cnblogs.com/jiaoluo/p/3544910.html
Copyright © 2011-2022 走看看