00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef RSA_H
00011 #define RSA_H RSA_H
00012
00013 #include <gmp.h>
00014 #include <vector>
00015
00017
00018 class mpz {
00019 public:
00020 mpz_t t;
00021 int dummy;
00022 mpz() { mpz_init(t); }
00023 ~mpz(){ mpz_clear(t); }
00024 mpz(const mpz& o){ mpz_init_set(t, o.t); }
00025 mpz& operator=(const mpz& o){ mpz_init_set(t, o.t); return *this; }
00026 };
00027
00029 class rsa {
00030 public:
00031 rsa() {};
00032 rsa(unsigned char* modulus, int mlen, unsigned char* exponent, int elen){
00033 mpz_import(n.t, mlen, 1, 1, 0, 0, modulus);
00034 mpz_import(e.t, elen, 1, 1, 0, 0, exponent);
00035 }
00036 std::vector<unsigned char> perform(const unsigned char* input, int length) const{
00037 mpz in, out;
00038 mpz_import(in.t, length, 1, 1, 0, 0, input);
00039 mpz_powm(out.t, in.t,e.t,n.t);
00040 size_t size;
00041 std::vector<unsigned char> rv(mpz_sizeinbase(out.t, 256) + 10);
00042 mpz_export(&rv[0], &size, 1, 1, 0, 0, out.t);
00043 rv.resize(size);
00044 return rv;
00045 }
00046 private:
00047 mpz n, e;
00048 };
00049
00050 #endif