指定的填充模式对此算法无效-c#-System.Security.Cryptography
对c#来说相当陌生,目前在解密长密码时遇到问题,错误为 指定的密钥不是此算法的有效大小 我知道这与不支持的加密密码位长度有关,但不确定如何使用建议的方法来允许这些较长的密码 这是我的加密和解密 “cipherKey”:“0123456789abcdef”,“cipherVector”: “某种真正的二氧化碳”指定的填充模式对此算法无效-c#-System.Security.Cryptography,c#,security,encryption,cryptography,system,C#,Security,Encryption,Cryptography,System,对c#来说相当陌生,目前在解密长密码时遇到问题,错误为 指定的密钥不是此算法的有效大小 我知道这与不支持的加密密码位长度有关,但不确定如何使用建议的方法来允许这些较长的密码 这是我的加密和解密 “cipherKey”:“0123456789abcdef”,“cipherVector”: “某种真正的二氧化碳” 函数公共字符串解密字符串(字符串密文) 及 cipher应该是新字节[fullCipher.Length-IV.Length]。您可能丢失了数据中的尾随块。如果这些是您正在加密的用户密码,
函数
公共字符串解密字符串(字符串密文)
及
cipher
应该是新字节[fullCipher.Length-IV.Length]
。您可能丢失了数据中的尾随块。如果这些是您正在加密的用户密码,请扔掉这些代码,因为您不应该这样做。用户密码必须进行散列,这是另一回事。
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace DataApi
{
public class Encryption
{
private readonly IConfigurationService _configService;
private const string _vector = "cipherVector";
private const string _key = "cipherKey";
public Encryption(IConfigurationService configService)
{
_configService = configService;
}
public string EncryptString(string text)
{
if(string.IsNullOrEmpty(text))
{
return "";
}
try
{
var key = Encoding.UTF8.GetBytes(_configService.Get(_key));
byte[] IV = Encoding.ASCII.GetBytes(_configService.Get(_vector));
using (var aesAlg = Aes.Create())
{
using (var encryptor = aesAlg.CreateEncryptor(key, IV))
{
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(text);
}
var decryptedContent = msEncrypt.ToArray();
var result = new byte[IV.Length + decryptedContent.Length];
Buffer.BlockCopy(IV, 0, result, 0, IV.Length);
Buffer.BlockCopy(decryptedContent, 0, result, IV.Length, decryptedContent.Length);
return Convert.ToBase64String(result);
}
}
}
}
catch(Exception e) {
Loggifer.Error("Unable to encrypt string: "+text , e );
throw e;
}
}
public string DecryptString(string cipherText)
{
if(string.IsNullOrEmpty(cipherText))
{
return "";
}
try
{
var fullCipher = Convert.FromBase64String(cipherText);
byte[] IV = Encoding.ASCII.GetBytes(_configService.Get(_vector));
var cipher = new byte[16];
Buffer.BlockCopy(fullCipher, 0, IV, 0, IV.Length);
Buffer.BlockCopy(fullCipher, IV.Length, cipher, 0, IV.Length);
var key = Encoding.UTF8.GetBytes(_configService.Get(_key));
using (var aesAlg = Aes.Create())
{
using (var decryptor = aesAlg.CreateDecryptor(key, IV))
{
string result;
using (var msDecrypt = new MemoryStream(cipher))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
result = srDecrypt.ReadToEnd();
}
}
}
return result;
}
}
}
catch (Exception e)
{
Loggifer.Error("Unable to decrypt string: "+cipherText , e );
throw e;
}
}
}
}
var cipher = new byte[fullCipher.Length - IV.Length];
Buffer.BlockCopy(fullCipher, IV.Length, cipher, 0, fullCipher.Length - IV.Length);