我有一个使用core1.1的网络核心应用程序当将一个cript / decript模块从旧的.NET4.6迁移到网络核心时,它就无法工作
第一个TripleDES不再(以前)支持128位密钥并使用192位密钥修复,尝试更改它会导致错误。
其次,在尝试描述这个字符串时:
/ Tk0ydguv3HauCVUWDK3Tr6U8c9BBaaRwtSt5q4 / UHG =
TripleDES使用PKCS7 Padding saing启动错误“指定的填充模式对此算法无效。”这很奇怪,因为ti是默认的填充。
我的project.json:
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0",
"type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.1.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1",
"Microsoft.AspNetCore.Mvc.WebApiCompatShim": "1.0.1",
"Microsoft.AspNetCore.Session": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.1": {
"imports": [
"portable-net461+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ "bower install", "dotnet bundle" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
我的代码:
using System;
using System.Security.Cryptography;
using System.Text;
namespace WebApp.Class
{
public class Md5
{
private static readonly byte[] IV = { 240, 3, 45, 29, 0, 76, 173, 59 };
const int NumCryptokey = 6;
const int NumExtraClave = 8;
const int NumsKey = 7;
public static string Generate(int KeyChars)
{
int i_key = 0;
float Random1 = 0;
Int16 arrIndex = default(Int16);
StringBuilder sb = new StringBuilder();
char RandomLetter;
string KeyLetters = "abcdefghijklmnopqrstuvwxyz";
string KeyNumbers = "0123456789";
char[] LettersArray = null;
char[] NumbersArray = null;
LettersArray = KeyLetters.ToCharArray();
NumbersArray = KeyNumbers.ToCharArray();
for (i_key = 1; i_key <= KeyChars; i_key++)
{
Random random = new Random();
Random1 = random.Next();
arrIndex = -1;
if ((Convert.ToInt32(Random1 * 111)) % 2 == 0)
{
while (arrIndex < 0)
{
arrIndex = Convert.ToInt16(LettersArray.GetUpperBound(0) * Random1);
}
RandomLetter = LettersArray[arrIndex];
if ((Convert.ToInt32(arrIndex * Random1 * 99)) % 2 != 0)
{
RandomLetter = LettersArray[arrIndex];
RandomLetter = char.ToUpper(RandomLetter);
}
sb.Append(RandomLetter);
}
else
{
while (arrIndex < 0)
{
arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random1);
}
sb.Append(NumbersArray[arrIndex]);
}
}
return sb.ToString();
}
public static string Encriptar(string serializedQueryString)
{
string functionReturnValue = null;
string sRetorno = null;
try
{
string cryptokey = "";
string ExtraClave = "";
string sKey = "";
cryptokey = Generate(NumCryptokey);
ExtraClave = Generate(NumExtraClave);
sKey = Generate(NumsKey);
byte[] buffer = Encoding.ASCII.GetBytes(serializedQueryString + ExtraClave);
var des = TripleDES.Create();
var MD5 = System.Security.Cryptography.MD5.Create();
des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sKey + cryptokey));
des.IV = IV;
sRetorno = cryptokey + ExtraClave + sKey + Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
functionReturnValue = sRetorno;
}
catch (Exception ex)
{
functionReturnValue = "";
}
return functionReturnValue;
}
public static string Desencriptar(string encryptedQueryString)
{
string functionReturnValue = null;
byte[] buffer = null;
var DES = System.Security.Cryptography.TripleDES.Create();
var Md5 = MD5.Create();
string sRetorno = null;
string cryptokey = "";
string ExtraClave = "";
string sKey = "";
cryptokey = encryptedQueryString.Substring(0,NumCryptokey);
ExtraClave = encryptedQueryString.Substring(NumCryptokey, NumExtraClave);
sKey = encryptedQueryString.Substring(NumCryptokey + NumExtraClave, NumsKey);
encryptedQueryString = encryptedQueryString.Substring(NumCryptokey + NumExtraClave + NumsKey, encryptedQueryString.Length-(NumCryptokey + NumExtraClave + NumsKey));
try
{
buffer = Convert.FromBase64String(encryptedQueryString);
byte[] by = new byte[24];
by = Md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sKey + cryptokey));
Array.Resize(ref by, 24);
DES.Key = by;
DES.IV = IV;
sRetorno = Encoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length)).Replace(ExtraClave, "");
functionReturnValue = sRetorno;
}
catch (Exception ex)
{
functionReturnValue = "";
}
return functionReturnValue;
}
}
}
答案 0 :(得分:-1)
发现它!
public static string Encriptar(string serializedQueryString)
{
string functionReturnValue = null;
string sRetorno = null;
try
{
string cryptokey = "";
string ExtraClave = "";
string sKey = "";
cryptokey = Generate(NumCryptokey);
ExtraClave = Generate(NumExtraClave);
sKey = Generate(NumsKey);
byte[] buffer = Encoding.ASCII.GetBytes(serializedQueryString + ExtraClave);
var des = TripleDES.Create();
var Md5 = MD5.Create();
byte[] by = new byte[24];
by = Md5.ComputeHash(Encoding.ASCII.GetBytes(sKey + cryptokey));
Array.Resize(ref by, 24);
Array.Copy(by, 0, by, 16, 8);
des.Key = by;
des.IV = IV;
//es.Padding = PaddingMode.None;
sRetorno = cryptokey + ExtraClave + sKey + Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
functionReturnValue = sRetorno;
}
catch (Exception ex)
{
functionReturnValue = "";
}
return functionReturnValue;
}
答案 1 :(得分:-2)
我在ASP.NET Core中遇到了同样的问题。但是,我没有指定任何IV。
我在加密时添加了这一行,并在指定密钥后进行了解密。
DES3.IV = new byte[DES3.BlockSize / 8];
如果没有这一行,我就会收到上述错误。
的文档无论何时,IV属性都会自动设置为新的随机值 您创建一个SymmetricAlgorithm类的新实例或 当您手动调用GenerateIV方法时。
然而,我仍然不明白为什么这样有效,因为即使我尝试给IV提供静态值(就像你的问题)&amp;它甚至失败了。