标签:效率比较 window code define pen 随机 span com 算法
环境的配置可以参考http://www.cnblogs.com/yangyquin/p/5284530.html
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <Windows.h> 4 #include <openssl/rsa.h> 5 #include <openssl/err.h> 6 #pragma comment(lib,"libeay32.lib") 7 #pragma comment(lib,"ssleay32.lib") 8 #define P "BCF3" 9 #define Q "116AB" 10 #define N "CDAE1851" 11 #define E "10001" 12 #define D "8C88F2A5" 13 int My_Rsa_public_encrypt(int flen, const unsigned char *from, unsigned char *to); 14 int My_Rsa_private_decrypt(int flen, const unsigned char *from, unsigned char *to); 15 16 int main(void) 17 { 18 char* from ="456"; 19 unsigned char sz [500]={0}; 20 unsigned char decsz[500]={0}; 21 My_Rsa_public_encrypt(lstrlen(from)+1,(unsigned char*)from,sz); 22 My_Rsa_private_decrypt(lstrlen((char*)sz),sz,decsz); 23 system("pause"); 24 return 0; 25 } 26 int My_Rsa_public_encrypt(int flen, const unsigned char *from, unsigned char *to) 27 { 28 RSA* rSa = RSA_new(); 29 if (!rSa)return -1; 30 BIGNUM * bIgnUm = BN_new(); 31 BIGNUM * bIgnUe = BN_new(); 32 if (!bIgnUe || !bIgnUm) 33 { 34 RSA_free(rSa); 35 BN_free(bIgnUe); 36 BN_free(bIgnUm); 37 return -1; 38 } 39 BN_init(bIgnUm); 40 BN_init(bIgnUe); 41 BN_hex2bn(&bIgnUm,N); 42 BN_hex2bn(&bIgnUe,E); 43 rSa->n = bIgnUm; 44 rSa->e = bIgnUe; 45 ERR_load_crypto_strings(); 46 int nRet = RSA_public_encrypt(flen,from,to,rSa,RSA_NO_PADDING); 47 DWORD dwError = ERR_get_error(); 48 if(nRet<0) 49 { 50 printf("%s\r\n",ERR_lib_error_string(dwError)); 51 printf("%s\r\n",ERR_func_error_string(dwError)); 52 printf("%s\r\n",ERR_reason_error_string(dwError)); 53 BN_free(bIgnUm); 54 BN_free(bIgnUe); 55 RSA_free(rSa); 56 return -1; 57 } 58 printf("%X\r\n",*(PDWORD)to); 59 ERR_free_strings(); 60 BN_free(bIgnUe); 61 BN_free(bIgnUm); 62 RSA_free(rSa); 63 return 1; 64 65 } 66 int My_Rsa_private_decrypt(int flen, const unsigned char *from, unsigned char *to) 67 { 68 RSA* rSa = RSA_new(); 69 if (!rSa)return -1; 70 BIGNUM * bIgnUm = BN_new(); 71 BIGNUM *bIgnUp = BN_new(); 72 BIGNUM * bIgnUe = BN_new(); 73 if (!bIgnUe || !bIgnUm ||!bIgnUp) 74 { 75 RSA_free(rSa); 76 BN_free(bIgnUe); 77 BN_free(bIgnUm); 78 BN_free(bIgnUp); 79 return -1; 80 } 81 BN_init(bIgnUm); 82 BN_init(bIgnUe); 83 BN_init(bIgnUp); 84 BN_hex2bn(&bIgnUp,E); 85 BN_hex2bn(&bIgnUm,N); 86 BN_hex2bn(&bIgnUe,D); 87 rSa->n = bIgnUm; 88 rSa->d = bIgnUe; 89 rSa->e = bIgnUp; 90 ERR_load_crypto_strings(); 91 int nRet = RSA_private_decrypt(flen,from,to,rSa,RSA_NO_PADDING); 92 DWORD dwError = ERR_get_error(); 93 if(nRet<0) 94 { 95 printf("%s\r\n",ERR_lib_error_string(dwError)); 96 printf("%s\r\n",ERR_func_error_string(dwError)); 97 printf("%s\r\n",ERR_reason_error_string(dwError)); 98 BN_free(bIgnUm); 99 BN_free(bIgnUe); 100 RSA_free(rSa); 101 return -1; 102 } 103 printf("%s\r\n",to); 104 ERR_free_strings(); 105 BN_free(bIgnUe); 106 BN_free(bIgnUm); 107 BN_free(bIgnUp); 108 RSA_free(rSa); 109 return 1; 110 }
标签:效率比较 window code define pen 随机 span com 算法
原文地址:http://www.cnblogs.com/guolongzheng/p/7419413.html