/***************************************************************************** * 函 数 名 : idioIdentify * * 功能描述 : 通过校验输入数据的hash值是 否 * 与其签名用RSA解密后的值一样 * 来校验输入数据的合法性 * * * 输入参数 : dataAddr--输入数据的地址 * dataLen--输入数据的长度 * pubKey--公钥 * pIdio--输入数据签名的指针 * 输出参数 : * * 返 回 值 : OK/ !OK * * 其它说明 : * *****************************************************************************/ int idioIdentify(UINT32 dataAddr, UINT32 dataLen, KEY_STRUCT *pubKey, UINT32* pIdio) { UINT32 sha256Hash[SHA256_HASH_SIZE]; /*用来存放SHA256值的临时buffer*/ UINT32 rsa[IDIO_LEN/4]; /*用来存放RSA加密值的临时buffer*/ int i; memset(sha256Hash, 0, SHA256_HASH_SIZE*4); memset(rsa, 0, IDIO_LEN); /*通过SHA256计算输入数据(data)的hash值*/ if(OK != SHA256Hash(dataAddr, dataLen, sha256Hash)) { print_info("\r\nhash calc err!" ); return !OK; } /*使用公钥(pubKey),通过RSA解密出输入签名(dataIdioAddr)的明文*/ if(OK != RSA(pubKey, pIdio, rsa)) { print_info("\r\nrsa calc err!" ); return !OK; } /*比较二者是否相同*/ for(i = 0; i < SHA256_HASH_SIZE; i++) { if( sha256Hash[i] != rsa[i] ) { return !OK; } } return OK; }
int main() { //shamir(); el_gamal(); vernama(); RSA(); return 0; }
Result<RSA> RSA::from_pem(Slice pem) { init_crypto(); auto *bio = BIO_new_mem_buf(const_cast<void *>(static_cast<const void *>(pem.ubegin())), narrow_cast<int32>(pem.size())); if (bio == nullptr) { return Status::Error("Cannot create BIO"); } SCOPE_EXIT { BIO_free(bio); }; auto *rsa = RSA_new(); if (rsa == nullptr) { return Status::Error("Cannot create RSA"); } SCOPE_EXIT { RSA_free(rsa); }; if (!PEM_read_bio_RSAPublicKey(bio, &rsa, nullptr, nullptr)) { return Status::Error("Error while reading rsa pubkey"); } if (RSA_size(rsa) != 256) { return Status::Error("RSA_size != 256"); } const BIGNUM *n_num; const BIGNUM *e_num; #if OPENSSL_VERSION_NUMBER < 0x10100000L n_num = rsa->n; e_num = rsa->e; #else RSA_get0_key(rsa, &n_num, &e_num, nullptr); #endif auto n = static_cast<void *>(BN_dup(n_num)); auto e = static_cast<void *>(BN_dup(e_num)); if (n == nullptr || e == nullptr) { return Status::Error("Cannot dup BIGNUM"); } return RSA(BigNum::from_raw(n), BigNum::from_raw(e)); }
void choice() { printf("\n "); printf("\n \t \t \t \t Menu"); printf("\n "); printf("\n \t \t \t1.Simple Cryptography"); printf("\n "); printf("\n \t \t \t2.Difficult Cryptography"); printf("\n "); printf("\n \t \t \t3.Exit"); printf("\n "); int ch,flag=1; printf("\n \t \t \tEnter the choice of Cryptography\n \t \t \t"); scanf("%d",&ch); switch(ch) { case 1:caesar(); break; case 2:RSA(); break; case 3:exit(0); default: printf("\n \t \t \tInvalid Choice"); printf("\n "); flag=0; } if(flag==0) { int i; system("cls"); //Used to clear the console printf("\n \t \t \tInvalid Choice please wait"); for (i=0;i<4;i++) { delay(); printf("."); } system("cls"); choice(); } }
RSA RSA::clone() const { return RSA(n_.clone(), e_.clone()); }