/// <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)); }
/// <summary> /// Encrypt a message /// </summary> /// /// <param name="PublicKey">The RLWE public key</param> /// <param name="Message">The message array to encrypt</param> /// /// <returns>The encrypted message</returns> public byte[] Encrypt(RLWEPublicKey PublicKey, byte[] Message) { uint[] lmsg = new uint[M]; uint[] cpt1 = new uint[M]; uint[] cpt2 = new uint[M]; // bit encoding lmsg = Encode(Message); // reverse msg BitReverse(lmsg); // pub a + p, message m, ciphertest c1 + c2 RLWEEncrypt(Convert8To32(PublicKey.A), cpt1, cpt2, lmsg, Convert8To32(PublicKey.P)); return(ArrayUtils.Concat(Convert32To8(cpt1), Convert32To8(cpt2))); }
//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); }
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(); }
/// <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); }
/// <summary> /// Encrypt a message /// </summary> /// /// <param name="PublicKey">The RLWE public key</param> /// <param name="Message">The message array to encrypt</param> /// /// <returns>The encrypted message</returns> public byte[] Encrypt(RLWEPublicKey PublicKey, byte[] Message) { uint[] lmsg = new uint[M]; uint[] cpt1 = new uint[M]; uint[] cpt2 = new uint[M]; // bit encoding lmsg = Encode(Message); // reverse msg BitReverse(lmsg); // pub a + p, message m, ciphertest c1 + c2 RLWEEncrypt(Convert8To32(PublicKey.A), cpt1, cpt2, lmsg, Convert8To32(PublicKey.P)); return ArrayUtils.Concat(Convert32To8(cpt1), Convert32To8(cpt2)); }
//constructor for the private key object using the Byte array provided public PrivateKey(byte[] value = null) { _keyValue = RLWEPublicKey.From(value); }
//constructor for the private key object (internally a "public key") public PrivateKey(RLWEPublicKey privateKey) { _keyValue = privateKey; }