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