Crypt-RSA-Yandex
view release on metacpan or search on metacpan
vlong public_key::encrypt( const vlong& plain )
{
#if defined(__DEBUG__)
if ( plain >= m ) {
printf("ERROR: plain too big for this key\n");
}
#endif
return modexp( plain, e, m );
}
vlong private_key::decrypt( const vlong& cipher )
{
// Calculate values for performing decryption
// These could be cached, but the calculation is quite fast
vlong d = modinv( e, (p-(vlong)1)*(q-(vlong)1) );
vlong u = modinv( p, q );
vlong dp = d % (p-(vlong)1);
vlong dq = d % (q-(vlong)1);
// Apply chinese remainder theorem
vlong a = modexp( cipher % p, dp, p );
e = e + (vlong) (me_str[i]-'0');
}
if (e == vlong(0))
throw "Bad key: no value after `#'";
}
void private_key::MakeMeStr(char * me_str)
{
vlong_pair_2_str (me_str,m,e);
}
void private_key::MakePqStr(char * me_str)
{
vlong_pair_2_str (me_str,p,q);
}
void private_key::MakePq (const char *me_str)
{
str_2_vlong_pair (me_str,p,q);
{
m = p*q;
e = 50001; // must be odd since p-1 and q-1 are even
while ( gcd(p-(vlong)1,e) != (vlong)1 || gcd(q-(vlong)1,e) != (vlong)1 ) e += 2;
}
}
// RSA.HPP -------------------------------------------
class public_key
{
public:
vlong m,e;
vlong encrypt( const vlong& plain ); // Requires 0 <= plain < m
void MakeMe(const char *);
};
class private_key : public public_key
{
public:
vlong p,q;
vlong decrypt( const vlong& cipher );
void create( const char * r1, const char * r2 );
// r1 and r2 should be null terminated random strings
// each of length around 35 characters (for a 500 bit modulus)
void MakeMeStr(char *);
void MakePq(const char *);
void MakePqStr(char *);
};
#define MAX_CRYPT_BITS 1024
class CCryptoProviderRSA
{
class private_key prkface;
void EncryptPortion(const char *pt, size_t,char *ct,size_t &);
void DecryptPortion(const char *ct, size_t,char *pt,size_t &);
public:
CCryptoProviderRSA();
virtual ~CCryptoProviderRSA();
virtual void Encrypt(const char *, size_t,char *, size_t &);
( run in 0.285 second using v1.01-cache-2.11-cpan-a5abf4f5562 )