Wraps around a public RSA key's raw data for some added conveniences.
More...
#include <hdicoreCrypt.h>
|
bool | generateKeyPair (const int32_t, PrivateKey &, PublicKey &) |
| Generates a new RSA key pair of a given size/length.
|
|
bool | encryptWithPublicKey (const PublicKey &, const std::string &, std::string &) |
| Encrypts a string (or raw bytes stuffed into a string) with a public RSA key.
|
|
bool | decryptWithPrivateKey (const PrivateKey &, const std::string &, std::string &) |
| Decrypts a base-64 encoded string of cipher text (previously encrypted with the encryptWithPublicKey() function or equivalent)
|
|
bool | encryptWithPrivateKey (const PrivateKey &, const std::string &, std::string &) |
| Encrypts a string (or raw bytes stuffed into a string) with a private RSA key.
|
|
bool | decryptWithPublicKey (const PublicKey &, const std::string &, std::string &) |
| Decrypts a base-64 encoded string of cipher text (previously encrypted with the encryptWithPrivateKey() function or equivalent)
|
|
Wraps around a public RSA key's raw data for some added conveniences.
◆ PublicKey() [1/3]
hdi::core::crypt::rsa::PublicKey::PublicKey |
( |
| ) |
|
Constructs an empty PublicKey object.
- Author
- GW
- Date
- 10/2013
- Note
- To test if a PublicKey object is empty/valid, call valid() on it
-
Empty PublicKey objects do not relate to any actual public RSA key; they are designed to be "receivers" of some other PublicKey object via the overloaded assignment operator. Empty PublicKey objects are useless until such time (though it is safe to call any of their methods).
◆ PublicKey() [2/3]
hdi::core::crypt::rsa::PublicKey::PublicKey |
( |
const PublicKey & |
key_ | ) |
|
Constructs a PublicKey object from another (copy constructor)
- Author
- GW
- Date
- 10/2013
- Parameters
-
◆ PublicKey() [3/3]
hdi::core::crypt::rsa::PublicKey::PublicKey |
( |
const std::string & |
serializedKey_ | ) |
|
Constructs a PublicKey object from a previous serialization of the key (using the serialize() method)
- Author
- GW
- Date
- 10/2013
- Parameters
-
serializedKey_ | Base-64 encoded serialization of a public RSA key |
◆ ~PublicKey()
virtual hdi::core::crypt::rsa::PublicKey::~PublicKey |
( |
| ) |
|
|
virtual |
Destructs a PublicKey object.
- Author
- GW
- Date
- 10/2013
◆ length()
int32_t hdi::core::crypt::rsa::PublicKey::length |
( |
| ) |
const |
Gets the length of the key in bits.
- Author
- GW
- Date
- 10/2013
- Returns
- The number of bits for the public key
◆ operator!=()
bool hdi::core::crypt::rsa::PublicKey::operator!= |
( |
const PublicKey & |
rhs_ | ) |
const |
Compares two PublicKey objects for inequality.
- Author
- GW
- Date
- 10/2013
- Parameters
-
rhs_ | Righthand side of the inequality operator; PublicKey object to compare |
- Returns
- true if the two objects have differing key values, false otherwise
◆ operator=()
Assigns one PublicKey object to another.
- Author
- GW
- Date
- 10/2013
- Parameters
-
rhs_ | Righthand side of the assignment operator; public key object to copy values from |
- Returns
- A reference to the target object (lefthand side of assignment operator), but with its values updated
◆ operator==()
bool hdi::core::crypt::rsa::PublicKey::operator== |
( |
const PublicKey & |
rhs_ | ) |
const |
Compares two PublicKey objects for equality.
- Author
- GW
- Date
- 10/2013
- Parameters
-
rhs_ | Righthand side of the equality operator; PublicKey object to compare |
- Returns
- true if the two objects have the same key values, false otherwise
◆ serialize()
std::string hdi::core::crypt::rsa::PublicKey::serialize |
( |
| ) |
const |
Serializes the public key in a base-64 string; useful for transmitting the string to another party or writing the key to disk.
- Author
- GW
- Date
- 10/2013
- Returns
- The serialized version of the key
- Note
- A key serialization string can be converted back into a PublicKey object at runtime with the PublicKey(const std::string&) constructor.
◆ valid()
bool hdi::core::crypt::rsa::PublicKey::valid |
( |
| ) |
const |
Gets whether the object contains a valid key.
- Author
- GW
- Date
- 10/2013
- Returns
- true if the key is valid/not empty, false otherwise
◆ decryptWithPrivateKey
bool decryptWithPrivateKey |
( |
const PrivateKey & |
, |
|
|
const std::string & |
, |
|
|
std::string & |
|
|
) |
| |
|
friend |
Decrypts a base-64 encoded string of cipher text (previously encrypted with the encryptWithPublicKey() function or equivalent)
- Author
- GW
- Date
- 10/2013
- Parameters
-
key_ | Private RSA key to use for decryption |
b64Cipher_ | Base-64 encoded cipher text to attempt to decrypt |
data__ | Return-by-reference for the decrypted plain text (or raw bytes stuffed into a string) |
- Returns
- true if the data could be decrypted, false otherwise
◆ decryptWithPublicKey
bool decryptWithPublicKey |
( |
const PublicKey & |
, |
|
|
const std::string & |
, |
|
|
std::string & |
|
|
) |
| |
|
friend |
Decrypts a base-64 encoded string of cipher text (previously encrypted with the encryptWithPrivateKey() function or equivalent)
- Author
- GW
- Date
- 10/2013
- Parameters
-
key_ | Public RSA key to use for decryption |
b64Cipher_ | Base-64 encoded cipher text to attempt to decrypt |
data__ | Return-by-reference for the decrypted plain text (or raw bytes stuffed into a string) |
- Returns
- true if the data could be decrypted, false otherwise
◆ encryptWithPrivateKey
bool encryptWithPrivateKey |
( |
const PrivateKey & |
, |
|
|
const std::string & |
, |
|
|
std::string & |
|
|
) |
| |
|
friend |
Encrypts a string (or raw bytes stuffed into a string) with a private RSA key.
- Author
- GW
- Date
- 10/2013
- Parameters
-
key_ | Private RSA key to use for encryption |
data_ | Plain data to be encrypted (in the form of a string or binary data put inside of a string object) |
b64Cipher__ | Return-by-reference for the base-64 encoded cipher text |
- Returns
- true if the data could be encrypted, false otherwise
- Note
- To decrypt the cipher, use the decryptWithPublicKey() function (or equivalent - this lib is built on OpenSSL and the RSA standard, so any base-64 decoder and function that understands RSA private key decryption can decrypt the cipher with the correct key).
-
Typically, sensitive data is not encrypted with a private key because anyone with the public key can decrypt it. However, "signing" (encrypting) some token data with a private key is a common practice to prove to a holder of the public key that the true owner of the private key e.g. received a message and is responding to it (as only the holder of the private key could do this).
◆ encryptWithPublicKey
bool encryptWithPublicKey |
( |
const PublicKey & |
, |
|
|
const std::string & |
, |
|
|
std::string & |
|
|
) |
| |
|
friend |
Encrypts a string (or raw bytes stuffed into a string) with a public RSA key.
- Author
- GW
- Date
- 10/2013
- Parameters
-
key_ | Public RSA key to use for encryption |
data_ | Plain data to be encrypted (in the form of a string or binary data put inside of a string object) |
b64Cipher__ | Return-by-reference for the base-64 encoded cipher text |
- Returns
- true if the data could be encrypted, false otherwise
- Note
- To decrypt the cipher, use the decryptWithPrivateKey() function (or equivalent - this lib is built on OpenSSL and the RSA standard, so any base-64 decoder and function that understands RSA private key decryption can decrypt the cipher with the correct key).
◆ generateKeyPair
Generates a new RSA key pair of a given size/length.
- Author
- GW
- Date
- 10/2013
- Parameters
-
size_ | Size/length of the key in bits (must be n*1024; not recommended for n>16) |
privKey__ | Return-by-reference for the new private key |
pubKey__ | Return-by-reference for the new public key |
- Returns
- true if the key pair could be generated, false otherwise
- Note
- A minimum key size of 2048 bits is recommended.