Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Rsa Class Reference

Performs RSA encryption and decryption in-memory. More...

#include <Rsa.h>

List of all members.

Public Methods

 Rsa ()
 ~Rsa ()
bool loadKey (Cert &cert)
bool loadKey (char *file, char *pass)
int publicEncrypt (unsigned char *in, int inlen, unsigned char *out)
int privateDecrypt (unsigned char *in, int inlen, unsigned char *out)
int privateEncrypt (unsigned char *in, int inlen, unsigned char *out)
int publicDecrypt (unsigned char *in, int inlen, unsigned char *out)

Static Public Attributes

const int blockSize = RSA_BLOCK_SIZE
 basic block size for encoding/decoding. More...


Private Attributes

RSA * m_rsa


Detailed Description

Performs RSA encryption and decryption in-memory.

Rsa encrypts and decrypts data using the OpenSSL RSA library.
Rsa does nothing but wrap the OpenSSL RSA library.
Rsa encryptes data of 117 bytes at most and the result of encryption is always
128 bytes.

=============
Rsa Class API
=============

 Member Variables
    m_rsa: openssl rsa structure.

 Member Functions

    Rsa() is a constructor of Rsa. It sets m_rsa to NULL;
    
    ~Rsa() is a destructor of Rsa. It frees m_rsa.

    loadKey(Cert &cert) creates m_rsa from a cert which contains a public key.
    Once the key is loaded, this object can then use publicEncrypt() and publicDecrypt().

    loadKey(char* file, char* passwd) creates m_rsa from a file which contains
    a public and private key pair. passwd is a key to decrypt the file. 
    Once these keys are loaded, this object can use publicEncrypt(), privateDecrypt(),
    privateEncrypt(), and publicDecrypt().

    publicEncrypt() is a wrapper of RSA_public_encrypt() and encrypts data 
    with a public key.

    privateDecrypt() is a wrapper of RSA_private_decrypt() and decrypts data
    with a private key.

    privateEncrypt() is a wrapper of RSA_private_encrypt() and encrypts data
    with a private key.

    publicDecrypt() is a wrapper of RSA_public_decrypt() and decrypts data
    with a public key.

=======
Example
=======

Cert cert;
Rsa  pub;
Rsa  priv;

int  enc_len;
char enc_data[128];
int  dec_len;
char dec_data[117];

// Generate a public and private key pair at random.
// cert.pem contains a public key.
// priv.pem contains both public and private key.
// priv.pem is encrypted with a key "peekabooty".
Cert::generateRandomCerts(".", "./openssl.cnf", "./cert.pem", "./priv.pem",
                           "peekabooty");

// Load a public key from "cert.pem".
cert.load("cert.pem");
pub.loadKey(cert);

// Load a public key and a private key from "priv.pem" with a key "peekabooty".
priv.loadKey("priv.pem", "peekabooty");

// Encrypt "test data!!!" and store the result to enc_data.
enc_len = pub.publicEncrypt("test data!!!", strlen("test data!!!"), enc_data);
ASSERT(enc_len == 128);

// Decrypt enc_data and store the result to dec_data.
dec_len = priv.privateDecrypt(enc_data, enc_len, dec_data);
ASSERT(dec_len <= 117);

  

Definition at line 15 of file Rsa.h.


Constructor & Destructor Documentation

Rsa::Rsa
 

Definition at line 86 of file Rsa.cpp.

Rsa::~Rsa
 

Definition at line 92 of file Rsa.cpp.


Member Function Documentation

bool Rsa::loadKey char * file,
char * passwd
 

Load RSA key pair from private pem file.

Parameters:
file   it points to a file of private key in PEM format.
pass   password for private key file.

Definition at line 162 of file Rsa.cpp.

bool Rsa::loadKey Cert & cert
 

Load public key from Cert.

Parameters:
cert   Cert containing public key.
Returns:
true if succeed, false otherwise.

Definition at line 107 of file Rsa.cpp.

int Rsa::privateDecrypt unsigned char * in,
int inlen,
unsigned char * out
 

Decrypt data with private key.

Parameters:
in   input data
inlen   the size of input data
out   output buffer. output buffer should have Rsa::blockSize byte spaces.
Returns:
the size of output data.

Definition at line 245 of file Rsa.cpp.

int Rsa::privateEncrypt unsigned char * in,
int inlen,
unsigned char * out
 

Encrypt data with private key.

Parameters:
in   input data
inlen   the size of input data. inlen *must* be less or equal than 117.
out   output buffer. output buffer should have Rsa::blockSize byte spaces.
Returns:
the size of output data.

Definition at line 265 of file Rsa.cpp.

int Rsa::publicDecrypt unsigned char * in,
int inlen,
unsigned char * out
 

Decrypt data with public key.

Parameters:
in   input data
inlen   the size of input data
out   output buffer. output buffer should have Rsa::blockSize byte spaces.
Returns:
the size of output data.

Definition at line 285 of file Rsa.cpp.

int Rsa::publicEncrypt unsigned char * in,
int inlen,
unsigned char * out
 

Encrypt data with public key.

Parameters:
in   input data.
inlen   the size of input data. inlen *must* be less or equal than 117.
out   output buffer. output buffer should have Rsa::blockSize byte spaces.
Returns:
the size of output data.

Definition at line 226 of file Rsa.cpp.


Member Data Documentation

const int Rsa::blockSize = RSA_BLOCK_SIZE [static]
 

basic block size for encoding/decoding.

Definition at line 84 of file Rsa.cpp.

RSA* Rsa::m_rsa [private]
 

Definition at line 38 of file Rsa.h.


The documentation for this class was generated from the following files:
Generated at Thu Jul 11 13:31:58 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001