public string Unprotect(string protectedData, bool fromBase64 = false) { try { if (string.IsNullOrEmpty(protectedData)) { return(string.Empty); } string text; if (fromBase64) { try { text = Base256Encoders.DecryptFromBase64(protectedData, CurrentKey.MasterKey); } catch { List <DataProtectionKeys> keys = GetKeys(); if (keys.Count > 1) { text = Base256Encoders.DecryptFromBase64(protectedData, keys.Skip(1).FirstOrDefault().MasterKey); return(text); } return(string.Empty); } } else { try { text = Base256Encoders.DecryptFromBase256(protectedData, CurrentKey.MasterKey); } catch { List <DataProtectionKeys> keys2 = GetKeys(); if (keys2.Count > 1) { text = Base256Encoders.DecryptFromBase256(protectedData, keys2.Skip(1).FirstOrDefault().MasterKey); return(text); } return(string.Empty); } } string iV = GetIV(); if (text.StartsWith(iV)) { return(text.Substring(iV.Length)); } return(string.Empty); } catch { return(string.Empty); } }
public string Protect(string plaintext, bool toBase64 = false) { if (string.IsNullOrEmpty(plaintext)) { return(string.Empty); } try { if (toBase64) { return(Base256Encoders.EncryptToBase64($"{GetIV()}{plaintext}", CurrentKey.MasterKey)); } return(Base256Encoders.EncryptToBase256($"{GetIV()}{plaintext}", CurrentKey.MasterKey)); } catch { return(string.Empty); } }
public byte[] Protect(byte[] plaintext) { if (plaintext != null && plaintext.Length != 0) { try { string iV = GetIV(); byte[] bytes = Base256Encoders.SecureUtf8Encoding.GetBytes(iV); byte[] array = new byte[bytes.Length + plaintext.Length]; bytes.CopyTo(array, 0); plaintext.CopyTo(array, bytes.Length); return(Base256Encoders.EncryptToBase256(array, CurrentKey.MasterKey)); } catch { return(new byte[0]); } } return(new byte[0]); }
public byte[] Unprotect(byte[] protectedData) { try { if (protectedData != null && protectedData.Length != 0) { byte[] bytes = Base256Encoders.DecryptFromBase256(protectedData, CurrentKey.MasterKey); string _string = Base256Encoders.SecureUtf8Encoding.GetString(bytes); string iV = GetIV(); if (_string.Length > iV.Length && _string.StartsWith(iV)) { _string = _string.Substring(iV.Length); return(Base256Encoders.SecureUtf8Encoding.GetBytes(_string)); } return(new byte[0]); } return(new byte[0]); } catch { return(new byte[0]); } }