整数到char*和char*到Integer的转换

Integer to char* and char* to Integer conversion

本文关键字:char 转换 整数 Integer      更新时间:2023-10-16

我正在使用Crypto++库,我创建了这个简单的类:

class EncoderRSA
{
    public:
        EncoderRSA();
        void keyGeneration();
        void substitutePublicKey(Integer e, Integer n);
        Integer encode(std::string plainText);
        std::string decode(Integer cypher);
    private:
        AutoSeededRandomPool prng;  // Pseudo Random Number Generator
        RSA::PublicKey publicKey;   // For encrypt plain text
        RSA::PrivateKey privateKey; // For decrypt plain text
};

但是我有一些问题:当我使用EncoderRSA::encode(message)编码我的消息时,我想将其从Integer转换为char*(用于套接字发送),从char*转换为Integer(在另一边接收后)。我该怎么做呢?

从整数到char*和从char*到整数的转换

我将回答标题中的问题,并回避正文的问题,因为这似乎是加密,而不是编码

Integer to char*

很容易得到char*,因为Integer类(头,实现)重载operator<<

那么,你可以这样做:

#include <iostream>
#include <sstream>
#include <string>
#include "integer.h"
...
using std::cout;
using std::endl;
using std::string;
using CryptoPP::Integer;
// Perhaps this is received from the EncoderRSA::encode() function
Integer n = ...;
ostringstream oss;
oss << n;
// Create this temporary; otherwise, you might get yourself into trouble
string str(oss.str());
cout << "string: " << str << endl;
cout << "char*: " << str.c_str() << endl;

char* to Integer

这很简单,因为Integer类有它的构造函数。From integer.h:

// convert from string (requires NULL terminator)
Integer (const char *str)

程序如下:

#include <string>
#include "integer.h"
...
using std::cout;
using std::endl;
using std::string;
using CryptoPP::Integer;
// Read from the wire
const char data[] = "12345678901234567890123456789012345678901234567890123456789012345678901234567890.";
// Perhaps this is passed to the EncoderRSA::decode() function
Integer n(data, strlen(data));

如果你想要byte数组版本,那么…

Integer to byte*

#include <vector>
#include "integer.h"
...
using std::vector;
using CryptoPP::Integer;
// Perhaps this is received from the EncoderRSA::encode() function
Integer n = ...;
size_t sz = n.MinEncodedSize();
vector v;
v.resize(sz);
n.Encode(&v[0], v.size());

byte* to Integer

这很简单,因为Integer类有它的构造函数。From integer.h:

// convert from string (does NOT require NULL terminator)
Decode (const byte *input, size_t inputLen, Signedness=UNSIGNED)

程序如下:

#include "integer.h"
...
using CryptoPP::Integer;
// Read from the wire; pretend they are binary
const char byte[] = "12345678901234567890123456789012345678901234567890123456789012345678901234567890";
// Perhaps this is passed to the EncoderRSA::decode() function
Integer n;
n.decode(data, sizeof(data));

编码和解码可以变得更花哨。例如,这里有一个程序,hexencocodes对它们进行编码:

#include <iostream>
#include <string>
#include "integer.h"
...
using std::cout;
using std::endl;
using std::string;
using CryptoPP::Integer;
// Perhaps this is received from the EncoderRSA::encode() function
Integer n("0xfedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321");
string encoded;
size_t req = n.MinEncodedSize();
encoded.reserve(req);
HexEncoder encoder(new StringSink(encoded));
n.Encode(encoder, req);
cout << encoded << endl;

它产生:

$ ./cryptopp-test.exe 
FEDCBA0987654321FEDCBA0987654321FEDCBA0987654321FEDCBA0987654321

你可以切换到Base64编码器:

Base64Encoder encoder(new StringSink(encoded));
n.Encode(encoder, req);
cout << encoded << endl;

它产生:

$ ./cryptopp-test.exe 
/ty6CYdlQyH+3LoJh2VDIf7cugmHZUMh/ty6CYdl

解决这个问题有以下两种方法:

Integer EncryptorRSA::stringToInteger(std::string str) {
    Integer integer(str.c_str());
    return integer;
}
std::string EncryptorRSA::integerToString(Integer integer) {
    std::stringstream stream;
    stream << std::hex << integer;
    return stream.str();
}