点关注不迷路

Max

C# Base64密文转换字符串报 "Base-64 字符数组或字符串的长度无效。" 问题记录

做一下字符串替换以及PadRight操作就可以了:

string payloadstr = "eyJpZCI6MTMwNzkyNzA3NjUwNjE0MDY3MywiYWNjb3VudCI6InRlc3QiLCJ1c2VyS2V5IjoiMTIzIiwic3ViIjoiMTMwNzkyNzA3NjUwNjE0MDY3MyIsImlhdCI6MTYwMDY3OTM3OCwiZXhwIjoxNjAwNzY1Nzc4fQ";
string dummyData = payloadstr.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
if (dummyData.Length % 4 > 0)
{
  dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
}
byte[] byteArray = Convert.FromBase64String(dummyData); 
Console.WriteLine(Encoding.UTF8.GetString(byteArray));

 

posted @ 2020-09-24 11:29  Max麦克斯  阅读(1152)  评论(1编辑  收藏  举报