コード例 #1
0
        public static string DecryptString(string cryptedValue, KeyVector keyVector)
        {
            var retVal = string.Empty;

            if (cryptedValue.HasValue())
            {
                MemoryStream ms = null;
                CryptoStream cs = null;

                try
                {
                    byte[] buffer = Convert.FromBase64String(cryptedValue);

                    var rv = new RijndaelManaged();
                    ms = new MemoryStream(buffer);

                    cs = new CryptoStream(
                        ms,
                        rv.CreateDecryptor(keyVector.Key, keyVector.Vector),
                        CryptoStreamMode.Read
                    );

                    var sr = new StreamReader(cs);
                    retVal = sr.ReadToEnd();
                }
                catch (Exception ex)
                {
                    string message = string.Format("Base.Cryptography.EncryptDecryptHelper: DecryptString(): {0}", ex.GetFriendlyMessage());
                    throw new Exception(message);
                }
                finally
                {
                    if (ms != null)
                    {
                        ms.Close();
                    }
                    if (cs != null)
                    {
                        cs.Close();
                    }
                }
            }

            return retVal;
        }
コード例 #2
0
        public static string EncryptString(string plainValue, KeyVector keyVector)
        {
            var retVal = string.Empty;

            if (plainValue.HasValue())
            {
                var ms = new MemoryStream();
                CryptoStream cs = null;

                try
                {
                    var rv = new RijndaelManaged();
                    cs = new CryptoStream(
                        ms,
                        rv.CreateEncryptor(keyVector.Key, keyVector.Vector),
                        CryptoStreamMode.Write
                    );

                    var sw = new StreamWriter(cs);
                    sw.Write(plainValue);
                    sw.Flush();
                    cs.FlushFinalBlock();
                    ms.Flush();

                    // convert back to a string
                    retVal = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
                    sw.Close();
                }
                catch (Exception ex)
                {
                    string message = string.Format("Base.Cryptography.EncryptDecryptHelper: EncryptString(): {0}", ex.GetFriendlyMessage());
                    throw new Exception(message);
                }
                finally
                {
                    if (ms != null)
                    {
                        ms.Close();
                    }
                    if (cs != null)
                    {
                        cs.Close();
                    }
                }
            }

            return retVal;
        }