Example #1
0
        public static string DecryptRijndael(string ivEncrypted, string key, out bool candecrypt)
        {
            AesCryptoServiceProvider aes = null;

            candecrypt = false;
            string encrypted = ivEncrypted.Length >= GX_AJAX_PRIVATE_IV.Length ? ivEncrypted.Substring(GX_AJAX_PRIVATE_IV.Length) : ivEncrypted;

            try
            {
                int    discarded      = 0;
                byte[] encryptedBytes = HexEncoding.GetBytes(encrypted, out discarded);
                if (encryptedBytes.Length > 0)
                {
                    byte[] keyBytes = HexEncoding.GetBytes(key, out discarded);
                    byte[] ivBytes  = HexEncoding.GetBytes(GX_AJAX_PRIVATE_IV, out discarded);
                    aes         = new AesCryptoServiceProvider();             //CBC Mode
                    aes.IV      = ivBytes;
                    aes.Key     = keyBytes;
                    aes.Padding = PaddingMode.Zeros;
                    MemoryStream memoryStream;
                    using (memoryStream = new MemoryStream(encryptedBytes))
                    {
                        CryptoStream cryptoStream;
                        using (cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                        }
                    }
                    string decrypted = Encoding.ASCII.GetString(memoryStream.ToArray());
                    int    zeroIdx   = decrypted.IndexOf(NULL_CHARACTER);
                    if (zeroIdx != -1)
                    {
                        decrypted = decrypted.Substring(0, zeroIdx);
                    }
                    candecrypt = true;
                    return(decrypted);
                }
            }
            catch (Exception) { }
            finally
            {
                if (aes != null)
                {
                    aes.Clear();
                }
            }
            return(encrypted);
        }
Example #2
0
        public static string EncryptRijndael(string decrypted, string key)
        {
            AesCryptoServiceProvider aes = null;
            string encrypted             = null;

            try
            {
                int    discarded      = 0;
                byte[] decryptedBytes = Encoding.ASCII.GetBytes(decrypted);
                byte[] keyBytes       = HexEncoding.GetBytes(key, out discarded);
                byte[] ivBytes        = HexEncoding.GetBytes(GX_AJAX_PRIVATE_IV, out discarded);
                aes         = new AesCryptoServiceProvider();         //CBC Mode
                aes.IV      = ivBytes;
                aes.Key     = keyBytes;
                aes.Padding = PaddingMode.Zeros;
                MemoryStream memoryStream;
                using (memoryStream = new MemoryStream())
                {
                    CryptoStream cryptoStream;
                    using (cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(decryptedBytes, 0, decryptedBytes.Length);
                        cryptoStream.FlushFinalBlock();
                        encrypted = HexEncoding.ToString(memoryStream.ToArray());
                        int zeroIdx = encrypted.IndexOf(NULL_CHARACTER);
                        if (zeroIdx != -1)
                        {
                            encrypted = encrypted.Substring(0, zeroIdx);
                        }
                    }
                }
                return(encrypted);
            }
            catch (Exception) {
            }
            finally
            {
                if (aes != null)
                {
                    aes.Clear();
                }
            }

            return(decrypted);
        }