Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# base-64字符数组或字符串的长度无效-azure服务器_C#_Azure_Encryption - Fatal编程技术网

C# base-64字符数组或字符串的长度无效-azure服务器

C# base-64字符数组或字符串的长度无效-azure服务器,c#,azure,encryption,C#,Azure,Encryption,我的服务器在azure上运行,经过数小时的调试后,我将azure更改为在64位计算机上运行,现在当我尝试加密或解密时,出现了以下错误:“base-64字符数组或字符串的长度无效”可能是什么? (当我运行本地测试时,所有工作正常) 使用系统; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq; 使用System.Security.Cryptography; 使用系统文本; 使用System.Web; 命名空间服务器 { 公开类密码 {

我的服务器在azure上运行,经过数小时的调试后,我将azure更改为在64位计算机上运行,现在当我尝试加密或解密时,出现了以下错误:“base-64字符数组或字符串的长度无效”可能是什么? (当我运行本地测试时,所有工作正常)

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Security.Cryptography;
使用系统文本;
使用System.Web;
命名空间服务器
{
公开类密码
{
静态只读字符串PasswordHash=“P@@Sw0rd”;
静态只读字符串SaltKey=”S@LT&钥匙”;
静态只读字符串VIKey=“@1B2c3D4e5F6g7H8”;
公共静态字符串加密(字符串明文)
{
字节[]明文字节=Encoding.UTF8.GetBytes(明文);
byte[]keyBytes=新的Rfc2898DeriveBytes(密码哈希,Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256/8);
var symmetricKey=new RijndaelManaged(){Mode=CipherMode.CBC,Padding=PaddingMode.Zeros};
var encryptor=symmetricKey.CreateEncryptor(keyBytes,Encoding.ASCII.GetBytes(VIKey));
字节[]密文字节;
使用(var memoryStream=new memoryStream())
{
使用(var cryptoStream=new cryptoStream(memoryStream,encryptor,CryptoStreamMode.Write))
{
cryptoStream.Write(明文字节,0,明文字节.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes=memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
StringBuilder crypt=新的StringBuilder(Convert.ToBase64String(cipherTextBytes));
如果(crypt.Length>0)
{
对于(int i=0;i<3;i++)
{
if(crypt[crypt.Length-1-i]='=')
{
crypt[crypt.Length-1-i]='*';
}
否则就断了;
}
}
返回crypt.ToString();
}
公共静态字符串解密(字符串加密文本)
{
StringBuilder crypt=新的StringBuilder(encryptedText);
如果(crypt.Length>0)
{
对于(int i=0;i<3;i++)
{
if(crypt[crypt.Length-1-i]='*')
{
crypt[crypt.Length-1-i]='=';
}
否则就断了;
}
}
byte[]cipherTextBytes=Convert.FromBase64String(crypt.ToString());
byte[]keyBytes=新的Rfc2898DeriveBytes(密码哈希,Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256/8);
var symmetricKey=new RijndaelManaged(){Mode=CipherMode.CBC,Padding=PaddingMode.None};
var decryptor=symmetricKey.CreateDecryptor(keyBytes,Encoding.ASCII.GetBytes(VIKey));
var memoryStream=新的memoryStream(密文字节);
var cryptoStream=新的加密流(memoryStream,Decryptostream,CryptoStreamMode.Read);
字节[]明文字节=新字节[cipherTextBytes.Length];
int decryptedByteCount=cryptoStream.Read(plainTextBytes,0,plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
返回Encoding.UTF8.GetString(plainTextBytes,0,decryptedByteCount).TrimEnd(“\0”.ToCharArray());
}
}
}

在我的Azure Web应用程序(x64平台)上测试了您的代码后,我发现它在我这方面运行良好。这是我的测试代码

public ActionResult TestCrypto()
{
    string encryptedText = Crypto.Encrypt("123456");
    string text = "Encrypted Text : " + encryptedText + "<br/>";
    string originalText = Crypto.Decrypt(encryptedText);
    text += "Origial Text : " + originalText;
    return Content(text);
}
public ActionResult TestCrypto()
{
字符串encryptedText=Crypto.Encrypt(“123456”);
string text=“加密文本:“+encryptedText+”
”; string originalText=加密解密(encryptedText); 文本+=“原始文本:”+原始文本; 返回内容(文本); }
这是我从服务器上得到的结果

我建议您在服务器中测试上面的代码并发布结果

base-64字符数组或字符串的长度无效

在解密加密文本时经常发生异常。如果将加密文本存储在数据库中,有时由于用于存储数据的列没有足够的空间,加密文本会丢失一些数据。例如,您定义的列类型是nvarchar(32),加密文本的长度是56,大于32。在解密之前,请确保加密文本未被更改。

IV必须是不可预测的(读取:随机)。不要使用静态IV(
VIKey
),因为这会使密码具有确定性,因此在语义上不安全。观察密文的攻击者可以确定以前何时发送相同的消息前缀。IV不是秘密,所以你可以把它和密文一起发送。通常,它只是在密文前面加上前缀,并在解密之前切掉。请检查。这可能是一个主键base64格式问题
public ActionResult TestCrypto()
{
    string encryptedText = Crypto.Encrypt("123456");
    string text = "Encrypted Text : " + encryptedText + "<br/>";
    string originalText = Crypto.Decrypt(encryptedText);
    text += "Origial Text : " + originalText;
    return Content(text);
}