Hot Door CORE  0.7.0
Adobe® Illustrator® Plug-in Library
Public Member Functions | Friends | List of all members
hdi::core::crypt::rsa::PublicKey Class Reference

Wraps around a public RSA key's raw data for some added conveniences. More...

#include <hdicoreCrypt.h>

Public Member Functions

 PublicKey ()
 Constructs an empty PublicKey object. More...
 
 PublicKey (const PublicKey &key_)
 Constructs a PublicKey object from another (copy constructor) More...
 
 PublicKey (const std::string &serializedKey_)
 Constructs a PublicKey object from a previous serialization of the key (using the serialize() method) More...
 
virtual ~PublicKey ()
 Destructs a PublicKey object. More...
 
PublicKeyoperator= (const PublicKey &rhs_)
 Assigns one PublicKey object to another. More...
 
bool valid () const
 Gets whether the object contains a valid key. More...
 
int32_t length () const
 Gets the length of the key in bits. More...
 
std::string 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. More...
 
bool operator== (const PublicKey &rhs_) const
 Compares two PublicKey objects for equality. More...
 
bool operator!= (const PublicKey &rhs_) const
 Compares two PublicKey objects for inequality. More...
 

Friends

bool generateKeyPair (const int32_t, PrivateKey &, PublicKey &)
 Generates a new RSA key pair of a given size/length. More...
 
bool encryptWithPublicKey (const PublicKey &, const std::string &, std::string &)
 Encrypts a string (or raw bytes stuffed into a string) with a public RSA key. More...
 
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) More...
 
bool encryptWithPrivateKey (const PrivateKey &, const std::string &, std::string &)
 Encrypts a string (or raw bytes stuffed into a string) with a private RSA key. More...
 
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) More...
 

Detailed Description

Wraps around a public RSA key's raw data for some added conveniences.

Constructor & Destructor Documentation

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).
hdi::core::crypt::rsa::PublicKey::PublicKey ( const PublicKey key_)

Constructs a PublicKey object from another (copy constructor)

Author
GW
Date
10/2013
Parameters
key_PublicKey object to copy values from
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
virtual hdi::core::crypt::rsa::PublicKey::~PublicKey ( )
virtual

Destructs a PublicKey object.

Author
GW
Date
10/2013

Member Function Documentation

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
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
PublicKey& hdi::core::crypt::rsa::PublicKey::operator= ( const PublicKey rhs_)

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
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
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.
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

Friends And Related Function Documentation

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
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
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).
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).
bool generateKeyPair ( const int32_t  ,
PrivateKey ,
PublicKey  
)
friend

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.