Esempio n. 1
0
        /// <summary>
        /// Mapping between AsymmetricEncryptionPadding enum representation and string name
        /// </summary>
        /// <param name="asymmetricEncryptionPadding">AsymmetricEncryptionPadding enum, padding name</param>
        /// <param name="error">Error type for error management</param>
        /// <returns>string name of asymmetricEncryptionPadding</returns>
        public static string valueOf(AsymmetricEncryptionPadding asymmetricEncryptionPadding, Error error)
        {
            switch (asymmetricEncryptionPadding)
            {
            case AsymmetricEncryptionPadding.NOPADDING:
                return("NOPADDING");

            case AsymmetricEncryptionPadding.OAEPPADDING:
                return("OAEPPADDING");

            case AsymmetricEncryptionPadding.PCKS1PADDING:
                return("PCKS1PADDING");

            case AsymmetricEncryptionPadding.ISO97961PADDING:
                return("ISO97961PADDING");

            default:
                error.setError("AE004", "Unrecognized AsymmetricEncryptionPadding");
                return("");
            }
        }
        /********EXTERNAL OBJECT PUBLIC METHODS  - END ********/


        /// <summary>
        /// Encrypts the string encoded plain text
        /// </summary>
        /// <param name="asymmetricEncryptionAlgorithm">string AsymmetricEncryptionAlgorithm enum, algorithm name</param>
        /// <param name="hashAlgorithm">string HashAlgorithm enum, algorithm name</param>
        /// <param name="asymmetricEncryptionPadding">string AsymmetricEncryptionPadding enum, padding name</param>
        /// <param name="keyPath">string path to key/certificate</param>
        /// <param name="isPrivate">boolean true if key is private, false if it is public</param>
        /// <param name="alias">string keystore/certificate pkcs12 format alias</param>
        /// <param name="password">Srting keysore/certificate pkcs12 format alias</param>
        /// <param name="plainText">string to encrypt</param>
        /// <returns>string Base64 encrypted plainText text</returns>
        private string DoEncryptInternal(string hashAlgorithm, string asymmetricEncryptionPadding, Key key, bool isPrivate, string plainText)
        {
            this.error.cleanError();

            HashAlgorithm hash = HashAlgorithmUtils.getHashAlgorithm(hashAlgorithm, this.error);
            AsymmetricEncryptionPadding padding = AsymmetricEncryptionPaddingUtils.getAsymmetricEncryptionPadding(asymmetricEncryptionPadding, this.error);

            if (this.error.existsError())
            {
                return("");
            }

            string asymmetricEncryptionAlgorithm = "";
            AsymmetricKeyParameter asymKey       = null;

            if (isPrivate)
            {
                PrivateKeyManager keyMan = (PrivateKeyManager)key;
                if (!keyMan.HasPrivateKey || keyMan.HasError())
                {
                    this.error = keyMan.GetError();
                    return("");
                }
                asymmetricEncryptionAlgorithm = keyMan.getPrivateKeyAlgorithm();

                asymKey = keyMan.getPrivateKeyParameterForEncryption();
                if (keyMan.HasError())
                {
                    this.error = keyMan.GetError();
                    return("");
                }
            }
            else
            {
                CertificateX509 cert = (CertificateX509)key;
                if (!cert.Inicialized || cert.HasError())
                {
                    this.error = cert.GetError();
                    return("");
                }
                asymmetricEncryptionAlgorithm = cert.getPublicKeyAlgorithm();
                asymKey = cert.getPublicKeyParameterForEncryption();
                if (cert.HasError())
                {
                    this.error = cert.GetError();
                    return("");
                }
            }

            AsymmetricEncryptionAlgorithm algorithm = AsymmetricEncryptionAlgorithmUtils
                                                      .getAsymmetricEncryptionAlgorithm(asymmetricEncryptionAlgorithm, this.error);

            try
            {
                return(doEncrypt(algorithm, hash, padding, asymKey, plainText));
            }
            catch (InvalidCipherTextException)
            {
                this.error.setError("AE036", "Algoritmo inválido" + algorithm);

                return("");
            }
        }
        /// <summary>
        /// Buils Asymmetric Block Cipher engine
        /// </summary>
        /// <param name="asymBlockCipher">AsymmetricBlockCipher enum, algorithm name</param>
        /// <param name="hash">Digest Engine for hashing</param>
        /// <param name="asymmetricEncryptionPadding">AsymmetricEncryptionPadding enum, padding name</param>
        /// <returns>AsymmetricBlockCipher Engine specific for the algoritm, hash and padding</returns>
        private IAsymmetricBlockCipher getPadding(IAsymmetricBlockCipher asymBlockCipher, IDigest hash, AsymmetricEncryptionPadding asymmetricEncryptionPadding)
        {
            switch (asymmetricEncryptionPadding)
            {
            case AsymmetricEncryptionPadding.NOPADDING:
                return(null);

            case AsymmetricEncryptionPadding.OAEPPADDING:
                if (hash != null)
                {
                    return(new OaepEncoding(asymBlockCipher, hash));
                }
                else
                {
                    return(new OaepEncoding(asymBlockCipher));
                }

            case AsymmetricEncryptionPadding.PCKS1PADDING:
                return(new Pkcs1Encoding(asymBlockCipher));

            case AsymmetricEncryptionPadding.ISO97961PADDING:
                return(new ISO9796d1Encoding(asymBlockCipher));

            default:
                error.setError("AE044", "Unrecognized AsymmetricEncryptionPadding");
                return(null);
            }
        }
        /// <summary>
        /// Encrypts the string encoded plain text
        /// </summary>
        /// <param name="asymmetricEncryptionAlgorithm"></param>
        /// <param name="hashAlgorithm"></param>
        /// <param name="asymmetricEncryptionPadding"></param>
        /// <param name="asymmetricKeyParameter"></param>
        /// <param name="plainText"></param>
        /// <returns>Base64 encrypted encryptedInput text</returns>
        private string doEncrypt(AsymmetricEncryptionAlgorithm asymmetricEncryptionAlgorithm, HashAlgorithm hashAlgorithm, AsymmetricEncryptionPadding asymmetricEncryptionPadding, AsymmetricKeyParameter asymmetricKeyParameter, string plainText)
        {
            IAsymmetricBlockCipher asymEngine = getEngine(asymmetricEncryptionAlgorithm);
            IDigest hash = getDigest(hashAlgorithm);
            IAsymmetricBlockCipher        cipher         = getPadding(asymEngine, hash, asymmetricEncryptionPadding);
            BufferedAsymmetricBlockCipher bufferedCipher = new BufferedAsymmetricBlockCipher(cipher);

            if (this.error.existsError())
            {
                return("");
            }
            bufferedCipher.Init(true, asymmetricKeyParameter);
            EncodingUtil eu = new EncodingUtil();

            byte[] inputBytes = eu.getBytes(plainText);
            if (eu.GetError().existsError())
            {
                this.error = eu.GetError();
                return("");
            }
            bufferedCipher.ProcessBytes(inputBytes, 0, inputBytes.Length);
            byte[] outputBytes = bufferedCipher.DoFinal();
            if (outputBytes == null || outputBytes.Length == 0)
            {
                this.error.setError("AE041", "Asymmetric encryption error");
                return("");
            }
            this.error.cleanError();
            return(Base64.ToBase64String(outputBytes));
        }
        /// <summary>
        /// Decrypts the base64 encoded encrypted text
        /// </summary>
        /// <param name="asymmetricEncryptionAlgorithm">string AsymmetricEncryptionAlgorithm enum, algorithm name</param>
        /// <param name="hashAlgorithm">string HashAlgorithm enum, algorithm name</param>
        /// <param name="asymmetricEncryptionPadding">string AsymmetricEncryptionPadding enum, padding name</param>
        /// <param name="asymmetricKeyParameter">AsymmetricKeyParameter with loaded key for specified algorithm</param>
        /// <param name="encryptedInput">string Base64 to decrypt</param>
        /// <returns>string decypted encryptedInput text</returns>
        private string doDecrypt(AsymmetricEncryptionAlgorithm asymmetricEncryptionAlgorithm, HashAlgorithm hashAlgorithm, AsymmetricEncryptionPadding asymmetricEncryptionPadding, AsymmetricKeyParameter asymmetricKeyParameter, string encryptedInput)
        {
            IAsymmetricBlockCipher asymEngine = getEngine(asymmetricEncryptionAlgorithm);
            IDigest hash = getDigest(hashAlgorithm);
            IAsymmetricBlockCipher        cipher         = getPadding(asymEngine, hash, asymmetricEncryptionPadding);
            BufferedAsymmetricBlockCipher bufferedCipher = new BufferedAsymmetricBlockCipher(cipher);

            if (this.error.existsError())
            {
                return("");
            }
            bufferedCipher.Init(false, asymmetricKeyParameter);
            byte[] inputBytes = Base64.Decode(encryptedInput);
            bufferedCipher.ProcessBytes(inputBytes, 0, inputBytes.Length);
            byte[] outputBytes = bufferedCipher.DoFinal();
            if (outputBytes == null || outputBytes.Length == 0)
            {
                this.error.setError("AE040", "Asymmetric decryption error");
                return("");
            }
            EncodingUtil eu = new EncodingUtil();

            this.error = eu.GetError();
            return(eu.getString(outputBytes));
        }