[英]org.apache.commons.codec.binary.Base64 in C#
I want to convert a Java class into a C# class. 我想将Java类转换为C#类。
The Java class includes org.apache.commons.codec.binary.Base64
and uses lots of its methods. Java类包含org.apache.commons.codec.binary.Base64
并使用许多方法。
Is there any similar class available in C# that has the same methods and/or same functionality? C#中是否有类似的类具有相同的方法和/或相同的功能?
These may help? 这些可能有帮助吗?
Convert.FromBase64String Method Convert.FromBase64String方法
Convert.ToBase64String Method Convert.ToBase64String方法
For URL safety I would look into the JWT spec . 为了URL安全,我将研究JWT规范 。 It shows the following code: 它显示以下代码:
static string base64urlencode(byte [] arg)
{
string s = Convert.ToBase64String(arg); // Regular base64 encoder
s = s.Split('=')[0]; // Remove any trailing '='s
s = s.Replace('+', '-'); // 62nd char of encoding
s = s.Replace('/', '_'); // 63rd char of encoding
return s;
}
static byte [] base64urldecode(string arg)
{
string s = arg;
s = s.Replace('-', '+'); // 62nd char of encoding
s = s.Replace('_', '/'); // 63rd char of encoding
switch (s.Length % 4) // Pad with trailing '='s
{
case 0: break; // No pad chars in this case
case 2: s += "=="; break; // Two pad chars
case 3: s += "="; break; // One pad char
default: throw new System.Exception(
"Illegal base64url string!");
}
return Convert.FromBase64String(s); // Standard base64 decoder
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.