RSA
Overview
Provides basic RSA cryptographic operations, depends on PKIC.
Encrypt
Is a static stateless function, which uses a PKIC as Public Key.
Takes the buffer by reference and returns a new encrypted copy of the data.
RSA::encrypt(pkic& key,dutils::dbuffer& payload)
- Parameters:
tancrypt::RSA::pkic key- Key container (priv/pubkey must be loaded)dutils::dbuffer& payload- Data buffer to encrypt
- Returns:
dutils::dbuffer- Encrypted data buffer
In case of a failure throws an exception.
Example
#include <iostream>
#include "tancrypt/rsa.hpp"
int main()
{
// Makes life easier
using namespace tancrypt;
// Keypair generation
RSA::pkic key;
key.generate_keypair(2048);
// Preparing example buffer from string
dutils::dbuffer data_in("Hewwo, I am secret ^.^");
// Encrypting data
dutils::dbuffer encrypted_buffer = RSA::encrypt(key,data_in);
// You can check the results via helper function dutils::hexStr
std::cout << "Original:" << data_in.toStr()<< std::endl;
std::cout << "Original(hex):" << dutils::hexStr(data_in) << std::endl;
std::cout << "Encrypted:" << encrypted_buffer.toStr() << std::endl;
std::cout << "Encrypted(hex):" << dutils::hexStr(encrypted_buffer) << std::endl;
return 0;
}
Decrypt
Is a static stateless function, which uses a PKIC as Private Key.
Takes the buffer by reference and returns a new decrypted copy of the data.
RSA::decrypt(pkic& key,dutils::dbuffer& payload)
- Parameters:
tancrypt::RSA::pkic& key- Key container (Must contain private key)dutils::dbuffer& payload- Encrypted data buffer
- Returns:
dutils::dbuffer- Decrypted data buffer
In case of a failure throws an exception.
Example
Sign
Is a static stateless function, takes PKIC and hashing algorithm of your choice.
Takes the buffer by reference and returns a new cryptographic signature.
RSA::sign(pkic& key,dutils::dbuffer &buffer, hashAlg alg)
- Parameters:
tancrypt::RSA::pkic key- key container (must contain private key)dutils::dbuffer &buffer- The data to be signedtancrypt::hashAlg alg- Hashing algorithm to use for the digest
- Returns:
dutils::dbuffer- Data signature
Example
Verify
Is a static stateless function, which takes PKIC, data, hashing algorithm and original data.
This function verifies the signature against original data.
Note
Algorithm choice must match the algorithm which was used to sign the data, otherwise the operation will fail.
RSA::verify(pkic& key, dutils::dbuffer&sig, dutils::dbuffer &buffer, hashAlg alg)
- Parameters:
tancrypt::RSA::pkic& key- key container (must contain public key)dutils::dbuffer &sig- Singature data bufferdutils::dbuffer &buffer- The data to verifytancrypt::hashAlg alg- Hashing algorithm to use for the digest
- Returns:
bool- Signature matches data (returnstrueif valid)
Example