Esempio n. 1
0
        public static string DecryptStringRijndaelFromHexString(string originalHexStringToDecrypt)
        {
            string DecryptedString;

            ASCIIEncoding   textConverter = new ASCIIEncoding();
            RijndaelManaged oRijndael     = new RijndaelManaged();
            string          Key1          = "carwash";
            string          Key2          = "andi";

            Byte[] toDecrypte;
            Byte[] key;
            Byte[] IV;

            key = GetRijndaelKey(Key1, textConverter);
            IV  = GetRijndaelIV(Key2, textConverter);

            //get decryptor
            ICryptoTransform decryptor = oRijndael.CreateDecryptor(key, IV);

            try
            {
                //decrypt data
                Byte[] encryted = HexStringHelper.ConvertHexStringToBytes(originalHexStringToDecrypt);

                MemoryStream msDecrypt = new MemoryStream(encryted);
                CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

                //covert data to a byte array.
                toDecrypte = new Byte[encryted.Length - 1];

                //write all data to the crypto stream and flush it.
                csDecrypt.Read(toDecrypte, 0, toDecrypte.Length);

                int nSigCharLength = -1;
                foreach (Byte nByte in toDecrypte)
                {
                    nSigCharLength += 1;
                    if (nByte == 0)
                    {
                        break;
                    }
                }

                //redim preserve
                toDecrypte = toDecrypte.Take(nSigCharLength).ToArray();

                DecryptedString = textConverter.GetString(toDecrypte);
            }
            catch (CryptographicException ex)
            {
                throw new ApplicationException(ex.Message, ex);
            }
            catch (ApplicationException ex)
            {
                throw new ApplicationException(ex.Message, ex);
            }

            return(DecryptedString);
        }
Esempio n. 2
0
        public static string EncryptStringRijndaelToHexString(string StringToEncrypt)
        {
            ASCIIEncoding   textConverter = new ASCIIEncoding();
            RijndaelManaged oRijndael     = new RijndaelManaged();
            string          Key1          = "carwash";
            string          Key2          = "andi";

            Byte[] encrypted;
            Byte[] toEncrypt;
            Byte[] key;
            Byte[] IV;

            //get the Key and IV
            key = GetRijndaelKey(Key1, textConverter);
            IV  = GetRijndaelIV(Key2, textConverter);

            //get an encryptor
            ICryptoTransform encryptor = oRijndael.CreateEncryptor(key, IV);

            //encryp data
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            toEncrypt = textConverter.GetBytes(StringToEncrypt);

            //Write all data to the crypto stream and flush it.
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
            csEncrypt.FlushFinalBlock();

            //Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray();

            StringToEncrypt = HexStringHelper.ConvertBytesToHexString(encrypted);

            return(StringToEncrypt);
        }