public string Decrypt(string value) { string res = ""; try { if (value == null) { return(""); } if (value.Trim().Length == 0) { return(""); } string encrypted = decrypt_str(value); encrypted = encrypted.Substring(64); byte[] encryptedBytesWithSalt = Convert.FromBase64String(encrypted); byte[] salt = new byte[8]; byte[] encryptedBytes = new byte[encryptedBytesWithSalt.Length - salt.Length - 8]; Buffer.BlockCopy(encryptedBytesWithSalt, 8, salt, 0, salt.Length); Buffer.BlockCopy(encryptedBytesWithSalt, salt.Length + 8, encryptedBytes, 0, encryptedBytes.Length); byte[] key, iv; DeriveKeyAndIV(passphrase, salt, out key, out iv); res = DecryptStringFromBytesAes(encryptedBytes, key, iv); } catch (Exception exp) { clsCommon common = new clsCommon(); common.Log("Decrypt", exp.Message + "(value=" + value + ")", true, exp); } return(res); }
public string Decrypt(string encrypted) { string res = ""; try { if (encrypted == null) { return(""); } if (encrypted.Trim().Length == 0) { return(""); } // base 64 decode byte[] encryptedBytesWithSalt = Convert.FromBase64String(encrypted); // extract salt (first 8 bytes of encrypted) byte[] salt = new byte[8]; byte[] encryptedBytes = new byte[encryptedBytesWithSalt.Length - salt.Length - 8]; Buffer.BlockCopy(encryptedBytesWithSalt, 8, salt, 0, salt.Length); Buffer.BlockCopy(encryptedBytesWithSalt, salt.Length + 8, encryptedBytes, 0, encryptedBytes.Length); // get key and iv byte[] key, iv; DeriveKeyAndIV(passphrase, salt, out key, out iv); res = DecryptStringFromBytesAes(encryptedBytes, key, iv); res = Uri.UnescapeDataString(res); } catch (Exception exp) { clsCommon common = new clsCommon(); common.Log("Decrypt", exp.Message + "(value=" + encrypted + ")", true, exp); } return(res); }
public bool IsValidString(string val) { bool res = false; try { if (val.ToLower().Trim().Contains("'")) { return(false); } if (val.ToLower().Trim().Contains('"')) { return(false); } if (val.ToLower().Trim().Contains("&")) { return(false); } return(true); } catch (Exception exp) { clsCommon common = new clsCommon(); common.Log("IsValidString", exp.Message + "(val=" + val + ")", true, exp); } return(res); }
public string GetNewPW() { string res = ""; try { string new_pass = "******"; return(new_pass); } catch (Exception exp) { clsCommon common = new clsCommon(); common.Log("GetNewPW", exp.Message, true, exp); } return(res); }
public string Encrypt(string plainText) { string res = ""; try { if (plainText == null) { return(""); } if (plainText.Trim().Length == 0) { return(""); } // generate salt byte[] key, iv; byte[] salt = new byte[8]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetNonZeroBytes(salt); DeriveKeyAndIV(passphrase, salt, out key, out iv); // encrypt bytes byte[] encryptedBytes = EncryptStringToBytesAes(plainText, key, iv); // add salt as first 8 bytes byte[] encryptedBytesWithSalt = new byte[salt.Length + encryptedBytes.Length + 8]; Buffer.BlockCopy(Encoding.ASCII.GetBytes("Salted__"), 0, encryptedBytesWithSalt, 0, 8); Buffer.BlockCopy(salt, 0, encryptedBytesWithSalt, 8, salt.Length); Buffer.BlockCopy(encryptedBytes, 0, encryptedBytesWithSalt, salt.Length + 8, encryptedBytes.Length); // base64 encode res = Convert.ToBase64String(encryptedBytesWithSalt); res = Uri.EscapeUriString(res); } catch (Exception exp) { clsCommon common = new clsCommon(); common.Log("Encrypt", exp.Message + "(value=" + plainText + ")", true, exp); } return(res); }
public string Encrypt(string value) { string res = ""; try { if (value == null) { return(""); } if (value.Trim().Length == 0) { return(""); } byte[] key, iv; byte[] salt = new byte[8]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetNonZeroBytes(salt); DeriveKeyAndIV(passphrase, salt, out key, out iv); byte[] encryptedBytes = EncryptStringToBytesAes(value, key, iv); byte[] encryptedBytesWithSalt = new byte[salt.Length + encryptedBytes.Length + 8]; Buffer.BlockCopy(Encoding.ASCII.GetBytes("Salted__"), 0, encryptedBytesWithSalt, 0, 8); Buffer.BlockCopy(salt, 0, encryptedBytesWithSalt, 8, salt.Length); Buffer.BlockCopy(encryptedBytes, 0, encryptedBytesWithSalt, salt.Length + 8, encryptedBytes.Length); res = Convert.ToBase64String(encryptedBytesWithSalt); string new_pass = sha256(passphrase); string new_enc = CalcHMACSHA256Hash(res, new_pass) + res; res = encrypt_str(new_enc); return(res); } catch (Exception exp) { clsCommon common = new clsCommon(); common.Log("Encrypt", exp.Message + "(value=" + value + ")", true, exp); } return(res); }