I'm making program in C++ that works with vector<uint8_t> and uses Crypto++ library. I separate my vector to small ones and trying to convert it to Integer, but sometimes small vector block contains of zeros at the begining, so when i convert back from Integer to vector<uint8_t>, this vector does not contain this zeros. Here is the code:
void Test(vector<uint8_t> data)
{
vector<uint8_t> cipher;
size_t blockSize = _keySize / 8;
for (size_t i = 0; i < data.size(); i += blockSize)
{
size_t blockLength = min(blockSize, data.size() - i);
vector<uint8_t> block(data.begin() + i, data.begin() + i + blockLength);
Integer m;
m.Decode(block.data(), block.size());
size_t Size = m.MinEncodedSize();
vector<uint8_t> encrypt(Size);
m.Encode(encrypt.data(), encrypt.size());
if (encrypt != block)
{
cout << i << " " << unsigned(block[0]) << endl;
}
}
}
In this code _keySize is 1024, and data contains 140000 elements. Here is the output:
11264 0
17280 0
29952 0
53632 0
57728 0
63104 0
75392 0
137216 0
138880 0
I was trying to use OpenSSL library, but has same mistake.
This answer is based on assumptions as I'm not familiar with Crypto++ library; still it is plausible at least:
Integersounds pretty much like an arbitrary size integer type as known from other big-integer libraries.I'm drawing now a parallel to standard input/output streams:
On
Decodeing data the string is read and some internal representation constructed from. It's pretty likely that this one is not based on the smallest possible integer type (signed or unsignedchar), but rather on the largest one the underlying CPU supports natively (i.e. the one corresponding to the latter's register size).This corresponds to reading some value from console into a variable:
You can now pass in
1,01, or any arbitrary number of leading zeros (well, not accounting for the fact that a leading zero switches by default to octal number format...). When you now output the variable again:you simply get
1as output, all the leading zeros dropped.Analogously: Why should some big-integer type produce leading zeros on creating a string representation of its internal value?
At least it won't by default if you don't tell it. Provided this is possible at all then do so, check the API for if such function exists and how it is called. This would correspond to:
to get
0001printed.If you discover no such API exists then you need to do by hand; start as following:
If my assumptions are correct you should now get the desired results.