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

📄 validat2.cpp

📁 一个DES,RSA,MD5,RC4等加密算法的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
bool XTRDHValidate()
{
	cout << "\nXTR-DH validation suite running...\n\n";

	FileSource f("xtrdh171.dat", true, new HexDecoder());
	XTR_DH dh(f);
	return SimpleKeyAgreementValidate(dh);
}

bool ElGamalValidate()
{
	cout << "\nElGamal validation suite running...\n\n";
	bool pass = true;
	{
		FileSource fc("elgc2048.dat", true, new HexDecoder);
		ElGamalDecryptor privC(fc);
		ElGamalEncryptor pubC(privC);
		privC.Precompute();
		ByteQueue queue;
		privC.SavePrecomputation(queue);
		pubC.LoadPrecomputation(queue);

		pass = CryptoSystemValidate(privC, pubC) && pass;
	}
	{
		LC_RNG rng(4780);
		cout << "Generating new encryption key..." << endl;
		ElGamalDecryptor privC(rng, 128);
		ElGamalEncryptor pubC(privC);

		pass = CryptoSystemValidate(privC, pubC) && pass;
	}
	return pass;
}

bool NRValidate()
{
	cout << "\nNR validation suite running...\n\n";
	bool pass = true;
	{
		FileSource f("nr2048.dat", true, new HexDecoder);
		NRSigner<SHA> privS(f);
		privS.Precompute();
		NRVerifier<SHA> pubS(privS);

		pass = SignatureValidate(privS, pubS) && pass;
	}
	{
		LC_RNG rng(4781);
		cout << "Generating new signature key..." << endl;
		NRSigner<SHA> privS(rng, 256);
		NRVerifier<SHA> pubS(privS);

		pass = SignatureValidate(privS, pubS) && pass;
	}
	return pass;
}

bool DSAValidate()
{
	cout << "\nDSA validation suite running...\n\n";

	bool pass = true, fail;
	{
	FileSource fs("dsa512.dat", true, new HexDecoder());
	DSAPrivateKey priv(fs);
	priv.Precompute(16);
	DSAPublicKey pub(priv);

	byte seed[]={0xd5, 0x01, 0x4e, 0x4b, 0x60, 0xef, 0x2b, 0xa8, 0xb6, 0x21, 
				 0x1b, 0x40, 0x62, 0xba, 0x32, 0x24, 0xe0, 0x42, 0x7d, 0xd3};
	Integer k("358dad57 1462710f 50e254cf 1a376b2b deaadfbfh");
	Integer h("a9993e36 4706816a ba3e2571 7850c26c 9cd0d89dh");
	byte sig[]={0x8b, 0xac, 0x1a, 0xb6, 0x64, 0x10, 0x43, 0x5c, 0xb7, 0x18,
				0x1f, 0x95, 0xb1, 0x6a, 0xb9, 0x7c, 0x92, 0xb3, 0x41, 0xc0, 
				0x41, 0xe2, 0x34, 0x5f, 0x1f, 0x56, 0xdf, 0x24, 0x58, 0xf4, 
				0x26, 0xd1, 0x55, 0xb4, 0xba, 0x2d, 0xb6, 0xdc, 0xd8, 0xc8};
	Integer r(sig, 20);
	Integer s(sig+20, 20);

	Integer pGen, qGen, rOut, sOut;
	int c;

	fail = !GenerateDSAPrimes(seed, 160, c, pGen, 512, qGen);
	fail = fail || (pGen != pub.GetModulus()) || (qGen != pub.GetSubgroupSize());
	pass = pass && !fail;

	cout << (fail ? "FAILED    " : "passed    ");
	cout << "prime generation test\n";

	priv.RawSign(k, h, rOut, sOut);
	fail = (rOut != r) || (sOut != s);
	pass = pass && !fail;

	cout << (fail ? "FAILED    " : "passed    ");
	cout << "signature check against test vector\n";

	fail = !pub.VerifyMessage((byte *)"abc", 3, sig);
	pass = pass && !fail;

	cout << (fail ? "FAILED    " : "passed    ");
	cout << "verification check against test vector\n";

	fail = pub.VerifyMessage((byte *)"xyz", 3, sig);
	pass = pass && !fail;
	}
	FileSource fs1("dsa1024.dat", true, new HexDecoder());
	DSAPrivateKey priv(fs1);
	FileSource fs2("dsa1024b.dat", true, new HexDecoder());
	DSAPublicKey pub(fs2);
	pass = SignatureValidate(priv, pub) && pass;
	return pass;
}

bool LUCValidate()
{
	cout << "\nLUC validation suite running...\n\n";
	bool pass=true;

	{
		FileSource f("luc512.dat", true, new HexDecoder);
		LUCSSA_PKCS1v15_SHA_Signer priv(f);
		LUCSSA_PKCS1v15_SHA_Verifier pub(priv);
		pass = SignatureValidate(priv, pub) && pass;
	}
	{
		FileSource f("luc512.dat", true, new HexDecoder);
		LUCES_OAEP_SHA_Decryptor priv(f);
		LUCES_OAEP_SHA_Encryptor pub(priv);
		pass = CryptoSystemValidate(priv, pub) && pass;
	}
	return pass;
}

bool LUCELGValidate()
{
	cout << "\nLUCELG validation suite running...\n\n";

	FileSource f("lucs512.dat", true, new HexDecoder);
	LUCELG_Signer<SHA> privS(f);
	LUCELG_Verifier<SHA> pubS(privS);

	bool pass = SignatureValidate(privS, pubS);

	FileSource fc("lucc512.dat", true, new HexDecoder);
	LUCELG_Decryptor privC(fc);
	LUCELG_Encryptor pubC(privC);

	pass = CryptoSystemValidate(privC, pubC) && pass;

	return pass;
}

bool RabinValidate()
{
	cout << "\nRabin validation suite running...\n\n";
	bool pass=true;

	{
		FileSource f("rabi512.dat", true, new HexDecoder);
		RabinSignerWith(SHA) priv(f);
		RabinVerifierWith(SHA) pub(priv);
		pass = SignatureValidate(priv, pub) && pass;
	}
	{
		FileSource f("rabi512.dat", true, new HexDecoder);
		RabinDecryptor priv(f);
		RabinEncryptor pub(priv);
		pass = CryptoSystemValidate(priv, pub) && pass;
	}
	return pass;
}

bool RWValidate()
{
	cout << "\nRW validation suite running...\n\n";

	FileSource f("rw512.dat", true, new HexDecoder);
	RWSigner<SHA> priv(f);
	RWVerifier<SHA> pub(priv);

	return SignatureValidate(priv, pub);
}

bool BlumGoldwasserValidate()
{
	cout << "\nBlumGoldwasser validation suite running...\n\n";

	FileSource f("blum512.dat", true, new HexDecoder);
	BlumGoldwasserPrivateKey priv(f);
	BlumGoldwasserPublicKey pub(priv);

	return CryptoSystemValidate(priv, pub);
}

bool ECPValidate()
{
	cout << "\nECP validation suite running...\n\n";

	LC_RNG rng(5665);
	ECDecryptor<ECP> cpriv(rng, ASN1::secp192r1());
	ECEncryptor<ECP> cpub(cpriv);
	ByteQueue bq;
	cpriv.DEREncode(bq);
	cpub.SetEncodeAsOID(true);
	cpub.DEREncode(bq);
	ECSigner<ECP, SHA> spriv(bq);
	ECVerifier<ECP, SHA> spub(bq);
	ECDHC<ECP> ecdhc(ASN1::secp192r1());
	ECMQVC<ECP> ecmqvc(ASN1::secp192r1());

	spriv.Precompute();
	ByteQueue queue;
	spriv.SavePrecomputation(queue);
	spub.LoadPrecomputation(queue);

	bool pass = SignatureValidate(spriv, spub);
	pass = CryptoSystemValidate(cpriv, cpub) && pass;
	pass = SimpleKeyAgreementValidate(ecdhc) && pass;
	pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;

	cout << "Turning on point compression..." << endl;
	cpriv.SetPointCompression(true);
	cpub.SetPointCompression(true);
	ecdhc.SetPointCompression(true);
	ecmqvc.SetPointCompression(true);
	pass = CryptoSystemValidate(cpriv, cpub) && pass;
	pass = SimpleKeyAgreementValidate(ecdhc) && pass;
	pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;

	cout << "Testing SEC 2 recommended curves..." << endl;
	OID oid;
	while (!(oid = ECParameters<ECP>::GetNextRecommendedParametersOID(oid)).m_values.empty())
	{
		ECParameters<ECP> params(oid);
		bool fail = !params.ValidateParameters(rng);
		cout << (fail ? "FAILED" : "passed") << "    " << dec << params.GetCurve().GetField().MaxElementBitLength() << " bits" << endl;
		pass = pass && !fail;
	}

	return pass;
}

bool EC2NValidate()
{
	cout << "\nEC2N validation suite running...\n\n";

	LC_RNG rng(5667);
	ECDecryptor<EC2N> cpriv(rng, ASN1::sect193r1());
	ECEncryptor<EC2N> cpub(cpriv);
	ByteQueue bq;
	cpriv.DEREncode(bq);
	cpub.SetEncodeAsOID(true);
	cpub.DEREncode(bq);
	ECSigner<EC2N, SHA> spriv(bq);
	ECVerifier<EC2N, SHA> spub(bq);
	ECDHC<EC2N> ecdhc(ASN1::sect193r1());
	ECMQVC<EC2N> ecmqvc(ASN1::sect193r1());

	spriv.Precompute();
	ByteQueue queue;
	spriv.SavePrecomputation(queue);
	spub.LoadPrecomputation(queue);

	bool pass = SignatureValidate(spriv, spub);
	pass = CryptoSystemValidate(cpriv, cpub) && pass;
	pass = SimpleKeyAgreementValidate(ecdhc) && pass;
	pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;

	cout << "Turning on point compression..." << endl;
	cpriv.SetPointCompression(true);
	cpub.SetPointCompression(true);
	ecdhc.SetPointCompression(true);
	ecmqvc.SetPointCompression(true);
	pass = CryptoSystemValidate(cpriv, cpub) && pass;
	pass = SimpleKeyAgreementValidate(ecdhc) && pass;
	pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;

#if 0	// TODO: turn this back on when I make EC2N faster for pentanomial basis
	cout << "Testing SEC 2 recommended curves..." << endl;
	OID oid;
	while (!(oid = ECParameters<EC2N>::GetNextRecommendedParametersOID(oid)).m_values.empty())
	{
		ECParameters<EC2N> params(oid);
		bool fail = !params.ValidateParameters(rng);
		cout << (fail ? "FAILED" : "passed") << "    " << params.GetCurve().GetField().MaxElementBitLength() << " bits" << endl;
		pass = pass && !fail;
	}
#endif

	return pass;
}

bool ECDSAValidate()
{
	cout << "\nECDSA validation suite running...\n\n";

	// from Sample Test Vectors for P1363
	GF2NT gf2n(191, 9, 0);
	byte a[]="\x28\x66\x53\x7B\x67\x67\x52\x63\x6A\x68\xF5\x65\x54\xE1\x26\x40\x27\x6B\x64\x9E\xF7\x52\x62\x67";
	byte b[]="\x2E\x45\xEF\x57\x1F\x00\x78\x6F\x67\xB0\x08\x1B\x94\x95\xA3\xD9\x54\x62\xF5\xDE\x0A\xA1\x85\xEC";
	EC2N ec(gf2n, PolynomialMod2(a,24), PolynomialMod2(b,24));

	EC2N::Point P;
	ec.DecodePoint(P, (byte *)"\x04\x36\xB3\xDA\xF8\xA2\x32\x06\xF9\xC4\xF2\x99\xD7\xB2\x1A\x9C\x36\x91\x37\xF2\xC8\x4A\xE1\xAA\x0D"
		"\x76\x5B\xE7\x34\x33\xB3\xF9\x5E\x33\x29\x32\xE7\x0E\xA2\x45\xCA\x24\x18\xEA\x0E\xF9\x80\x18\xFB", ec.EncodedPointSize());
	Integer n("40000000000000000000000004a20e90c39067c893bbb9a5H");
	Integer d("340562e1dda332f9d2aec168249b5696ee39d0ed4d03760fH");
	EC2N::Point Q(ec.Multiply(d, P));
	ECSigner<EC2N, SHA, ECDSA> priv(ec, P, n, Q, d);
	ECVerifier<EC2N, SHA, ECDSA> pub(priv);

	Integer h("A9993E364706816ABA3E25717850C26C9CD0D89DH");
	Integer k("3eeace72b4919d991738d521879f787cb590aff8189d2b69H");
	byte sig[]="\x03\x8e\x5a\x11\xfb\x55\xe4\xc6\x54\x71\xdc\xd4\x99\x84\x52\xb1\xe0\x2d\x8a\xf7\x09\x9b\xb9\x30"
		"\x0c\x9a\x08\xc3\x44\x68\xc2\x44\xb4\xe5\xd6\xb2\x1b\x3c\x68\x36\x28\x07\x41\x60\x20\x32\x8b\x6e";
	Integer r(sig, 24);
	Integer s(sig+24, 24);

	Integer rOut, sOut;
	bool fail, pass=true;

	priv.RawSign(k, h, rOut, sOut);
	fail = (rOut != r) || (sOut != s);
	pass = pass && !fail;

	cout << (fail ? "FAILED    " : "passed    ");
	cout << "signature check against test vector\n";

	fail = !pub.VerifyMessage((byte *)"abc", 3, sig);
	pass = pass && !fail;

	cout << (fail ? "FAILED    " : "passed    ");
	cout << "verification check against test vector\n";

	fail = pub.VerifyMessage((byte *)"xyz", 3, sig);
	pass = pass && !fail;

	pass = SignatureValidate(priv, pub) && pass;

	return pass;
}

⌨️ 快捷键说明

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