/// <summary> /// Encrypt a plain text message /// </summary> /// /// <param name="Input">The plain text</param> /// /// <returns>The cipher text</returns> public byte[] Encrypt(byte[] Input) { if (!_isEncryption) throw new CryptoAsymmetricSignException("FujisakiCipher:Encrypt", "The cipher is not initialized for encryption!", new ArgumentException()); // generate random vector r of length k bits GF2Vector r = new GF2Vector(_K, _rndEngine); // convert r to byte array byte[] rBytes = r.GetEncoded(); // compute (r||input) byte[] rm = ByteUtils.Concatenate(rBytes, Input); // compute H(r||input) _dgtEngine.BlockUpdate(rm, 0, rm.Length); byte[] hrm = new byte[_dgtEngine.DigestSize]; _dgtEngine.DoFinal(hrm, 0); // convert H(r||input) to error vector z GF2Vector z = CCA2Conversions.Encode(_N, _T, hrm); // compute c1 = E(r, z) byte[] c1 = CCA2Primitives.Encrypt((MPKCPublicKey)_asmKey, r, z).GetEncoded(); byte[] c2; // get PRNG object using (KDF2Drbg sr0 = new KDF2Drbg(GetDigest(_cprParams.Digest))) { // seed PRNG with r' sr0.Initialize(rBytes); // generate random c2 c2 = new byte[Input.Length]; sr0.Generate(c2); } // XOR with input for (int i = 0; i < Input.Length; i++) c2[i] ^= Input[i]; // return (c1||c2) return ByteUtils.Concatenate(c1, c2); }
public byte[] Encrypt(byte[] Input) { if (!_isEncryption) throw new CryptoAsymmetricSignException("PointchevalCipher:Encrypt", "The cipher is not initialized for encryption!", new ArgumentException()); int kDiv8 = _K >> 3; // generate random r of length k div 8 bytes byte[] r = new byte[kDiv8]; _rndEngine.GetBytes(r); // generate random vector r' of length k bits GF2Vector rPrime = new GF2Vector(_K, _rndEngine); // convert r' to byte array byte[] rPrimeBytes = rPrime.GetEncoded(); // compute (input||r) byte[] mr = ByteUtils.Concatenate(Input, r); // compute H(input||r) _dgtEngine.BlockUpdate(mr, 0, mr.Length); byte[] hmr = new byte[_dgtEngine.DigestSize]; _dgtEngine.DoFinal(hmr, 0); // convert H(input||r) to error vector z GF2Vector z = CCA2Conversions.Encode(_N, _T, hmr); // compute c1 = E(rPrime, z) byte[] c1 = CCA2Primitives.Encrypt((MPKCPublicKey)_asmKey, rPrime, z).GetEncoded(); byte[] c2; // get PRNG object using (KDF2Drbg sr0 = new KDF2Drbg(GetDigest(_cprParams.Digest))) { // seed PRNG with r' sr0.Initialize(rPrimeBytes); // generate random c2 c2 = new byte[Input.Length + kDiv8]; sr0.Generate(c2); } // XOR with input for (int i = 0; i < Input.Length; i++) c2[i] ^= Input[i]; // XOR with r for (int i = 0; i < kDiv8; i++) c2[Input.Length + i] ^= r[i]; // return (c1||c2) return ByteUtils.Concatenate(c1, c2); }