Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simple RSA key generation and encryption library for C++

NotificationsYou must be signed in to change notification settings

Soreing/rsa-cpp-encrypt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

rsa-cpp-encrypt is a simple textbook RSA library for C++. The library is capable of generating 2048 bit RSA key pairs, encrypting, decrypting and also exporting/importing keys in PKCS#1 format. The implementation usesbiging-x86cpp and is only compatible with x86 architecture CPUs

Installation

Add the foldersbigint andrsa-crypt from/include in your include path. If you want to compile the library from source, includeASN1.cpp andRSACipher.cpp from the/src folder. Alternatively, you can compile the source code to a static library and include it that way.

Usage

Creating an RSA Cipher

To create a cipher, you can provide a public or a private key. If you want to generate a new key, you can use thegenPrivKey() function. The function generates a random key you can seed with the Csrand() function.

srand(1234);RSACipherrsa(genPrivKey());

Importing Keys

Alternatively, you can create an empty RSA cipher, then import a public or a private key. The import and export functions only take PKCS#1 format RSA keys.

RSACipher rsa;rsa.importPubKey("-----BEGIN RSA PUBLIC KEY-----\n""MIIBCgKCAQEAq ...... 7dcERQIDAQAB""\n-----END RSA PUBLIC KEY-----");
RSACipher rsa;rsa.importPrvKey("-----BEGIN RSA PRIVATE KEY-----\n""MIIEpQIBAAKCA ...... dMbKLovrxqU=""\n-----END RSA PRIVATE KEY-----");

Exporting Keys

You can export a private or a public key into a character array buffer. You also need to provide the size of the buffer. The key is exported in PKCS#1 format

char buff[2048];rsa.exportPubKey(buff,2048);// Prints PKCS#1 formatted Public Keycout << buff << endl;

Encrypting and Decrypting

Once you have created an RSA cipher, you can encrypt or decrypt 256 bytes of data. For this, the cipher expects a 256 byte large Big Integer. The result of encryption and decryption is also a Big Integer.

BigInt<256>message("123456");BigInt<256> ciphertext = rsa.encrypt(message);BigInt<256> plaintext  = rsa.decrypt(ciphertext);cout<< message <<"\n\n";cout<< ciphertext.toString(64) <<"\n\n";cout<< plaintext <<"\n\n";
Output:123456S12DtydRI2r72ouUWsHSVat5lfuU45bYVl+W3aPgcKnnlX05w2qmlgjN+MMsURnmA0/SAM+ZPvBej8Vvs3i0XwT5J6Q+qR6k7SQZ6qdPM8tiqaHeJ+9tVMLu+NJ4NTmtUypFxIufm0agvx3RyK4EmhOOw7dZC9CqadnAWxDYDpG6eVkH+PO2qUzxVddy4Iojj/29kjo2p3TzvSoE7lMoAT3trW3Er+vhjRpK/saFD8EnsTc//bfQ87EFUxt81p+e9uGiXz4Cd6aWUTXNypo3uy+VaLg9+9yQ1WVdxPrv/OMFNsE7XsFW9DgfZHGZy+4T0IekUvEjGf2+2b8ciYXpZA==123456

About

Simple RSA key generation and encryption library for C++

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp