永发信息网

帮忙解释crypto中的RSA算法,分全给你

答案:1  悬赏:50  手机版
解决时间 2021-02-06 16:35
  • 提问者网友:呐年旧曙光
  • 2021-02-05 21:52
void main()

{

char priKey[128] = {0};

char pubKey[128] = {0};

char seed[1024] = {0};

// 生成 RSA 密钥对

strcpy(priKey, "pri"); // 生成的私钥文件

strcpy(pubKey, "pub"); // 生成的公钥文件

strcpy(seed, "seed");

GenerateRSAKey(1024, priKey, pubKey, seed);

// RSA 加解密

char message[1024] = {0};

cout<<"Origin Text:\t"<<"Hello World!"<
strcpy(message, "Hello World!");

string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密

cout<<"Encrypted Text:\t"<
string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA 解密

cout<<"Decrypted Text:\t"<
string a;

cin >> a;
}

//------------------------

// 生成RSA密钥对

//------------------------

void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed)

{

RandomPool randPool;

randPool.Put((byte *)seed, strlen(seed));

RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);

HexEncoder privFile(new FileSink(privFilename));

priv.DEREncode(privFile);

privFile.MessageEnd();

RSAES_OAEP_SHA_Encryptor pub(priv);

HexEncoder pubFile(new FileSink(pubFilename));

pub.DEREncode(pubFile);

pubFile.MessageEnd();

}

//------------------------

// RSA加密

//------------------------

string RSAEncryptString(const char *pubFilename, const char *seed, const char *message)

{

FileSource pubFile(pubFilename, true, new HexDecoder);

RSAES_OAEP_SHA_Encryptor pub(pubFile);

RandomPool randPool;

randPool.Put((byte *)seed, strlen(seed));

string result;

StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));

return result;

}

//------------------------

// RSA解密

//------------------------

string RSADecryptString(const char *privFilename, const char *ciphertext)

{

FileSource privFile(privFilename, true, new HexDecoder);

RSAES_OAEP_SHA_Decryptor priv(privFile);

string result;

StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));

return result;

}

//------------------------

// 定义全局的随机数池

//------------------------

RandomPool & GlobalRNG()

{
static RandomPool randomPool;
return randomPool;
}

我的意思是具体每一步骤的含义,没让你解释RSA的概念
最佳答案
  • 五星知识达人网友:拾荒鲤
  • 2021-02-05 21:57
没学过,等有时件漫漫看.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯