Exemplo n.º 1
0
        private void TestEncrypt(RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[] enc;

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);

                int    sz   = mpe.MaxPlainText;
                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(string.Format("Passed N:{0} Q:{1} encryption test", Param.N, Param.Q)));
            }
        }
Exemplo n.º 2
0
        private void TestKey()
        {
            RLWEParameters     encParams = RLWEParamSets.RLWEN256Q7681;
            RLWEKeyGenerator   keyGen    = new RLWEKeyGenerator(encParams);
            IAsymmetricKeyPair keyPair   = keyGen.GenerateKeyPair();

            byte[] enc, dec, data;

            // encrypt an array
            using (RLWEEncrypt cipher = new RLWEEncrypt(encParams))
            {
                cipher.Initialize(keyPair.PublicKey);
                data = new byte[cipher.MaxPlainText];
                new CSPRng().GetBytes(data);
                enc = cipher.Encrypt(data);
            }

            // decrypt the cipher text
            using (RLWEEncrypt cipher = new RLWEEncrypt(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"));
        }
Exemplo n.º 3
0
        static double Decrypt(int Iterations, RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[]    ptext = new CSPRng().GetBytes(Param.N >> 3);
            byte[]    rtext = new byte[Param.N >> 3];
            byte[]    ctext;
            Stopwatch runTimer = new Stopwatch();

            using (RLWEEncrypt mpe = new RLWEEncrypt(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.º 4
0
        static void FullCycle(RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[] enc;

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                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.º 5
0
        static double Encrypt(int Iterations, RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[]    ptext = new CSPRng().GetBytes(Param.N >> 3);
            byte[]    ctext;
            Stopwatch runTimer = new Stopwatch();

            using (RLWEEncrypt mpe = new RLWEEncrypt(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.º 6
0
        private void TestEncode()
        {
            RLWEParameters     mpar  = RLWEParamSets.RLWEN256Q7681;
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(mpar);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            RLWEPublicKey pub = (RLWEPublicKey)akp.PublicKey;

            byte[] enc = pub.ToBytes();
            using (RLWEPublicKey pub2 = RLWEPublicKey.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 (RLWEPublicKey pub2 = RLWEPublicKey.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"));

            RLWEPrivateKey pri = (RLWEPrivateKey)akp.PrivateKey;

            enc = pri.ToBytes();
            using (RLWEPrivateKey pri2 = RLWEPrivateKey.From(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 (RLWEPrivateKey pri2 = RLWEPrivateKey.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 (RLWEEncrypt mpe = new RLWEEncrypt(mpar))
            {
                mpe.Initialize(akp.PublicKey);

                int    sz   = mpe.MaxPlainText;
                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();
        }