Пример #1
0
        private void TestSign()
        {
            RLWEParameters     mpar  = RLWEParamSets.RLWEN512Q12289;
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(mpar);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            using (RLWESign sgn = new RLWESign(mpar))
            {
                sgn.Initialize(akp.PublicKey);

                int    sz   = sgn.MaxPlainText;
                byte[] data = new byte[200];
                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"));
            }
        }
Пример #2
0
        //signs a message using the internal private and public keys
        public string SignMessage(string message)
        {
            RLWEParameters mpar      = RLWEParamSets.RLWEN512Q12289;
            string         signature = "";

            using (RLWESign sgn = new RLWESign(mpar))
            {
                sgn.Initialize(_privateKey.Key);
                byte[] messageBytes = Encoding.UTF8.GetBytes(message);

                byte[] signatureBytes = sgn.Sign(messageBytes, 0, messageBytes.Length);
                signature = Convert.ToBase64String(signatureBytes);
            }

            return(signature);
        }
Пример #3
0
        //verify provided message with the signature and public key provided
        public bool VerifySignature(string message, string signature, IPublicKey publicKey)
        {
            bool result = false;

            RLWEParameters mpar = RLWEParamSets.RLWEN512Q12289;

            using (RLWESign sgn = new RLWESign(mpar))
            {
                sgn.Initialize(((PublicKey)publicKey).Key);
                byte[] messageBytes   = Encoding.UTF8.GetBytes(message);
                byte[] signatureBytes = Convert.FromBase64String(signature);

                result = sgn.Verify(messageBytes, 0, messageBytes.Length, signatureBytes);
            }

            return(result);
        }
Пример #4
0
        //signs a message using the private and public keys provided
        public string SignMessage(string message, IPrivateKey privateKey, IPublicKey publicKey = null)
        {
            //NB: public key isn't required by this implementation, so is initialized to null
            RLWEParameters mpar      = RLWEParamSets.RLWEN512Q12289;
            string         signature = "";

            using (RLWESign sgn = new RLWESign(mpar))
            {
                sgn.Initialize(((PrivateKey)(privateKey)).Key);
                byte[] messageBytes = Encoding.UTF8.GetBytes(message);

                byte[] signatureBytes = sgn.Sign(messageBytes, 0, messageBytes.Length);
                signature = Convert.ToBase64String(signatureBytes);
            }

            return(signature);
        }