Beispiel #1
0
        /// <summary>
        /// Decrypt a ciphertext
        /// </summary>
        ///
        /// <param name="PrivateKey">The RLWE private key</param>
        /// <param name="Message">The encrypted message</param>
        ///
        /// <returns>The decrypted message</returns>
        public byte[] Decrypt(RLWEPrivateKey PrivateKey, byte[] Message)
        {
            uint[] lmsg  = new uint[M];
            uint[] lmsg2 = new uint[M];

            uint[][] cpt = ArrayUtils.Split(Convert8To32(Message), Message.Length / 4);
            RLWEDecrypt(cpt[0], cpt[1], Convert8To32(PrivateKey.R2));
            QBDecode(cpt[0]);
            ArrangeFinal(cpt[0], lmsg2);

            return(Decode(lmsg2));
        }
Beispiel #2
0
        /// <summary>
        /// Generate a RLWE key pair
        /// </summary>
        ///
        /// <returns>An initialized RLWE KeyPair</returns>
        public RLWEKeyPair Generate()
        {
            uint[] pubA  = new uint[M];
            uint[] pubP  = new uint[M];
            uint[] priR2 = new uint[M];

            KeyGen(pubA, pubP, priR2);

            RLWEPrivateKey pri = new RLWEPrivateKey(M, Convert32To8(priR2));
            RLWEPublicKey  pub = new RLWEPublicKey(M, Convert32To8(pubA), Convert32To8(pubP));

            return(new RLWEKeyPair(pub, pri));
        }
Beispiel #3
0
        //Generates a Key pair using the ED25519 algorithms
        //initializes internal public and private keys, which can then be exported as required
        public bool GenerateKeyPair()
        {
            RLWEParameters     mpar    = RLWEParamSets.RLWEN512Q12289;
            RLWEKeyGenerator   mkgen   = new RLWEKeyGenerator(mpar);
            IAsymmetricKeyPair keyPair = mkgen.GenerateKeyPair();

            RLWEPublicKey  pubKey  = (RLWEPublicKey)keyPair.PublicKey;
            RLWEPrivateKey privKey = (RLWEPrivateKey)keyPair.PrivateKey;

            //Private and public keys appear reversed in this signing inplementation
            _privateKey = new PrivateKey((RLWEPublicKey)keyPair.PublicKey);
            _publicKey  = new PublicKey((RLWEPrivateKey)keyPair.PrivateKey);

            return(true);
        }
Beispiel #4
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();
        }
Beispiel #5
0
        /// <summary>
        /// Generate a RLWE key pair
        /// </summary>
        /// 
        /// <returns>An initialized RLWE KeyPair</returns>
        public RLWEKeyPair Generate()
        {
            uint[] pubA = new uint[M];
            uint[] pubP = new uint[M];
            uint[] priR2 = new uint[M];

            KeyGen(pubA, pubP, priR2);

            RLWEPrivateKey pri = new RLWEPrivateKey(M, Convert32To8(priR2));
            RLWEPublicKey pub = new RLWEPublicKey(M, Convert32To8(pubA), Convert32To8(pubP));

            return new RLWEKeyPair(pub, pri);
        }
Beispiel #6
0
        /// <summary>
        /// Decrypt a ciphertext
        /// </summary>
        /// 
        /// <param name="PrivateKey">The RLWE private key</param>
        /// <param name="Message">The encrypted message</param>
        /// 
        /// <returns>The decrypted message</returns>
        public byte[] Decrypt(RLWEPrivateKey PrivateKey, byte[] Message)
        {
            uint[] lmsg = new uint[M];
            uint[] lmsg2 = new uint[M];

            uint[][] cpt = ArrayUtils.Split(Convert8To32(Message), Message.Length / 4);
            RLWEDecrypt(cpt[0], cpt[1], Convert8To32(PrivateKey.R2));
            QBDecode(cpt[0]);
            ArrangeFinal(cpt[0], lmsg2);

            return Decode(lmsg2);
        }
Beispiel #7
0
 //Sets internal public key using byte array provided
 public PublicKey(byte[] value = null)
 {
     _keyValue = RLWEPrivateKey.From(value);
 }
Beispiel #8
0
 //Constructor
 public PublicKey(RLWEPrivateKey publicKey)
 {
     _keyValue = publicKey;
 }