/// <summary>
        /// The McEliece encryption primitive
        /// </summary>
        ///
        /// <param name="PublicKey">The public key</param>
        /// <param name="M">The message vector</param>
        /// <param name="Z">The error vector</param>
        ///
        /// <returns><c>m*G + z</c></returns>
        public static GF2Vector Encrypt(MPKCPublicKey PublicKey, GF2Vector M, GF2Vector Z)
        {
            GF2Matrix matrixG = PublicKey.G;
            Vector    mG      = matrixG.LeftMultiplyLeftCompactForm(M);

            return((GF2Vector)mG.Add(Z));
        }
        /// <summary>
        /// The McEliece encryption primitive
        /// </summary>
        /// 
        /// <param name="PublicKey">The public key</param>
        /// <param name="M">The message vector</param>
        /// <param name="Z">The error vector</param>
        /// 
        /// <returns><c>m*G + z</c></returns>
        public static GF2Vector Encrypt(MPKCPublicKey PublicKey, GF2Vector M, GF2Vector Z)
        {
            GF2Matrix matrixG = PublicKey.G;
            Vector mG = matrixG.LeftMultiplyLeftCompactForm(M);

            return (GF2Vector)mG.Add(Z);
        }
Exemple #3
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();
        }