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

问题描述

你好,

我正在使用asp.net Web应用程序.我将项目文件上传到实时服务器上.在浏览时,我仅在某些系统中遇到错误.在其他一些系统中,它也可以正常工作.您能建议我解决这个问题的任何方法吗?

异常:Base-64 char数组或字符串的无效长度

上面的异常出现在下面的代码中

Hello,

I am working with asp.net webapplication. I uploaded my project files on live server. While browsing i am getting below error in some systems only. And in some other systems its working fine. Can you suggest me any solution for this problem

Exception : Invalid length for a Base-64 char array or string

Above exception is coming in below code

protected void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                //decrypting email
                string email = GlobalMethods.Decrypt(Request["Email"].ToString(), true);
                //getting entered user name
                string user = txtUser.Text;
                //getting entered dob
                DateTime DOB = DateTime.ParseExact(txtdob.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None).Date;

                //validating in db
                DataTable dt = bll.getUser_vaild(email, user, DOB);
                //if exists display next screen to update password
                if (dt.Rows.Count > 0)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "edit", "document.getElementById('view2').style.display='block';", true);
                }
                else
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Invalid details');</script>", false);
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLogFile(HttpContext.Current.Request.UserHostAddress, this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message.ToString());
                ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('" + ex.Message + "');</script>", false);
            }
        }





以下是加密,解密方法:

公共静态字符串Encrypt(字符串toEncrypt,布尔值useHashing)
{
byte [] keyArray;
byte [] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

System.Configuration.AppSettingsReader settingsReader =
新的AppSettingsReader();
//从配置文件获取密钥

字符串键=(string)settingsReader.GetValue("SecurityKey",
typeof(String));
//System.Windows.Forms.MessageBox.Show(key);
//如果使用散列,则将get hashcode视为您的密钥
如果(useHashing)
{
MD5CryptoServiceProvider hashmd5 =新的MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//始终释放资源并刷新数据
//的密码服务提供.最佳实践

hashmd5.Clear();
}
其他
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes =新的TripleDESCryptoServiceProvider();
//设置TripleDES算法的密钥
tdes.Key = keyArray;
//操作模式.还有其他4种模式.
//我们选择ECB(电子密码书)
tdes.Mode = CipherMode.ECB;
//填充模式(如果添加了额外的字节)

tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateEncryptor();
//将字节数组的指定区域转换为resultArray
byte [] resultArray =
cTransform.TransformFinalBlock(toEncryptArray,0,
toEncryptArray.Length);
//释放TripleDes Encryptor拥有的资源
tdes.Clear();
//将加密的数据返回为不可读的字符串格式
return Convert.ToBase64String(resultArray,0,resultArray.Length);
}
公共静态字符串Decrypt(string cipherString,bool useHashing)
{
byte [] keyArray;
//获取字符串的字节码

cipherString = cipherString.Replace('''',``+'');

byte [] toEncryptArray = Convert.FromBase64String(cipherString);

System.Configuration.AppSettingsReader settingsReader =
新的AppSettingsReader();
//从配置文件中获取密钥以打开锁!
字符串键=(string)settingsReader.GetValue("SecurityKey",
typeof(String));

如果(useHashing)
{
//如果使用了散列,请获取有关您的密钥的散列代码
MD5CryptoServiceProvider hashmd5 =新的MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//释放MD5CryptoServiceProvider拥有的任何资源

hashmd5.Clear();
}
其他
{
//如果未实现散列,则获取密钥的字节码
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}

TripleDESCryptoServiceProvider tdes =新的TripleDESCryptoServiceProvider();
//设置TripleDES算法的密钥
tdes.Key = keyArray;
//操作模式.还有其他4种模式.
//我们选择ECB(电子密码书)

tdes.Mode = CipherMode.ECB;
//填充模式(如果添加了额外的字节)
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte [] resultArray = cTransform.TransformFinalBlock(
toEncryptArray,0,toEncryptArray.Length);
//释放TripleDes Encryptor拥有的资源
tdes.Clear();
//返回清除已解密的TEXT
返回UTF8Encoding.UTF8.GetString(resultArray);
}

谢谢,





Below are Encrypt, Decrypt methods :

public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

System.Configuration.AppSettingsReader settingsReader =
new AppSettingsReader();
// Get the key from config file

string key = (string)settingsReader.GetValue("SecurityKey",
typeof(String));
//System.Windows.Forms.MessageBox.Show(key);
//If hashing use get hashcode regards to your key
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//Always release the resources and flush data
// of the Cryptographic service provide. Best Practice

hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes.
//We choose ECB(Electronic code Book)
tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)

tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateEncryptor();
//transform the specified region of bytes array to resultArray
byte[] resultArray =
cTransform.TransformFinalBlock(toEncryptArray, 0,
toEncryptArray.Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//Return the encrypted data into unreadable string format
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
//get the byte code of the string

cipherString = cipherString.Replace('' '', ''+'');

byte[] toEncryptArray = Convert.FromBase64String(cipherString);

System.Configuration.AppSettingsReader settingsReader =
new AppSettingsReader();
//Get your key from config file to open the lock!
string key = (string)settingsReader.GetValue("SecurityKey",
typeof(String));

if (useHashing)
{
//if hashing was used get the hash code with regards to your key
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//release any resource held by the MD5CryptoServiceProvider

hashmd5.Clear();
}
else
{
//if hashing was not implemented get the byte code of the key
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes.
//We choose ECB(Electronic code Book)

tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(
toEncryptArray, 0, toEncryptArray.Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//return the Clear decrypted TEXT
return UTF8Encoding.UTF8.GetString(resultArray);
}

Thanks,

推荐答案



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

06-23 12:46