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 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]); } }