在c#asp.net中编码和解码?

时间:2022-10-28 16:39:27

i am using Encoding and decoding :

我正在使用编码和解码:

For Encoding:

private string EncodeServerName(string ServerName)
    {
      byte[] NameEncodein = new byte[ServerName.Length];
      NameEncodein = System.Text.Encoding.UTF8.GetBytes(ServerName);
      string EcodedName = Convert.ToBase64String(NameEncodein);
      return EcodedName;
    }

and Decoding:

     public string DecoAndGetServerName(string Servername)
     {
         System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
         System.Text.Decoder strDecoder = encoder.GetDecoder();
         byte[] to_DecodeByte = Convert.FromBase64String(Servername);
         int charCount = strDecoder.GetCharCount(to_DecodeByte, 0, to_DecodeByte.Length);
         char[] decoded_char = new char[charCount];
         strDecoder.GetChars(to_DecodeByte, 0, to_DecodeByte.Length, decoded_char,0);
         string Name = new string(decoded_char);

         return Name;
     }

I am sending ServerName:DEV-SQL1\SQL2008

我发送ServerName:DEV-SQL1 \ SQL2008

It is encoded:REVWLVNRTDFcU1FMMjAwOA==

它编码为:REVWLVNRTDFcU1FMMjAwOA ==

Again i want to decode but getting Exception:in line:

我想再次解码,但得到例外:在线:

byte[] to_DecodeByte = Convert.FromBase64String(Servername);

byte [] to_DecodeByte = Convert.FromBase64String(Servername);

Exception IS:

`The input is not a valid Base-64 string as it contains a non-base 64 character,

`输入不是有效的Base-64字符串,因为它包含非基数64字符,

more than two padding characters, or a non-white space character among the padding characters.`

填充字符中有两个以上的填充字符或非空格字符

How to solve this issue.

如何解决这个问题。

Please Help Me

请帮帮我

2 个解决方案

#1


18  

Your code seems way too complex :-), here is one that works:

你的代码似乎太复杂了:-),这是一个有效的:

public static string EncodeServerName(string serverName)
{
    return Convert.ToBase64String(Encoding.UTF8.GetBytes(serverName));
}

public static string DecodeServerName(string encodedServername)
{
    return Encoding.UTF8.GetString(Convert.FromBase64String(encodedServername));
}

#2


0  

the same code works for me, which you written in DecoAndGetServerName().

相同的代码适用于我,你在DecoAndGetServerName()中编写。

the thing is, you need to pass ENCODED STRING to your DecoAndGetServerName() function,

问题是,你需要将ENCODED STRING传递给你的DecoAndGetServerName()函数,

which might be encoded like :

可能编码如下:

string Servername=Convert.ToBase64String(Encoding.UTF8.GetBytes("serverName"));

That's why you got that Error The input is not a valid Base-64 string as it contains a non-base 64 character,....

这就是你得到错误的原因输入不是有效的Base-64字符串,因为它包含一个非基数64字符,....

#1


18  

Your code seems way too complex :-), here is one that works:

你的代码似乎太复杂了:-),这是一个有效的:

public static string EncodeServerName(string serverName)
{
    return Convert.ToBase64String(Encoding.UTF8.GetBytes(serverName));
}

public static string DecodeServerName(string encodedServername)
{
    return Encoding.UTF8.GetString(Convert.FromBase64String(encodedServername));
}

#2


0  

the same code works for me, which you written in DecoAndGetServerName().

相同的代码适用于我,你在DecoAndGetServerName()中编写。

the thing is, you need to pass ENCODED STRING to your DecoAndGetServerName() function,

问题是,你需要将ENCODED STRING传递给你的DecoAndGetServerName()函数,

which might be encoded like :

可能编码如下:

string Servername=Convert.ToBase64String(Encoding.UTF8.GetBytes("serverName"));

That's why you got that Error The input is not a valid Base-64 string as it contains a non-base 64 character,....

这就是你得到错误的原因输入不是有效的Base-64字符串,因为它包含一个非基数64字符,....