用C ++实现RSA密码学

编程入门 行业动态 更新时间:2024-10-28 00:15:09
本文介绍了用C ++实现RSA密码学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用C ++类实现RSA。 以下是课程:

I am trying to implement RSA using a C++ class. Below is the class :

class rsacrypto { long publickey; long privatekey; long modl; //Modulus public : rsacrypto(); //To be used to just generate private and public keys. rsacrypto(long &,long &,long &);//To be used just to generate private and public keys. rsacrypto(long key,long modulus) // Should be used when a data is to be encrypted or decrypted using a key. { publickey = privatekey = key; modl = modulus; } long ret_publickey() { return publickey; } long ret_privatekey() { return privatekey; } long ret_modulus() { return modl; } void encrypt(char *); void decrypt(char *); }; rsacrypto::rsacrypto() { long p1,p2; //Prime numbers long n = 0; //Modulus long phi =0; //Totient value. long e = 0; //Public key exponent. long d = 0; //Private key exponent. p1 = genrndprimes(100,900); Sleep(1000); p1 = genrndprimes(100,900); n = p1*p2; phi = totient(n); e = genrndnum(2,(phi-1)); while(gcd(e,phi)!=1) { e = genrndnum(2,(phi-1)); } d = (1/e)%phi; //Modular Multiplicative Inverse. privatekey = e; publickey = d; modl = n; } rsacrypto::rsacrypto(long &pubkey,long &privkey,long &mdls) { long p1,p2; //Prime numbers long n = 0; //Modulus long phi =0; //Totient value. long e = 0; //Public key exponent. long d = 0; //Private key exponent. p1 = genrndprimes(100,900); Sleep(1000); p1 = genrndprimes(100,900); n = p1*p2; phi = totient(n); e = genrndnum(2,(phi-1)); while(gcd(e,phi)!=1) { e = genrndnum(2,(phi-1)); } d = (1/e)%phi; //Modular Multiplicative Inverse. privatekey = e; publickey = d; pubkey = publickey; privkey = privatekey; mdls = n; modl = n; } void rsacrypto::encrypt(char *dat) { long siz = strlen(dat); for(long i=0;i<siz;i++) { dat[i]=(long)pow(dat[i],publickey)%modl; } } void rsacrypto::decrypt(char *datn) { long sizz = strlen(datn); for(long i=0;i<sizz;i++) { datn[i]=(long)pow(datn[i],privatekey)%modl; } }

genrndprimes( a,b):在a和b之间生成一个随机素数(如果a和b都是素数,则也包括a和b)。 genrndnum(a ,b):在a和b之间生成一个随机数(也包括a和b)。 主程序:

genrndprimes(a,b) : Generates a random prime number between a and b (also inclusive of a and b if any or both a and b are prime). genrndnum(a,b) : Generates a random number between a and b (also inclusive of a and b). Main Program :

void main() { char datm[]="Hello!! , Implementing RSA"; rsacrypto m; long prkey = m.ret_privatekey(); long publkey = m.ret_publickey(); long modulm = m.ret_modulus(); rsacrypto jj(publkey,modulm); rsacrypto ll(prkey,modulm); jj.encrypt(datm); puts(datm); cout<<"\n"; ll.decrypt(datm); puts(datm); }

我面临的问题是这个输出: 加密时:25个面部字符(ASCII值未知)。 解密后:(相同)25个面部字符(ASCII值未知)。

The problem i face is this output : Upon Encryption : 25 face characters (ASCII value value Unknown). Upon Decryption : (SAME) 25 face characters (ASCII value Unknown).

推荐答案

参见使用Win32加密API进行加密 [ ^ ],这应该有帮助。 See Encryption using the Win32 Crypto API[^], which should help.

我不是要发布一个解决方案,但我无法发布代码评论。我以前写过这两个函数用于调试。不确定它们是否符合您的需求。它们也可以进行改进和优化。希望他们能帮助你,这些评论可以解释他们的所作所为。使用它们,您可以将二进制数组字节转换为其ascii表示。 Hi, I don't mean to post a solution to this but I could not post code on the comments. Ihad written these two functions sometime ago for debugging. Not sure if they fit your needs. Also they can be improved and optimized. Hope they help you, the comments sort of explain what they do. With them you can convert a binary array of bytes to its ascii representation. /************************************************************************************ * int CMFRegisterLogger::iConvertBinArray2String( char* bBindata, * int iDataSize, * char* szDataString ) * description: Originally the data from the MF is binary, thus to print it in a file we must convert * it to an ASCII representation of this array of bytes. This convertion is not literal, * because each nibble of the bin values is converted to an ASCII byte. The output is * twice as big as the input * INPUT: Original data buffer and its size * OUTPUT: New buffer * RETURNS: returns 0 OK else 1 *************************************************************************************/ int iConvertBinArray2String( char* bBindata, int iDataSize, char* szDataString ) { if (( bBindata == NULL )||(szDataString) == NULL ) return 1; for(int i = 0; i < iDataSize ; i++) { char tmp[3]; memset(tmp,0,3); sConvertHexBin2String((unsigned char)bBindata[i],(unsigned char*)tmp); memcpy((szDataString+2*i),tmp,2); } return 0; } /************************************************************************************ *unsigned char* CMFRegisterLogger::sConvertHexBin2String(unsigned char ch, unsigned char* buffer) * description: Originally the data from the MF is binary, thus to print it in a file we must convert * it to an ASCII representation of this array of bytes. This convertion is not literal, * because each nibble of the bin values is converted to an ASCII byte. The output is * twice as big as the input. This method actually converts each nibble of a byte to * its ascii representation. * INPUT: A char * OUTPUT: 2 byte ASCII array representation * RETURNS: Pointer of the first byte of this array ( includes NULL terminator ) *************************************************************************************/ unsigned char* sConvertHexBin2String(unsigned char ch, unsigned char* buffer) { char tmp[3]; memset(tmp,0,3); if ( (ch&0x0F) == 0 ) { if ( ch == 0x00 ) { tmp[0] = 0x30; tmp[1] = 0x30; tmp[2] = 0x00; } else { sprintf(tmp,"%x",ch); if ( tmp[1] == 0 ) tmp[1] = 0x30; } } else if ( ((ch&0xF0)<<4) == 0 ) { char cfix = 0x00; sprintf(tmp,"%x",ch); cfix = tmp[0]; tmp[0] = 0x30; tmp[1] = cfix; } else sprintf(tmp,"%x",ch); memcpy(buffer,tmp,3); return buffer; }

更多推荐

用C ++实现RSA密码学

本文发布于:2023-07-05 14:38:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1038434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密码学   RSA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!