passwordDecode中的错误Base-64 char数组或字符串的长度无效. [英] Error in passwordDecodeInvalid length for a Base-64 char array or string.

查看:114
本文介绍了passwordDecode中的错误Base-64 char数组或字符串的长度无效.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我尝试登录"Base-64字符数组或字符串的passwordDecodeInvalid长度错误,都会收到此错误."

这是我的课

I get this error everytime i try to login "Error in passwordDecodeInvalid length for a Base-64 char array or string."

Here is my class

public string Encrypt(string password)
       {
           try
           {
               byte[] encPassword = new byte[password.Length];
               encPassword = System.Text.Encoding.UTF8.GetBytes(password);

               string encodedPassword = Convert.ToBase64String(encPassword);
               return encodedPassword;
           }
           catch (Exception ex)
           {
               throw new Exception("Error in PasswordEncode" + ex.Message);

           }

       }
       public string Decrypt(string password)
       {
           try
           {
               System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
               System.Text.Decoder passDecode = encoder.GetDecoder();
               byte[] decodedPassword = Convert.FromBase64String(password);
               int charCount = passDecode.GetCharCount(decodedPassword, 0, decodedPassword.Length);
               char[] decodedChar = new char[charCount];
               passDecode.GetChars(decodedPassword, 0, decodedPassword.Length, decodedChar, 0);
               string result = new string(decodedChar);
               return result;

           }
           catch (Exception ex)
           {
               throw new Exception("Error in passwordDecode" + ex.Message);
           }
       }

推荐答案

解码期间,您可以先获取与base64字符串等效的字符串,然后读取该字符串的utf8字节,然后对其进行解码并转换回到字符串.像这样的东西:

During decoding, you could first get the string equivalent of the base64 string, then read the utf8 bytes of the string then decode it and convert it back to string. Something like:

...

using System.Text;

...

public string Decrypt(string password)
{
     try
     {
        string temp = Convert.FromBase64String(password);

        byte[] decryped = Encoding.UTF8.GetBytes(temp) 

        return (string)decrypted;
     }
     catch { ... }
     
     ...
}


这篇关于passwordDecode中的错误Base-64 char数组或字符串的长度无效.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆