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

查看:142
本文介绍了使用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是正确的在他的评论中,通常使用混合加密(一种非对称密码,如RSA,带有对称密码,如AES)。

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天全站免登陆