Cryptography Reference
In-Depth Information
return (w + m);
}
////////////////////////////////////////////////////////////////////////
// member functions of the class RSApub
// constructor RSApub()
RSApub::RSApub (const RSAkey& k)
{
pkey = k.export();// import public key from k
}
// RSA encryption
LINT RSApub::crypt (const UCHAR* Mess, int LenMess)
{
int LenEncryptionBlock = key.bytelen_mod - 1;
UCHAR* EncryptionBlock = new UCHAR[LenEncryptionBlock];
// format encryption block according to PKCS #1
if (NULL == format_pkcs1 (EncryptionBlock, LenEncryptionBlock,
BLOCKTYPE_ENCR, Mess, (ULONG)LenMess))
{
delete [] EncryptionBlock;
return LINT (0); // formatting error: message too long
}
// transform encryption block into LINT number (constructor 3)
LINT m = LINT (EncryptionBlock, LenEncryptionBlock);
delete [] EncryptionBlock;
return (mexpkm (m, pkey.pubexp, pkey.mod));
}
// verification of RSA signature
int RSApub::verify (const UCHAR* Mess, int LenMess, const LINT& Signature)
{
int length, BlockType verification = 0;
UCHAR m H1[RMDVER>>3];
UCHAR* H2 = NULL;
UCHAR* EB = lint2byte (mexpkm (Signature, pkey.pubexp, pkey.mod), &length);
ripemd160 (H1 (UCHAR*)Mess, (ULONG)LenMess);
// take data from decrypted PKCS #1 encryption block
BlockType = parse_pkcs1 (H2, EB, &length, pkey.bytelen_mod);
if ((BlockType == 0 || BlockType == 1) && // Block Type Signature
(HashRes2 > NULL) && (length == (RMDVER >> 3)))
{
verification = !memcmp ((char *)H1, (char *)H2, RMDVER >> 3);
}
Search WWH ::




Custom Search