Exemplo n.º 1
0
        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"));
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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"));
        }
Exemplo n.º 4
0
        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();
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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!");
                }
            }
        }
Exemplo n.º 8
0
        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();
        }
Exemplo n.º 9
0
 public BaseMethods()
 {
     encParams = (MPKCParameters)MPKCParamSets.MPKCFM11T40S256.DeepCopy();
     keyGen    = new MPKCKeyGenerator(encParams);
     keyPair   = keyGen.GenerateKeyPair();
 }
Exemplo n.º 10
0
        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"));
            }
        }