![]() |
Gray C++ Libraries
0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
|
#include <cKeyRSA1.h>
Public Types | |
typedef cBigUnsigned | VAL_t |
Public Member Functions | |
cKeyRSA1 (void) | |
cKeyRSA1 (const VAL_t &_exp, const VAL_t &_mod) | |
virtual | ~cKeyRSA1 (void) |
bool | IsValidKey () const |
void | SetZeroKey () |
void | SetCopyKey (const cKeyRSA1 &src) |
void | GenerateKeys (OUT VAL_t &d, const VAL_t &p, const VAL_t &q) |
void | GenerateKeys (BIT_ENUM_t nBits, OUT VAL_t &d, OUT VAL_t *pP=nullptr, OUT VAL_t *pQ=nullptr) |
VAL_t | EncryptDecrypt (const VAL_t &source) const |
HRESULT | WritePublicKey (cASNWriterRev &w) const |
HRESULT | ReadPublicKey (cASNReader &r) |
HRESULT | LoadKeyFile (cStreamInput &rInp, const char *pszName=nullptr) |
HRESULT | LoadKeyFile (const FILECHAR_t *pszFileName, const char *pszName=nullptr) |
UNITTEST_FRIEND (cKeyRSA1) | |
Public Attributes | |
VAL_t | m_N |
modulus of the key (usually public) = multiplication of the 2 primes (pq). More... | |
VAL_t | m_E |
exponent of the key (public or private half of the key depending on use.) More... | |
Represent one side of the RSA key pair. public/private key cryptography. (asymmetric) used by SSL, OpenSSL, PGP big and slow. Rivest, Shamir, and Adleman patent expired on 21 September 2000 public key can encrypt a message that only the private key can decrypt/read. TLS (new SSL) provides RSA security with 1024 and 2048 bit strengths. X.509 certificates http://en.wikipedia.org/wiki/X.509
http://www.efgh.com/software/rsa.htm from: http://www.geocities.com/Colosseum/Park/8386/keepsecret.htm The theory behind RSA goes like this: Pick a pair of very large prime numbers and call them p and q. Multiply p and q together to produce an even larger number called n. Now pick another large number, which we'll call e, that is less than n and relatively prime to the product of p-1 and q-1. (Relatively prime means neither number is a factor of the other.) The numbers n and e now constitute the public key, and the private key d can be computed from e, p, and q using a mathematical formula.
To encrypt a block of data, raise the number the block represents (the number you get by lining up all the bits in the block of data and treating them as one very large number) to the power of e and perform a module n operation on the result. (For the mathematically uninitiated, the modulus operation here is the same as dividing the result by n and keeping the remainder.) To decrypt that same block, raise it to the power of d and perform a module n operation on the result. Perform the encryption operation on every block of data in the message, and you've successfully encrypted it using RSA. The security of RSA is predicated on the fact that it is very, very difficult– even for the fastest computers–to factor large numbers that are the products of prime numbers. It is critical that once you compute n from p and q, you discard p and q forever. If p and q become known, that RSA is worthless because the private key can be calculated. RSA can be broken, but it would take the world's fastest supercomputer hundreds or thousands of years to derive the private key from the public key by guessing the values of p and q. Are there enough prime numbers out there to rule out a trial-and-error approach to calculating the factors of n? You bet. The number of prime numbers whose length is 512 bits or less is more than 1E150 (the number 1 followed by 150 zeros), which, according to a publication from RSA Laboratories, is "more than the number of atoms in the known universe."
typedef cBigUnsigned GrayLib::cKeyRSA1::VAL_t |
GrayLib::cKeyRSA1::cKeyRSA1 | ( | void | ) |
|
virtual |
cKeyRSA1::VAL_t GrayLib::cKeyRSA1::EncryptDecrypt | ( | const VAL_t & | source | ) | const |
http://en.wikipedia.org/wiki/RSA_%28algorithm%29 result = ((source^m_E)m_N). modexp m_E = exponent of the key (public or private) m_N = modulus of the key (public and private should be same) = multiplication of the 2 primes.
void GrayLib::cKeyRSA1::GenerateKeys | ( | BIT_ENUM_t | nBits, |
OUT VAL_t & | d, | ||
OUT VAL_t * | pP = nullptr , |
||
OUT VAL_t * | pQ = nullptr |
||
) |
Generate the prime and public and private keys. *this is the public side. One key is (d,n) (private) exponent and modulus and the other is (e,n) (public) public and private keys are arbitrary interchangeable.
Generate the public and private keys given 2 prime numbers. *this is the public side. ASSUME m_E holds a random number to be the new public key exponent. It may need to be changed.
bool GrayLib::cKeyRSA1::IsValidKey | ( | ) | const |
Check a RSA key. Don't know or care if its public or private.
HRESULT GrayLib::cKeyRSA1::LoadKeyFile | ( | const FILECHAR_t * | pszFileName, |
const char * | pszName = nullptr |
||
) |
HRESULT GrayLib::cKeyRSA1::LoadKeyFile | ( | cStreamInput & | rInp, |
const char * | pszName = nullptr |
||
) |
Load a .KEY type file. cPEM
HRESULT GrayLib::cKeyRSA1::ReadPublicKey | ( | cASNReader & | r | ) |
|
inline |
void GrayLib::cKeyRSA1::SetZeroKey | ( | ) |
Free/clear all components of an RSA key
GrayLib::cKeyRSA1::UNITTEST_FRIEND | ( | cKeyRSA1 | ) |
HRESULT GrayLib::cKeyRSA1::WritePublicKey | ( | cASNWriterRev & | w | ) | const |
VAL_t GrayLib::cKeyRSA1::m_E |
exponent of the key (public or private half of the key depending on use.)
VAL_t GrayLib::cKeyRSA1::m_N |
modulus of the key (usually public) = multiplication of the 2 primes (pq).