[英]Invalid length for a Base-64 char array during decoding/decryption
問:我面臨以下大問題:
從時間到另一個我發現以下異常:
Base-64 char數組的長度無效
我使用加密和解密:
public static string Encrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Encoding.UTF8.GetBytes(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
catch (Exception ex)
{
string message = ex.Message;
}
return string.Empty;
}
public static string Decrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
text = text.Replace(" ", "+")
Byte[] byteArray = Convert.FromBase64String(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch (Exception ex)
{
string message = ex.Message;
}
我閱讀了很多關於這個問題的文章,一些帖子談論的解決方案是:
text = text.Replace(" ", "+")
這根本不能解決我的問題
我的字符串是: 3DZF/NZpp0yuQ=3D
我需要幫助來解決這個問題。
編輯
編輯:
Decoding the querystring values is done already when it's parsed into the Request.
要解決您需要解決的問題,然后解碼所有就緒的encode-base64字符串,取決於您使用它的位置。
例如,如果您在url(或查詢)上使用它,可能這是您要使用的地方,那么您需要在使用之前對URL進行編碼,在獲取URL之前對其進行解碼。 原因是您需要避免將URL用作代碼字符的相同字符與加密字符混合使用。
無論如何這里是解決你的問題的代碼(我出於同樣的原因使用):
public static string encodeSTROnUrl(string thisEncode)
{
if (null == thisEncode)
return string.Empty;
return HttpUtility.UrlEncode(Encrypt(thisEncode));
}
public static string decodeSTROnUrl(string thisDecode)
{
return Decrypt(HttpUtility.UrlDecode(thisDecode));
}
ps我有同樣的問題,並嘗試替換'+'和其他,但最后這是使它工作的原因。
不要忘記從代碼中刪除text = text.Replace(“”,“+”)和其他加密操作,只需加密和解密即可。
string imag = img;
imag = imag.Replace("\", "");
int c = imag.Length % 4;
if ((c) != 0)
imag = imag.PadRight((imag.Length + (4 - c)), "=");
[] converted = Convert.FromBase64String(imag);
using (System.IO.MemoryStream vstream = new System.IO.MemoryStream(converted)) {
return Image.FromStream(vstream);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.