private void TestParams() { MPKCParameters mpar = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); byte[] enc = mpar.ToBytes(); using (MPKCParameters mpar2 = MPKCParameters.From(enc)) { if (!mpar.Equals(mpar2)) { throw new Exception("EncryptionKey: public key comparison test failed!"); } } OnProgress(new TestEventArgs("Passed parameters byte serialization")); MemoryStream mstr = mpar.ToStream(); using (MPKCParameters mpar2 = MPKCParameters.From(mstr)) { if (!mpar.Equals(mpar2)) { throw new Exception("EncryptionKey: public key comparison test failed!"); } } OnProgress(new TestEventArgs("Passed parameters stream serialization")); }
private void TestKey() { MPKCParameters encParams = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); MPKCKeyGenerator keyGen = new MPKCKeyGenerator(encParams); IAsymmetricKeyPair keyPair = keyGen.GenerateKeyPair(); byte[] enc, dec, data; // encrypt an array using (MPKCEncrypt cipher = new MPKCEncrypt(encParams)) { cipher.Initialize(keyPair.PublicKey); data = new byte[66]; new CSPPrng().GetBytes(data); enc = cipher.Encrypt(data); } // decrypt the cipher text using (MPKCEncrypt cipher = new MPKCEncrypt(encParams)) { cipher.Initialize(keyPair.PrivateKey); dec = cipher.Decrypt(enc); } if (!Evaluate.AreEqual(dec, data)) { throw new Exception("TestKey test: decryption failure!"); } OnProgress(new TestEventArgs("Passed sub-key test")); }
static double Decrypt(int Iterations, MPKCParameters Param) { MPKCKeyGenerator mkgen = new MPKCKeyGenerator(Param); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); byte[] ptext = new CSPRng().GetBytes(64); byte[] rtext = new byte[64]; byte[] ctext; Stopwatch runTimer = new Stopwatch(); using (MPKCEncrypt mpe = new MPKCEncrypt(Param)) { mpe.Initialize(akp.PublicKey); ctext = mpe.Encrypt(ptext); mpe.Initialize(akp.PrivateKey); runTimer.Start(); for (int i = 0; i < Iterations; i++) { rtext = mpe.Decrypt(ctext); } runTimer.Stop(); } //if (!Compare.AreEqual(ptext, rtext)) // throw new Exception("Encryption test: decryption failure!"); return(runTimer.Elapsed.TotalMilliseconds); }
static void KeyGenSpeed(int Iterations) { Console.WriteLine(string.Format("Key creation average time over {0} passes:", Iterations)); Stopwatch runTimer = new Stopwatch(); double elapsed; elapsed = KeyGenerator(Iterations, MPKCParamSets.MPKCFM11T40S256); Console.WriteLine(string.Format("MPKCFM11T40S256: avg. {0} ms", elapsed / Iterations, Iterations)); Console.WriteLine(string.Format("{0} keys created in: {1} ms", Iterations, elapsed)); Console.WriteLine(string.Format("Creation Rate is {0} keys per second", (int)(1000.0 / (elapsed / Iterations)))); Console.WriteLine(""); elapsed = KeyGenerator(Iterations, MPKCParamSets.MPKCFM11T48S256); Console.WriteLine(string.Format("MPKCFM11T48S256: avg. {0} ms", elapsed / Iterations, Iterations)); Console.WriteLine(string.Format("{0} keys created in: {1} ms", Iterations, elapsed)); Console.WriteLine(string.Format("Creation Rate is {0} keys per second", (int)(1000.0 / (elapsed / Iterations)))); Console.WriteLine(""); Iterations = 4; Console.WriteLine(string.Format("Testing each key with {0} passes:", Iterations)); Console.WriteLine(""); foreach (int p in Enum.GetValues(typeof(MPKCParamSets.MPKCParamNames))) { MPKCParameters param = MPKCParamSets.FromName((MPKCParamSets.MPKCParamNames)p); elapsed = KeyGenerator(Iterations, param); Console.WriteLine(string.Format(Enum.GetName(typeof(MPKCParamSets.MPKCParamNames), p) + ": avg. {0} ms", elapsed / Iterations, Iterations)); Console.WriteLine(string.Format("{0} keys created in: {1} ms", Iterations, elapsed)); Console.WriteLine(""); } Console.WriteLine(""); }
/// <summary> /// Initialize this class /// </summary> /// /// <param name="Parameters">The cipher parameters</param> /// <param name="Info">The predefined nonce value</param> public KobaraImaiCipher(MPKCParameters Parameters, byte[] Info = null) { if (Info != null) KobaraImaiCipher.MPKCINFO = Info; _cipherParams = Parameters; _dgtEngine = GetDigest(Parameters.Digest); }
static void Main(string[] args) { MPKCParameters encParams = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); MPKCKeyGenerator keyGen = new MPKCKeyGenerator(encParams); IAsymmetricKeyPair keyPair = keyGen.GenerateKeyPair(); byte[] enc, dec, data; string message = "Привер Валера"; // encrypt an array using (MPKCEncrypt cipher = new MPKCEncrypt(encParams)) { cipher.Initialize(keyPair.PublicKey); data = Encoding.Default.GetBytes(message); enc = cipher.Encrypt(data); } Console.WriteLine("Исходный текст " + message); var b64encStr = Convert.ToBase64String(enc); Console.WriteLine("Зашифрованный текст " + b64encStr); var b64encArr = Convert.FromBase64String(b64encStr); // decrypt the cipher text using (MPKCEncrypt cipher = new MPKCEncrypt(encParams)) { cipher.Initialize(keyPair.PrivateKey); dec = cipher.Decrypt(b64encArr); } Console.WriteLine("Расшифрованный текст " + Encoding.Default.GetString(dec)); Console.ReadKey(); }
private void TestSign() { MPKCParameters mpar = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); MPKCKeyGenerator mkgen = new MPKCKeyGenerator(mpar); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); using (MPKCSign sgn = new MPKCSign(mpar)) { sgn.Initialize(akp.PublicKey); int sz = sgn.MaxPlainText - 1; byte[] data = new byte[320]; new VTDev.Libraries.CEXEngine.Crypto.Prng.CSPPrng().GetBytes(data); byte[] code = sgn.Sign(data, 0, data.Length); sgn.Initialize(akp.PrivateKey); if (!sgn.Verify(data, 0, data.Length, code)) { throw new Exception("EncryptionKey: private key comparison test failed!"); } OnProgress(new TestEventArgs("Passed byte sign and verify")); sgn.Initialize(akp.PublicKey); code = sgn.Sign(new MemoryStream(data)); sgn.Initialize(akp.PrivateKey); if (!sgn.Verify(new MemoryStream(data), code)) { throw new Exception("EncryptionKey: private key comparison test failed!"); } OnProgress(new TestEventArgs("Passed stream sign and verify")); } }
/// <summary> /// Initialize this class /// </summary> /// /// <param name="Parameters">The cipher parameters</param> /// <param name="Info">The predefined nonce value</param> public KobaraImaiCipher(MPKCParameters Parameters, byte[] Info = null) { if (Info != null) { KobaraImaiCipher.MPKCINFO = Info; } _cipherParams = Parameters; _dgtEngine = GetDigest(Parameters.Digest); }
static double KeyGenerator(int Iterations, MPKCParameters Param) { MPKCKeyGenerator mkgen = new MPKCKeyGenerator(Param); IAsymmetricKeyPair akp; Stopwatch runTimer = new Stopwatch(); runTimer.Start(); for (int i = 0; i < Iterations; i++) { akp = mkgen.GenerateKeyPair(); } runTimer.Stop(); return(runTimer.Elapsed.TotalMilliseconds); }
static double Encrypt(int Iterations, MPKCParameters Param) { MPKCKeyGenerator mkgen = new MPKCKeyGenerator(Param); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); byte[] ptext = new CSPRng().GetBytes(64); byte[] ctext; Stopwatch runTimer = new Stopwatch(); using (MPKCEncrypt mpe = new MPKCEncrypt(Param)) { mpe.Initialize(akp.PublicKey); runTimer.Start(); for (int i = 0; i < Iterations; i++) { ctext = mpe.Encrypt(ptext); } runTimer.Stop(); } return(runTimer.Elapsed.TotalMilliseconds); }
static void FullCycle() { MPKCParameters mpar = MPKCParamSets.MPKCFM11T40S256; //APR2011743FAST MPKCKeyGenerator mkgen = new MPKCKeyGenerator(mpar); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); byte[] enc; using (MPKCEncrypt mpe = new MPKCEncrypt(mpar)) { mpe.Initialize(akp.PublicKey); byte[] data = new byte[mpe.MaxPlainText]; enc = mpe.Encrypt(data); mpe.Initialize(akp.PrivateKey); byte[] dec = mpe.Decrypt(enc); if (!Compare.AreEqual(dec, data)) { throw new Exception("Encryption test: decryption failure!"); } } }
public BaseMethods() { encParams = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); keyGen = new MPKCKeyGenerator(encParams); keyPair = keyGen.GenerateKeyPair(); }
private void TestEncrypt() { MPKCParameters mpar = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); MPKCKeyGenerator mkgen = new MPKCKeyGenerator(mpar); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); byte[] enc; // Fujisaki using (MPKCEncrypt mpe = new MPKCEncrypt(mpar)) { mpe.Initialize(akp.PublicKey); int sz = mpe.MaxPlainText - 1; byte[] data = new byte[sz]; new CSPPrng().GetBytes(data); enc = mpe.Encrypt(data); mpe.Initialize(akp.PrivateKey); byte[] dec = mpe.Decrypt(enc); if (!Evaluate.AreEqual(dec, data)) { throw new Exception("Encryption test: decryption failure!"); } OnProgress(new TestEventArgs("Passed Fujisaki encryption test")); } // KobaraLmai using (MPKCEncrypt mpe = new MPKCEncrypt(mpar)) { mpar = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); mpe.Initialize(akp.PublicKey); int sz = mpe.MaxPlainText - 1; byte[] data = new byte[sz]; new VTDev.Libraries.CEXEngine.Crypto.Prng.CSPPrng().GetBytes(data); enc = mpe.Encrypt(data); mpe.Initialize(akp.PrivateKey); byte[] dec = mpe.Decrypt(enc); if (!Evaluate.AreEqual(dec, data)) { throw new Exception("Encryption test: decryption failure!"); } OnProgress(new TestEventArgs("Passed KobaraImai encryption test")); } // Pointcheval using (MPKCEncrypt mpe = new MPKCEncrypt(mpar)) { mpar = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); mpe.Initialize(akp.PublicKey); int sz = mpe.MaxPlainText - 1; byte[] data = new byte[sz]; new VTDev.Libraries.CEXEngine.Crypto.Prng.CSPPrng().GetBytes(data); enc = mpe.Encrypt(data); mpe.Initialize(akp.PrivateKey); byte[] dec = mpe.Decrypt(enc); if (!Evaluate.AreEqual(dec, data)) { throw new Exception("Encryption test: decryption failure!"); } OnProgress(new TestEventArgs("Passed Pointcheval encryption test")); } }
/// <summary> /// Initialize this class /// </summary> /// /// <param name="Paramaters">The cipher parameters</param> public FujisakiCipher(MPKCParameters Paramaters) { _cprParams = Paramaters; _dgtEngine = GetDigest(Paramaters.Digest); }
/// <summary> /// /// </summary> /// /// <param name="Rng"></param> /// <param name="Param"></param> public McElieceKeyGenerationParameters(SecureRandom Rng, MPKCParameters Param) : base(Rng, 256) { // XXX key size? _param = Param; }
private void TestEncode() { MPKCParameters mpar = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy(); MPKCKeyGenerator mkgen = new MPKCKeyGenerator(mpar); IAsymmetricKeyPair akp = mkgen.GenerateKeyPair(); MPKCPublicKey pub = (MPKCPublicKey)akp.PublicKey; byte[] enc = pub.ToBytes(); using (MPKCPublicKey pub2 = MPKCPublicKey.From(enc)) { if (!pub.Equals(pub2)) { throw new Exception("EncryptionKey: public key comparison test failed!"); } if (pub.GetHashCode() != pub2.GetHashCode()) { throw new Exception("EncryptionKey: public key hash test failed!"); } } OnProgress(new TestEventArgs("Passed public key serialization")); MemoryStream pubstr = pub.ToStream(); using (MPKCPublicKey pub2 = MPKCPublicKey.From(pubstr)) { if (!pub.Equals(pub2)) { throw new Exception("EncryptionKey: public key comparison test failed!"); } if (pub.GetHashCode() != pub2.GetHashCode()) { throw new Exception("EncryptionKey: public key hash test failed!"); } } pubstr.Dispose(); OnProgress(new TestEventArgs("Passed public key stream test")); MPKCPrivateKey pri = (MPKCPrivateKey)akp.PrivateKey; enc = pri.ToBytes(); using (MPKCPrivateKey pri2 = new MPKCPrivateKey(enc)) { if (!pri.Equals(pri2)) { throw new Exception("EncryptionKey: private key comparison test failed!"); } if (pri.GetHashCode() != pri2.GetHashCode()) { throw new Exception("EncryptionKey: private key hash test failed!"); } } OnProgress(new TestEventArgs("Passed private key serialization")); MemoryStream pristr = pri.ToStream(); using (MPKCPrivateKey pri2 = MPKCPrivateKey.From(pristr)) { if (!pri.Equals(pri2)) { throw new Exception("EncryptionKey: private key comparison test failed!"); } if (pri.GetHashCode() != pri2.GetHashCode()) { throw new Exception("EncryptionKey: private key hash test failed!"); } } pristr.Dispose(); OnProgress(new TestEventArgs("Passed private key stream test")); using (MPKCEncrypt mpe = new MPKCEncrypt(mpar)) { mpe.Initialize(akp.PublicKey); int sz = mpe.MaxPlainText - 1; byte[] data = new byte[sz]; new VTDev.Libraries.CEXEngine.Crypto.Prng.CSPPrng().GetBytes(data); enc = mpe.Encrypt(data); mpe.Initialize(akp.PrivateKey); byte[] dec = mpe.Decrypt(enc); if (!Evaluate.AreEqual(dec, data)) { throw new Exception("EncryptionKey: decryption failure!"); } OnProgress(new TestEventArgs("Passed encryption test")); } pri.Dispose(); pub.Dispose(); }
/// <summary> /// Initialize this class /// </summary> /// /// <param name="Parameters">The cipher parameters</param> public PointchevalCipher(MPKCParameters Parameters) { m_cprParams = Parameters; m_dgtEngine = GetDigest(Parameters.Digest); }
/// <summary> /// Initialize this class /// </summary> /// /// <param name="Paramaters">The cipher parameters</param> public FujisakiCipher(MPKCParameters Paramaters) { m_cprParams = Paramaters; m_dgtEngine = GetDigest(Paramaters.Digest); }
/// <summary> /// /// </summary> /// /// <param name="Rng"></param> /// <param name="Param"></param> public McElieceKeyGenerationParameters(SecureRandom Rng, MPKCParameters Param) : base(Rng, 256) { // XXX key size? _param = Param; }
/// <summary> /// Initialize this class /// </summary> /// /// <param name="Parameters">The cipher parameters</param> public PointchevalCipher(MPKCParameters Parameters) { _cprParams = Parameters; _dgtEngine = GetDigest(Parameters.Digest); }