예제 #1
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;
        }
예제 #2
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.CSPRng().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"));
            }
        }
예제 #3
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;
        }
예제 #4
0
파일: DtmKex.cs 프로젝트: modulexcite/CEX
        /// <summary>
        /// Get the asymmetric generator instance
        /// </summary>
        /// 
        /// <param name="Parameters">The cipher parameters</param>
        /// 
        /// <returns>The generator instance</returns>
        private IAsymmetricGenerator GetAsymmetricGenerator(IAsymmetricParameters Parameters)
        {
            IAsymmetricGenerator gen = null;

            try
            {
                if (Parameters.GetType().Equals(typeof(NTRUParameters)))
                    gen = new NTRUKeyGenerator((NTRUParameters)Parameters);
                else if (Parameters.GetType().Equals(typeof(MPKCParameters)))
                    gen = new MPKCKeyGenerator((MPKCParameters)Parameters);
                else if (Parameters.GetType().Equals(typeof(RLWEParameters)))
                    gen = new RLWEKeyGenerator((RLWEParameters)Parameters);

                return gen;
            }
            catch (Exception ex)
            {
                throw new CryptoProcessingException("DtmKex:GetAsymmetricGenerator", "The generator could not be loaded!", ex);
            }
        }
예제 #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;
        }
예제 #6
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!");
            }
        }
        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 CSPRng().GetBytes(data);

                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!");
                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.CSPRng().GetBytes(data);

                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!");
                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.CSPRng().GetBytes(data);

                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!");
                OnProgress(new TestEventArgs("Passed Pointcheval encryption test"));
            }
        }
        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 CSPRng().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 (!Compare.AreEqual(dec, data))
                throw new Exception("TestKey test: decryption failure!");
            OnProgress(new TestEventArgs("Passed sub-key test"));
        }
예제 #9
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!");
            }
            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!");
            }
            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!");
            }
            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!");
            }
            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.CSPRng().GetBytes(data);

                enc = mpe.Encrypt(data);

                mpe.Initialize(akp.PrivateKey);
                byte[] dec = mpe.Decrypt(enc);

                if (!Compare.AreEqual(dec, data))
                    throw new Exception("EncryptionKey: decryption failure!");
                OnProgress(new TestEventArgs("Passed encryption test"));
            }

            pri.Dispose();
            pub.Dispose();
        }