整数到char*和char*到Integer的转换
Integer to char* and char* to Integer conversion
我正在使用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();
}
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串