Esempio n. 1
0
        /// <summary>
        /// Initialize the cipher.
        /// <para>Requires a <see cref="NTRUPublicKey"/> for encryption, or a <see cref="NTRUPrivateKey"/> for decryption</para>
        /// </summary>
        ///
        /// <param name="Encryption">When true cipher is for encryption, if false, decryption</param>
        /// <param name="KeyPair">The <see cref="IAsymmetricKeyPair"/> containing the NTRU public or private key</param>
        ///
        /// <exception cref="NTRUException">Thrown if a key is invalid</exception>
        public void Initialize(bool Encryption, IAsymmetricKeyPair KeyPair)
        {
            if (!(KeyPair is NTRUKeyPair))
            {
                throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());
            }

            _keyPair = (NTRUKeyPair)KeyPair;

            if (_keyPair.PublicKey == null)
            {
                throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());
            }
            if (!(_keyPair.PublicKey is NTRUPublicKey))
            {
                throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());
            }

            if (!Encryption)
            {
                if (_keyPair.PrivateKey == null)
                {
                    throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());
                }
                if (!(_keyPair.PrivateKey is NTRUPrivateKey))
                {
                    throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());
                }
            }

            _isInitialized = true;
        }
Esempio n. 2
0
        /// <summary>
        /// Compare this object instance with another
        /// </summary>
        ///
        /// <param name="obj">Object to compare</param>
        ///
        /// <returns>True if equal, otherwise false</returns>
        public override bool Equals(Object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }

            NTRUKeyPair other = (NTRUKeyPair)obj;

            if (PrivateKey == null)
            {
                if (other.PrivateKey != null)
                {
                    return(false);
                }
            }
            else if (!PrivateKey.Equals(other.PrivateKey))
            {
                return(false);
            }
            if (PublicKey == null)
            {
                if (other.PublicKey != null)
                {
                    return(false);
                }
            }
            else if (!PublicKey.Equals(other.PublicKey))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Initialize the cipher.
        /// <para>Requires a <see cref="NTRUPublicKey"/> for encryption, or a <see cref="NTRUPrivateKey"/> for decryption</para>
        /// </summary>
        /// 
        /// <param name="Encryption">When true cipher is for encryption, if false, decryption</param>
        /// <param name="KeyPair">The <see cref="IAsymmetricKeyPair"/> containing the NTRU public or private key</param>
        /// 
        /// <exception cref="NTRUException">Thrown if a key is invalid</exception>
        public void Initialize(bool Encryption, IAsymmetricKeyPair KeyPair)
        {
            if (!(KeyPair is NTRUKeyPair))
                throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());

            _keyPair = (NTRUKeyPair)KeyPair;

            if (_keyPair.PublicKey == null)
                throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());
            if (!(_keyPair.PublicKey is NTRUPublicKey))
                throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());

            if (!Encryption)
            {
                if (_keyPair.PrivateKey == null)
                    throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());
                if (!(_keyPair.PrivateKey is NTRUPrivateKey))
                    throw new NTRUException("NTRUEncrypt:Initialize", "Not a valid NTRU key pair!", new InvalidDataException());
            }

            _isInitialized = true;
        }