Exemplo n.º 1
0
        public string EncryptData(string data, Algorithm.StringTransformationFormat inputStringEncode, Algorithm.Cryptography cryptographyAlgorithm, Algorithm.StringEncodeFormat outputStringEncode)
        {
            string encryptText = string.Empty;

            if (!string.IsNullOrEmpty(data))
            {
                byte[] cipherText = null;
                try
                {
                    BinaryEncodeHelper encodeHelper = new BinaryEncodeHelper(Algorithm.StringTransformationFormat.UTF8);
                    byte[]             dataBinary   = encodeHelper.Decode(data);

                    switch (outputStringEncode)
                    {
                    case Algorithm.StringEncodeFormat.Base32:
                    {
                        encodeHelper.SetEncodeFormat(Algorithm.StringTransformationFormat.Base32);
                        break;
                    }

                    case Algorithm.StringEncodeFormat.Base64:
                    {
                        encodeHelper.SetEncodeFormat(Algorithm.StringTransformationFormat.Base64);
                        break;
                    }
                    }
                    switch (cryptographyAlgorithm)
                    {
                    case Algorithm.Cryptography.AES:
                    {
                        AESCryptography aes = new AESCryptography();
                        cipherText = aes.EncryptData(dataBinary);
                        m_Password = encodeHelper.Encode(aes.Password);
                        m_Salt     = encodeHelper.Encode(aes.Salt);
                        break;
                    }

                    case Algorithm.Cryptography.RSA:
                    {
                        RSACryptography rsa = new RSACryptography();
                        cipherText = rsa.EncryptData(dataBinary);
                        BinaryEncodeHelper encodeLittleHelper = new BinaryEncodeHelper(Algorithm.StringTransformationFormat.UTF8);
                        m_Password = encodeHelper.Encode(encodeLittleHelper.Decode(rsa.PublicKey));
                        break;
                    }
                    }

                    if (cipherText != null)
                    {
                        encryptText = encodeHelper.Encode(cipherText);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return(encryptText);
        }
Exemplo n.º 2
0
        public string DecryptData(string encryptText, Algorithm.StringEncodeFormat inputStringEncode, Algorithm.Cryptography cryptographyAlgorithm, Algorithm.StringTransformationFormat outputStringEncode)
        {
            string data = string.Empty;

            if (!string.IsNullOrEmpty(encryptText))
            {
                byte[] cipherText = null;
                byte[] password   = null;
                byte[] salt       = null;
                try
                {
                    BinaryEncodeHelper encodeHelper = new BinaryEncodeHelper(Algorithm.StringTransformationFormat.Base64);
                    switch (inputStringEncode)
                    {
                    case Algorithm.StringEncodeFormat.Base32:
                    {
                        encodeHelper.SetEncodeFormat(Algorithm.StringTransformationFormat.Base32);
                        break;
                    }

                    case Algorithm.StringEncodeFormat.Base64:
                    {
                        encodeHelper.SetEncodeFormat(Algorithm.StringTransformationFormat.Base64);
                        break;
                    }
                    }
                    cipherText = encodeHelper.Decode(encryptText);
                    if (!string.IsNullOrEmpty(m_Password))
                    {
                        password = encodeHelper.Decode(m_Password);
                    }
                    if (!string.IsNullOrEmpty(m_Salt))
                    {
                        salt = encodeHelper.Decode(m_Salt);
                    }

                    byte[] text = null;
                    switch (cryptographyAlgorithm)
                    {
                    case Algorithm.Cryptography.AES:
                    {
                        if (cipherText != null && password != null && salt != null)
                        {
                            AESCryptography aes = new AESCryptography(password, salt);
                            text = aes.DecryptData(cipherText);
                        }
                        break;
                    }

                    case Algorithm.Cryptography.RSA:
                    {
                        RSACryptography rsa = new RSACryptography();
                        text = rsa.DecryptData(cipherText);
                        break;
                    }
                    }
                    if (text != null)
                    {
                        encodeHelper.SetEncodeFormat(outputStringEncode);
                        data = encodeHelper.Encode(text);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return(data);
        }