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

时间:2015-09-17 20:21:58

标签: c++ type-conversion rsa crypto++

我正在使用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*(对于套接字发送)和{{1} } char*(在另一方收到后)。我该怎么办?

2 个答案:

答案 0 :(得分:1)

  

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

我将回答标题中的问题,然后选择正文的问题,因为它似乎 加密 ,而不是 编码

Integerchar*

很容易获得char*,因为Integer类(headerimplementation)重载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*Integer

这很简单,因为Integer类有一个构造函数。来自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数组版本,那么......

Integerbyte*

#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*Integer

这很简单,因为Integer类有一个构造函数。来自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));

您可以通过编码和解码获得更好的体验。例如,这是HexEncodes编码的程序:

#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

答案 1 :(得分:0)

接下来的两个方法是解决这个问题:

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();
}