/// <summary> /// Encrypt a plain text message /// </summary> /// /// <param name="Input">The plain text</param> /// /// <returns>The cipher text</returns> /// /// <exception cref="CryptoAsymmetricException">Thrown if cipher has not been initialized, or input text is too long</exception> public byte[] Encrypt(byte[] Input) { if (!_isInitialized) { throw new CryptoAsymmetricException("RLWEEncrypt:Encrypt", "The cipher has not been initialized!", new InvalidOperationException()); } if (Input.Length > _maxPlainText - _mFp) { throw new CryptoAsymmetricException("RLWEEncrypt:Encrypt", "The input text is too long!", new ArgumentOutOfRangeException()); } if (!_isEncryption) { throw new CryptoAsymmetricSignException("RLWEEncrypt:Encrypt", "The cipher is not initialized for encryption!", new ArgumentException()); } int plen = _N >> 3; if (_N == 512) { NTT512 ntt = new NTT512(_rndEngine); byte[] ptx = new byte[plen]; if (Input.Length < _maxPlainText) { ptx = _rndEngine.GetBytes(plen); Array.Copy(Input, 0, ptx, _mFp, Input.Length); } else { Array.Copy(Input, 0, ptx, 0, Input.Length); } return(ntt.Encrypt((RLWEPublicKey)_asmKey, ptx)); } else { NTT256 ntt = new NTT256(_rndEngine); byte[] ptx = new byte[plen]; if (Input.Length < _maxPlainText) { ptx = _rndEngine.GetBytes(plen); Array.Copy(Input, 0, ptx, _mFp, Input.Length); } else { Array.Copy(Input, 0, ptx, 0, Input.Length); } return(ntt.Encrypt((RLWEPublicKey)_asmKey, ptx)); } }
/// <summary> /// Decrypt a cipher text /// </summary> /// /// <param name="Input">The cipher text</param> /// /// <returns>The plain text</returns> /// /// <exception cref="RLWEException">Thrown if cipher has not been initialized</exception> public byte[] Decrypt(byte[] Input) { if (!_isInitialized) { throw new RLWEException("RLWEEncrypt:Decrypt", "The cipher has not been initialized!", new InvalidOperationException()); } int plen = _N >> 3; if (_N == 512) { NTT512 ntt = new NTT512(_rndEngine); return(ntt.Decrypt((RLWEPrivateKey)_keyPair.PrivateKey, Input).SubArray(_mFp, plen - _mFp)); } else { NTT256 ntt = new NTT256(_rndEngine); return(ntt.Decrypt((RLWEPrivateKey)_keyPair.PrivateKey, Input).SubArray(_mFp, plen - _mFp)); } }
/// <summary> /// Decrypt a cipher text /// </summary> /// /// <param name="Input">The cipher text</param> /// /// <returns>The plain text</returns> /// /// <exception cref="CryptoAsymmetricException">Thrown if cipher has not been initialized</exception> public byte[] Decrypt(byte[] Input) { if (!_isInitialized) { throw new CryptoAsymmetricException("RLWEEncrypt:Decrypt", "The cipher has not been initialized!", new InvalidOperationException()); } if (_isEncryption) { throw new CryptoAsymmetricSignException("RLWEEncrypt:Decrypt", "The cipher is not initialized for decryption!", new ArgumentException()); } int plen = _N >> 3; if (_N == 512) { NTT512 ntt = new NTT512(_rndEngine); return(ntt.Decrypt((RLWEPrivateKey)_asmKey, Input).SubArray(_mFp, plen - _mFp)); } else { NTT256 ntt = new NTT256(_rndEngine); return(ntt.Decrypt((RLWEPrivateKey)_asmKey, Input).SubArray(_mFp, plen - _mFp)); } }