public static string DecodePassword(string password, EPasswordFormat passwordFormat, string passwordSalt) { var retval = string.Empty; if (passwordFormat == EPasswordFormat.Clear) { retval = password; } else if (passwordFormat == EPasswordFormat.Hashed) { throw new Exception("can not decode hashed password"); } else if (passwordFormat == EPasswordFormat.Encrypted) { var encryptor = new DesEncryptor { InputString = password, DecryptKey = passwordSalt }; encryptor.DesDecrypt(); retval = encryptor.OutString; } return(retval); }
public override void Configure(IDictionary <string, string> settings) { string conn = settings[Environment.ConnectionString]; DesEncryptor des = new DesEncryptor(); settings[Environment.ConnectionString] = des.Decrypt(conn); base.Configure(settings); }
public void Core_Security_Cryptography_DesEncryptor() { var rawData = "Hello, I am raw"; var encryptor = new DesEncryptor(rawData); var encrypted = encryptor.Encrypt(rawData); Assert.IsTrue(rawData != encrypted); Assert.IsTrue(encryptor.Decrypt(encrypted) == rawData); }
public static string Decrypt(string inputString) { if (string.IsNullOrEmpty(inputString)) return string.Empty; var encryptor = new DesEncryptor { InputString = inputString, DecryptKey = "TgQQk42O" }; encryptor.DesDecrypt(); return encryptor.OutString; }
private static string Decrypt(string inputString) { if (string.IsNullOrEmpty(inputString)) { return(string.Empty); } var encryptor = new DesEncryptor { InputString = inputString, DecryptKey = "TgQQk42O" }; encryptor.DesDecrypt(); return(encryptor.OutString); }
public static string EncodePassword(string password, EPasswordFormat passwordFormat, out string passwordSalt) { var retval = string.Empty; passwordSalt = string.Empty; if (passwordFormat == EPasswordFormat.Clear) { retval = password; } else if (passwordFormat == EPasswordFormat.Hashed) { passwordSalt = GenerateSalt(); var src = Encoding.Unicode.GetBytes(password); var buffer2 = Convert.FromBase64String(passwordSalt); var dst = new byte[buffer2.Length + src.Length]; Buffer.BlockCopy(buffer2, 0, dst, 0, buffer2.Length); Buffer.BlockCopy(src, 0, dst, buffer2.Length, src.Length); var algorithm = HashAlgorithm.Create("SHA1"); if (algorithm == null) { return(retval); } var inArray = algorithm.ComputeHash(dst); retval = Convert.ToBase64String(inArray); } else if (passwordFormat == EPasswordFormat.Encrypted) { passwordSalt = GenerateSalt(); var encryptor = new DesEncryptor { InputString = password, EncryptKey = passwordSalt }; encryptor.DesEncrypt(); retval = encryptor.OutString; } return(retval); }
public static string DecryptStringBySecretKey(string inputString, string secretKey) { if (string.IsNullOrEmpty(inputString)) { return(string.Empty); } inputString = inputString.Replace("0add0", "+").Replace("0equals0", "=").Replace("0and0", "&").Replace("0question0", "?").Replace("0quote0", "'").Replace("0slash0", "/"); var encryptor = new DesEncryptor { InputString = inputString, DecryptKey = secretKey }; encryptor.DesDecrypt(); return(encryptor.OutString); }
public string Encrypt(string text, string?salt = null) { var isSaltEmpty = string.IsNullOrWhiteSpace(salt); var saltDescription = isSaltEmpty ? $"empty {nameof(salt)}" : $"{nameof(salt)} '{salt}'"; _logger.LogInformation($"Encryption of {nameof(text)} '{text}' with {saltDescription} requested."); string hash; string textAndSalt; if (isSaltEmpty) { hash = DesEncryptor.Encrypt(text); salt = hash.Substring(0, 2); textAndSalt = text + salt; } else { textAndSalt = text + salt; if (_cache.TryGetValue <string>(textAndSalt, out var cachedHash)) { _logger.LogInformation($"Encrypted value of the {nameof(text)} '{text}' " + $"was found in cache; the value is '{cachedHash}'."); return(cachedHash); } hash = DesEncryptor.Encrypt(text, salt); } _cache.GetOrCreate(textAndSalt, cacheEntry => { cacheEntry.Size = 13; return(hash); }); _logger.LogInformation($"Encryption of {nameof(text)} '{text}' with {saltDescription} succeeded. " + $"Encrypted value is '{hash}'."); return(hash); }
public static string EncryptStringBySecretKey(string inputString, string secretKey) { if (string.IsNullOrEmpty(inputString)) { return(string.Empty); } var encryptor = new DesEncryptor { InputString = inputString, EncryptKey = secretKey }; encryptor.DesEncrypt(); var retval = encryptor.OutString; retval = retval.Replace("+", "0add0").Replace("=", "0equals0").Replace("&", "0and0").Replace("?", "0question0").Replace("'", "0quote0").Replace("/", "0slash0"); return(retval); }
public string Encrypt(string text, string?salt = null) { var isSaltEmpty = string.IsNullOrWhiteSpace(salt); var saltDescription = isSaltEmpty ? $"empty {nameof(salt)}" : $"{nameof(salt)} '{salt}'"; _logger.LogInformation($"Encryption of {nameof(text)} '{text}' with {saltDescription} requested."); string hash = isSaltEmpty ? DesEncryptor.Encrypt(text) : DesEncryptor.Encrypt(text, salt); _cache.GetOrCreate(hash, cacheEntry => { var trimmedText = text.Length <= 8 ? text : text.Substring(0, 8); cacheEntry.Size = trimmedText.Length; return(trimmedText); }); _logger.LogInformation($"Encryption of {nameof(text)} '{text}' with {saltDescription} succeeded. " + $"Encrypted value is '{hash}'."); return(hash); }