整数到 char* 和 char* 到整数的转换
Integer to char* and char* to Integer conversion
我正在使用 Crypto++ 库,我创建了这个简单的 class:
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
(在另一端接收之后)。我该怎么做?
Integer to char* and char* to Integer conversion
我将回答标题中的问题,并回避 body 的问题,因为 encrypt,而不是 encode.
Integer
至 char*
很容易得到 char*
因为 Integer
class (header, implementation) 重载了 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
class 有一个构造函数。来自 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
至 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*
至 Integer
这很简单,因为 Integer
class 有一个构造函数。来自 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
解决此问题的方法有以下两种:
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();
}
我正在使用 Crypto++ 库,我创建了这个简单的 class:
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
(在另一端接收之后)。我该怎么做?
Integer to char* and char* to Integer conversion
我将回答标题中的问题,并回避 body 的问题,因为 encrypt,而不是 encode.
Integer
至 char*
很容易得到 char*
因为 Integer
class (header, implementation) 重载了 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
class 有一个构造函数。来自 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
至 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*
至 Integer
这很简单,因为 Integer
class 有一个构造函数。来自 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
解决此问题的方法有以下两种:
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();
}