Exemplo n.º 1
0
        /// <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 CryptoAsymmetricException("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);
            }
        }