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 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(); }
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!"); } } }
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(); }
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")); } }