⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dh.cpp

📁 各种加密算法的集合
💻 CPP
字号:

#include "pch.h" 
#include "dh.h" 
#include "asn.h" 
#include "nbtheory.h" 
 
NAMESPACE_BEGIN(CryptoPP) 
 
DH::DH(const Integer &p, const Integer &g) 
	: p(p), g(g), gpc(p, g, ExponentBitLength(), 1) 
{ 
} 
 
DH::DH(RandomNumberGenerator &rng, unsigned int pbits) 
{ 
	PrimeAndGenerator pg(1, rng, pbits); 
	p = pg.Prime(); 
	g = pg.Generator(); 
	gpc.Precompute(p, g, ExponentBitLength(), 1); 
} 
 
DH::DH(BufferedTransformation &bt) 
{ 
	BERSequenceDecoder seq(bt); 
	p.BERDecode(seq); 
	g.BERDecode(seq); 
	gpc.Precompute(p, g, ExponentBitLength(), 1); 
} 
 
void DH::DEREncode(BufferedTransformation &bt) const 
{ 
	DERSequenceEncoder seq(bt); 
	p.DEREncode(seq); 
	g.DEREncode(seq); 
} 
 
void DH::Precompute(unsigned int precomputationStorage) 
{ 
	gpc.Precompute(p, g, ExponentBitLength(), precomputationStorage); 
} 
 
void DH::LoadPrecomputation(BufferedTransformation &bt) 
{ 
	gpc.Load(p, bt); 
} 
 
void DH::SavePrecomputation(BufferedTransformation &bt) const 
{ 
	gpc.Save(bt); 
} 
 
bool DH::ValidateDomainParameters(RandomNumberGenerator &rng) const 
{ 
	return VerifyPrime(rng, p) && VerifyPrime(rng, (p-1)/2) && g > 1 && g < p && Jacobi(g, p) == 1; 
} 
 
void DH::GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const 
{ 
	Integer x(rng, ExponentBitLength()); 
	Integer y = gpc.Exponentiate(x); 
	x.Encode(privateKey, PrivateKeyLength()); 
	y.Encode(publicKey, PublicKeyLength()); 
} 
 
bool DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const 
{ 
	Integer w(otherPublicKey, PublicKeyLength()); 
	if (validateOtherPublicKey && !(w > 1 && w < p && Jacobi(w, p) == 1)) 
		return false; 
 
	Integer s(privateKey, PrivateKeyLength()); 
	Integer z = a_exp_b_mod_c(w, s, p); 
	z.Encode(agreedValue, AgreedValueLength()); 
	return true; 
} 
 
unsigned int DH::ExponentBitLength() const 
{ 
	return 2*DiscreteLogWorkFactor(p.BitCount()); 
} 
 
NAMESPACE_END 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -