RSA Signature Algorithm based on crtpto++

Source: Internet
Author: User
Tags strlen

Toss for a quick 1 hours, will be common to some of the encrypted libraries are tested, then choose an application to the project according to the situation. crypto++ domestic use quite much, the data is relatively complete, but let me hate is the source file is too messy, all the algorithms are included, I can not tell which documents are I need, So compiled crypto++ source code generated static link library incredibly reached 34M, very scary Ah, software release time this algorithm library has 34M, than the software itself is larger, is trying to extract the part of their needs.

#include "randpool.h"
#include "Rsa.h"
#include "hex.h"
#include "Files.h"
#include <iostream>

using namespace Std;
using namespace Cryptopp;
//------------------------
function declaration
//------------------------
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 ();

//------------------------
Main program
//------------------------
int main ()
{
Char prikey[128] = {0};
Char pubkey[128] = {0};
Char seed[1024] = {0};
Generate RSA Key Pair
strcpy (Prikey, "PRIVATE.ILCD"); The generated private key file name
strcpy (PubKey, "PUBLIC.ILCD"); The generated public key file name
strcpy (seed, "seed");
Create public key, private key pairing
Generatersakey (1024, Prikey, PubKey, Seed);

RSA Plus decryption
Char message[1024] = {0};
strcpy (Message, "");
cout<< "original string: \ t" <<message<<endl<<endl;
String encryptedtext = rsaencryptstring (PubKey, seed, message); RSA encryption
cout<< "encrypted string: \ t" <<encryptedText<<endl<<endl;
String decryptedtext = Rsadecryptstring (Prikey, Encryptedtext.c_str ()); RSA decryption
cout<< "decrypted string: \ t" <<decryptedText<<endl<<endl;
return 0;
}

//------------------------
Generate RSA key pair
//------------------------
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 encryption
//------------------------
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));
return result;
}

//------------------------
RSA decryption
//------------------------
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;
}

//------------------------
Define the global random number
//------------------------
Randompool & Globalrng ()
{
Static Randompool Randompool;
return randompool;
}

This article from the "Nine Li Tribe" blog, please be sure to retain this source http://axiii.blog.51cto.com/396236/115

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.