Beispiel #1
0
 /// <summary>
 /// Decrypt text with a given password
 /// </summary>
 /// <param name="encryptedText">Encrypted text in Base64 format that was encrypted with one of the Encode (or compatible) methods</param>
 /// <param name="password">Password that was used to encrypt the text</param>
 /// <returns>Plain text</returns>
 public static string Decode(string encryptedText, string password)
 {
     var key = makeKey(password);
     byte[] encryptedBytes = CryptoUtils.ToBytes(encryptedText, _defaultStringFormat);
     byte[] plainBytes = Decode(encryptedBytes, password);
     return CryptoUtils.FromBytes(plainBytes, StringFormat.Unicode);
 }
Beispiel #2
0
 /// <summary>
 /// Encrypt object with a given password.  Returns string in Base64 format.  (Internally use RijindaelManaged encryptor)
 /// </summary>
 /// <param name="objectToEncrypt">Serializable object to encrypt</param>
 /// <param name="password">Password will be used to encrypt the text</param>
 /// <returns>Encrypted text in Base64 format</returns>
 public static string Encode(object objectToEncrypt, string password)
 {
     var key = makeKey(password);
     byte[] plainBytes = Serialize.To.Binary(objectToEncrypt);
     byte[] encryptedBytes = Encode(plainBytes, password);
     return CryptoUtils.FromBytes(encryptedBytes, _defaultStringFormat);
 }
Beispiel #3
0
 /// <summary>
 /// Calculate a unique hash code (Base64) for a piece of non-null text. (Uses SHA256 by default)
 /// </summary>
 /// <param name="plainText">Text to use for calculating hash code</param>
 /// <param name="hashSalt">(optional) Additional text that you want to use to add addtional 'secrecy' to your hash.  If provided uses HMAC256 instead of SHA256</param>
 /// <returns>Base64 encoded string of hash.  The value is suitable for storing in databases but might need to be HtmlEncoded if used in query strings or displayed on web pages</returns>
 public static string Hash(string plainText, string hashSalt = null)
 {
     byte[] plainBytes = Encoding.Unicode.GetBytes(plainText);
     byte[] saltBytes  = null;
     if (hashSalt != null)
     {
         saltBytes = Encoding.Unicode.GetBytes(hashSalt);
     }
     byte[] hashBytes = Hash(plainBytes, saltBytes);
     return(CryptoUtils.FromBytes(hashBytes, _defaultStringFormat));
 }
Beispiel #4
0
            /// <summary>
            /// Calculate a unique hash (Base64) code for a non-null object. (Uses SHA256 by default)
            /// </summary>
            /// <param name="objectToHash">Binary serializable object upon which to calculate hash</param>
            /// <param name="hashSalt">(optional) Additional text that you want to use to add addtional 'secrecy' to your hash.  If provided uses HMAC256 instead of SHA256</param>
            /// <returns>Base64 encoded string of hash.  The value is suitable for storing in databases but might need to be HtmlEncoded if used in query strings or displayed on web pages</returns>
            public static string Hash(object objectToHash, string hashSalt = null)
            {
                byte[] plainBytes = Serialize.To.Binary(objectToHash);
                byte[] saltBytes  = null;
                if (hashSalt != null)
                {
                    saltBytes = Encoding.Unicode.GetBytes(hashSalt);
                }
                byte[] hashBytes = Hash(plainBytes, saltBytes);

                return(CryptoUtils.FromBytes(hashBytes, _defaultStringFormat));
            }
Beispiel #5
0
 /// <summary>
 /// Encrypt text with a given password.  Returns string in Base64 format.  (Internally use RijindaelManaged encryptor)
 /// </summary>
 /// <param name="plainText">Text that will be encrypted</param>
 /// <param name="password">Password will be used to encrypt the text</param>
 /// <returns>Encrypted text in Base64 format</returns>
 public static string Encode(string plainText, string password)
 {
     byte[] plainBytes     = CryptoUtils.ToBytes(plainText, StringFormat.Unicode);
     byte[] encryptedBytes = Encode(plainBytes, password);
     return(CryptoUtils.FromBytes(encryptedBytes, _defaultStringFormat));
 }