rsa.cpp
来自「含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种Chec」· C++ 代码 · 共 149 行
CPP
149 行
/************************************************** RSA Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/rsa.h>#include <botan/numthry.h>namespace Botan {/************************************************** RSA_PublicKey Constructor **************************************************/RSA_PublicKey::RSA_PublicKey(const BigInt& mod, const BigInt& exp) : n(mod), e(exp), powermod_e_n(e, n) { if(e < 3 || e % 2 == 0) throw Invalid_Argument("RSA_PublicKey: invalid exponent"); if(n < 15 || n % 2 == 0) throw Invalid_Argument("RSA_PublicKey: invalid modulus"); }/************************************************** RSA Encryption Function **************************************************/SecureVector<byte> RSA_PublicKey::encrypt(const byte in[], u32bit len) const { BigInt temp(in, len); return encode(public_op(temp)); }/************************************************** RSA Verification Function **************************************************/SecureVector<byte> RSA_PublicKey::verify(const byte in[], u32bit len) const { BigInt temp(in, len); return encode(public_op(temp)); }/************************************************** RSA Public Operation **************************************************/BigInt RSA_PublicKey::public_op(const BigInt& i) const { if(i >= n || i.is_negative()) throw Invalid_Argument("RSA::public_op: i >= n || i < 0"); return powermod_e_n(i); }/************************************************** RSA_PrivateKey Constructor **************************************************/RSA_PrivateKey::RSA_PrivateKey(const BigInt& prime1, const BigInt& prime2, const BigInt& exp, const BigInt& d_exp, const BigInt& modulus) : RSA_PublicKey(((!modulus) ? prime1 * prime2 : modulus), exp) { if(prime1 < 3 || prime2 < 3) throw Invalid_Argument("RSA_PrivateKey: invalid prime(s)"); if(d_exp != 0 && d_exp < 3) throw Invalid_Argument("RSA_PrivateKey: invalid decryption exponent"); p = prime1; q = prime2; d = (!d_exp) ? inverse_mod(e, lcm(p - 1, q - 1)) : d_exp; precompute(); }/************************************************** Create a RSA Key **************************************************/RSA_PrivateKey::RSA_PrivateKey(u32bit bits, const BigInt& exp) { if(bits < 64) throw Invalid_Argument("RSA: Can't make a key that is only " + to_string(bits) + " bits long"); if(exp < 3 || exp % 2 == 0) throw Invalid_Argument("RSA: Invalid encryption exponent"); e = exp; p = random_prime((bits + 1) / 2, e); q = random_prime(bits - p.bits(), e); n = p * q; d = inverse_mod(e, lcm(p - 1, q - 1)); precompute(); }/************************************************** RSA Decryption Operation **************************************************/SecureVector<byte> RSA_PrivateKey::decrypt(const byte in[], u32bit len) const { BigInt temp(in, len); return encode(private_op(temp)); }/************************************************** RSA Signature Operation **************************************************/SecureVector<byte> RSA_PrivateKey::sign(const byte in[], u32bit len) const { BigInt temp(in, len); return encode(private_op(temp)); }/************************************************** RSA Private Key Operation **************************************************/BigInt RSA_PrivateKey::private_op(const BigInt& i) const { if(i >= n || i.is_negative()) throw Invalid_Argument("RSA::private_op: i >= n || i < 0"); BigInt j1 = powermod_d1_p(i); BigInt j2 = powermod_d2_q(i); BigInt h = powermod_d1_p.reduce(sub_mul(j1, j2, c)); return mul_add(h, q, j2); }/************************************************** RSA Precomputation **************************************************/void RSA_PrivateKey::precompute() { d1 = d % (p - 1); d2 = d % (q - 1); c = inverse_mod(q, p); powermod_d1_p = FixedExponent_Exp(d1, p); powermod_d2_q = FixedExponent_Exp(d2, q); if(!powermod_e_n.get_exponent()) powermod_e_n = FixedExponent_Exp(e, n); }/************************************************** Check Private RSA Parameters **************************************************/bool RSA_PrivateKey::check_params() const { if(!is_prime(p) || !is_prime(q)) return false; if(p * q != n) return false; if((e * d) % lcm(p - 1, q - 1) != 1) return false; return true; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?