C#-解密密码引发错误输入不是有效的Base-64字符串,因为它包含非Base-64字符
我正在使用以下方法解密我的密码:C#-解密密码引发错误输入不是有效的Base-64字符串,因为它包含非Base-64字符,c#,string,encryption,cryptography,C#,String,Encryption,Cryptography,我正在使用以下方法解密我的密码: var encryptedstring = "__utmc=1; __utma=1.1379697434.1428532783.1428532783.1428532783.1; __utmb=1.4.10.1428532783; __utmz=1.1428532783.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAnu1D5
var encryptedstring = "__utmc=1; __utma=1.1379697434.1428532783.1428532783.1428532783.1; __utmb=1.4.10.1428532783; __utmz=1.1428532783.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAnu1D5k/osU6aUvqIYSeF0gAAAAACAAAAAAAQZgAAAAEAACAAAABxRKV16Z/rmwCDJ14qOw09LPpYST26ttDoAtzEkEaE5AAAAAAOgAAAAAIAACAAAACo9ZWuW9nj0rQtgM6nefu6EE6bxNqklVVpzsHqf1HKRCAAAADzMPYSaE1KRBeAemPjsHdM2dAABzAaAF9EsOaVUs+OoEAAAAARxP9cg3UwTLH7SsQfTJixVo1FkIOErLSyA/mzzAA8KxT1wp5ANsVpE6BYA2BLzBAvKqLZ2FhxamPA96xXiGec";
var decryptstring = Decrypt(encryptedstring);
public string Decrypt(string cipher)
{
if (cipher == null) throw new ArgumentNullException("cipher");
//parse base64 string
byte[] data = Convert.FromBase64String(cipher);
//decrypt data
byte[] decrypted = ProtectedData.Unprotect(data, null, Scope);
return Encoding.Unicode.GetString(decrypted);
}
它在Convert.FromBase64String处抛出以下异常
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
编辑:
添加了加密代码:
public static string EncryptProtected(string plainText)
{
if (plainText == null) throw new ArgumentNullException("plainText");
var data = Encoding.Unicode.GetBytes(plainText);
byte[] encrypted = ProtectedData.Protect(data, null, DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encrypted);
}
第一行是无效的C#,无法编译。更不用说在
Decrypt
函数中没有使用任何这些变量。或者整个第一行都是字符串密码
?错误消息说明了一切。这个字符串不是base64 A-Z,A-Z,0-9+和/是@philsoady,你是对的。但是你知道我是否可以使用任何转换函数将上面的字符串转换为字节[],该字符串似乎不是加密密码;这没有任何意义,所有那些utm
s。此外,密码通常是散列的,这是单向的;无法从哈希中恢复原始数据。这个字符串是从哪里来的?猜测一下,您正在将加密数据存储在cookie中,然后尝试从cookie中提取并解密。不幸的是,您的encryptedstring
似乎是发送到页面的所有cookie的串联,而不仅仅是您设置的包含密码的cookie(“utm…”都是Google Analytics cookies)。您想要的片段是最后一个分号后面的部分,即“AQAAA…XiGec”,它是Base64编码的,应该正确解码。因此,在执行Base64解码之前,您将需要提取该部分。