2015-04-03 17 views
3

我使用的加密++库:将整数转换回SecByteBlock?

DH dh; 
AutoSeededRandomPool rnd; 
SecByteBlock priv(dh.PrivateKeyLength()); 
SecByteBlock pub(dh.PublicKeyLength()); 

此使用的Diffie-Hellman生成私钥和公钥:

dh.GenerateKeyPair(rnd, priv, pub); 

在这里,我无法读取私人和公共密钥转换为Integer(NUM如123 )

​​

什么是对我来说,转换回从Integer到代码210?

Integer -convert-> SecByteBlock 
+1

”不是个如果从不太安全的类型构建“SecByteBlock”,丢失想法?如果我明白这个名字(这个API严重缺乏信息),那么这个想法就是这个块在安全内存中。当你从不安全的内存中产生它时,很大一部分安全性会丢失。 – 2015-04-04 00:32:52

+2

@MaartenBodewes - 'Integer'也存储基于'SecBlock'类型的数字字节('SecByteBlock'是'SecBlock '的一个typedef)。基于对源代码的粗略阅读,唯一的安全功能是在释放内存时将内存归零;我没有看到它阻止交换到磁盘的证据。例如,我认为面对Heartbleed风格的攻击可能会带来一些好处,但真正缓解OP问题背景下泄漏的关键材料显然是使用HSM而不是软件加密库无论如何。 – softwariness 2015-04-04 03:34:29

+1

@softwariness这是很好的信息,谢谢你。即使我没有HSM躺在家里虽然(不适合我的笔记本电脑无论如何:)) – 2015-04-04 08:50:37

回答

2

提取字节出Integer的,你用MinEncodedSizeEncode方法(see the Crypto++ documentation)。请注意,编码是big-endian。

下面是一个例子方法来包装了(假定该Integer是无符号的,如将适用于这种情况):

void UnsignedIntegerToByteBlock(const Integer& x, SecByteBlock& bytes) 
{ 
    size_t encodedSize = x.MinEncodedSize(Integer::UNSIGNED); 
    bytes.resize(encodedSize); 
    x.Encode(bytes.BytePtr(), encodedSize, Integer::UNSIGNED); 
} 

您可以使用它像这样:

Integer a; 
// ... 
SecByteBlock bbA; 
UnsignedIntegerToByteBlock(a, bbA); 
+0

*“请注意,编码是big-endian”* - 我们开始向Integer类添加*一些* endian支持;看[Integer Patch](http://www.cryptopp.com/wiki/Integer_Patch)。但支持* *并未扩展到各种* Encode *和* Decode *方法。 – jww 2015-04-04 23:00:13