在Crypto ++中使用原始RSA算法对消息进行加密和解密? [英] Encrypt and Decrypt a message using raw RSA algorithm in Crypto++?

查看:239
本文介绍了在Crypto ++中使用原始RSA算法对消息进行加密和解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Crypto ++ 库来进行与密码学相关的工作。任务的子部分是加密和解密文本。该消息最多可以包含256个字符,包含字母数字数字点和特殊字符。

I am using Crypto++ library for cryptography related works. And sub-part of task is to encrypt and decrypt a text. The message can be up to 256 character long containing alphanumeric number spaces dot and special characters.

此代码段适用于文本长度小于或等于8的字符。但是之后,它无法解密加密的文本。

This piece of code is working for text length is less than or equal to 8. But after that it fails to decrypt the encrypted text.

// g++ -std=c++1y crypto.cpp -I /home/shravan40/cryptopp/build -lcryptopp

#include <iostream>

#include <cryptopp/rsa.h>
#include <cryptopp/integer.h>
#include <cryptopp/osrng.h>

int main(){
    // Keys
    CryptoPP::Integer n("0xbeaadb3d839f3b5f"), e("0x11"), d("0x21a5ae37b9959db9");

    CryptoPP::RSA::PrivateKey privKey;
    privKey.Initialize(n, e, d);

    CryptoPP::RSA::PublicKey pubKey;
    pubKey.Initialize(n, e);

    // Encryption
    std::string message = "Shravan Kumar";
    CryptoPP::Integer m((const byte *)message.data(), message.size());
    std::cout << "m: " << m << std::endl;
    // m: 126879297332596.

    CryptoPP::Integer c = pubKey.ApplyFunction(m);
    std::cout << "c: " << std::hex << c << std::endl;
    // c: 3f47c32e8e17e291h

    // Decryption
    CryptoPP::AutoSeededRandomPool prng;
    CryptoPP::Integer r = privKey.CalculateInverse(prng, c);
    std::cout << "r: " << std::hex << r << std::endl;

    // r: 736563726574h
    std::string recovered;
    recovered.resize(r.MinEncodedSize());

    r.Encode((byte *)recovered.data(), recovered.size());
    std::cout << "recovered: " << recovered << std::endl;

    // recovered: Expected : (Shravan Kumar), Received -> y5��dqm0
    return 0;
}


推荐答案

Richard Critten是正确的 通常使用混合加密(

Richard Critten is correct in his comment that usually hybrid encryption is used (an asymmetric cipher such as RSA with a symmetric cipher such as AES).

对于这类不安全的示例,尽管通常只要求您执行以下操作:将明文分成与模数 n 相同的大小。因此,在您的情况下,只需将每8个字节/字符放在一起,并将其用于(大字节序)数字。由于输入似乎是ASCII,因此这8个字节的最高位将始终设置为零,因此加密/解密应该没有任何问题。 RSA的输入当然必须始终小于 n

For these kind of insecure examples though you are usually simply required to split up the plaintext into parts the same size as the modulus n. So in your case just put every 8 bytes / characters together and use it for a (big endian) number. As the input seems to be ASCII the highest bit of those 8 bytes will always be set to zero, so you should not have any problems encrypting/decrypting. The input for RSA must of course always be less than n.

您当然必须考虑一个

注意:


  • 如果尚未告知(尚未):没有填充的原始RSA也不安全。因此,如果此示例将在现场实现,则不仅仅是密钥大小会成为问题。

  • 我不知道您在解密方面正在做什么。我想你应该再看看你的教科书。

这篇关于在Crypto ++中使用原始RSA算法对消息进行加密和解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆