Przeglądaj źródła

1. [Security] 调整 AES/AES_CTR_NoPadding/TripleDES(原DES3) 2. 其他适配调整. 3.发布版本时才生成包.

Roc 7 lat temu
rodzic
commit
0cc2cd74fc

+ 2 - 2
src/Essensoft.AspNetCore.Payment.Alipay/AlipayClient.cs

@@ -228,7 +228,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay
 
                 }
 
-                var encryptContent = AES.Encrypt(txtParams[BIZ_CONTENT], options.EncyptKey, AlipaySignature.AES_IV, AESCipherMode.CBC, AESPaddingMode.PKCS7);
+                var encryptContent = AES.Encrypt(txtParams[BIZ_CONTENT], options.EncyptKey, AlipaySignature.AES_IV, CipherMode.CBC, PaddingMode.PKCS7);
                 txtParams.Remove(BIZ_CONTENT);
                 txtParams.Add(BIZ_CONTENT, encryptContent);
                 txtParams.Add(ENCRYPT_TYPE, options.EncyptType);
@@ -398,7 +398,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay
                     throw new AlipayException("api only support Aes!");
                 }
 
-                var encryptContent = AES.Encrypt(result[BIZ_CONTENT], options.EncyptKey, AlipaySignature.AES_IV, AESCipherMode.CBC, AESPaddingMode.PKCS7);
+                var encryptContent = AES.Encrypt(result[BIZ_CONTENT], options.EncyptKey, AlipaySignature.AES_IV, CipherMode.CBC, PaddingMode.PKCS7);
                 result.Remove(BIZ_CONTENT);
                 result.Add(BIZ_CONTENT, encryptContent);
                 result.Add(ENCRYPT_TYPE, options.EncyptType);

+ 1 - 1
src/Essensoft.AspNetCore.Payment.Alipay/Essensoft.AspNetCore.Payment.Alipay.csproj

@@ -13,13 +13,13 @@
     <PackageProjectUrl>https://github.com/Essensoft/Payment</PackageProjectUrl>
     <RepositoryUrl>https://github.com/Essensoft/Payment</RepositoryUrl>
     <RepositoryType>git</RepositoryType>
-    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
     <PackageId>Essensoft.AspNetCore.Payment.Alipay</PackageId>
     <PackageTags>Essensoft;Payment;Alipay;支付宝;</PackageTags>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
   </PropertyGroup>
 
   <ItemGroup>

+ 2 - 1
src/Essensoft.AspNetCore.Payment.Alipay/Parser/AlipayJsonParser.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections;
+using System.Security.Cryptography;
 using Essensoft.AspNetCore.Payment.Alipay.Utility;
 using Essensoft.AspNetCore.Payment.Security;
 using Newtonsoft.Json;
@@ -21,7 +22,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Parser
             var item = ParseEncryptData(request, body);
             var bodyIndexContent = body.Substring(0, item.startIndex);
             var bodyEndexContent = body.Substring(item.endIndex);
-            var bizContent = AES.Decrypt(item.encryptContent, encryptKey, AlipaySignature.AES_IV, AESCipherMode.CBC, AESPaddingMode.PKCS7);
+            var bizContent = AES.Decrypt(item.encryptContent, encryptKey, AlipaySignature.AES_IV, CipherMode.CBC, PaddingMode.PKCS7);
 
             return bodyIndexContent + bizContent + bodyEndexContent;
         }

+ 2 - 1
src/Essensoft.AspNetCore.Payment.Alipay/Parser/AlipayXmlParser.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
+using System.Security.Cryptography;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Xml.Serialization;
@@ -23,7 +24,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Parser
 
             var bodyIndexContent = body.Substring(0, item.startIndex);
             var bodyEndContent = body.Substring(item.endIndex);
-            var encryptContent = AES.Decrypt(item.encryptContent, encryptKey, AlipaySignature.AES_IV, AESCipherMode.CBC, AESPaddingMode.PKCS7);
+            var encryptContent = AES.Decrypt(item.encryptContent, encryptKey, AlipaySignature.AES_IV, CipherMode.CBC, PaddingMode.PKCS7);
 
             return bodyIndexContent + encryptContent + bodyEndContent;
         }

+ 1 - 1
src/Essensoft.AspNetCore.Payment.JDPay/Essensoft.AspNetCore.Payment.JDPay.csproj

@@ -13,13 +13,13 @@
     <PackageProjectUrl>https://github.com/Essensoft/Payment</PackageProjectUrl>
     <RepositoryUrl>https://github.com/Essensoft/Payment</RepositoryUrl>
     <RepositoryType>git</RepositoryType>
-    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
     <PackageId>Essensoft.AspNetCore.Payment.JDPay</PackageId>
     <PackageTags>Essensoft;Payment;JDPay;京东支付;</PackageTags>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
   </PropertyGroup>
 
   <ItemGroup>

+ 5 - 2
src/Essensoft.AspNetCore.Payment.JDPay/Utility/JDPaySecurity.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections;
 using System.IO;
+using System.Security.Cryptography;
 using System.Security.Cryptography.X509Certificates;
 using System.Text;
 using Essensoft.AspNetCore.Payment.Security;
@@ -10,6 +11,8 @@ using Org.BouncyCastle.Crypto.Operators;
 using Org.BouncyCastle.Pkcs;
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.X509.Store;
+using SHA1 = Essensoft.AspNetCore.Payment.Security.SHA1;
+using SHA256 = Essensoft.AspNetCore.Payment.Security.SHA256;
 using X509Certificate = Org.BouncyCastle.X509.X509Certificate;
 
 namespace Essensoft.AspNetCore.Payment.JDPay.Utility
@@ -127,14 +130,14 @@ namespace Essensoft.AspNetCore.Payment.JDPay.Utility
         public static string EncryptECB(string data, byte[] key)
         {
             var resultByte = InitResultByteArray(data);
-            var desdata = DES3.Encode(resultByte, key, iv, DESCipherMode.ECB, DESPaddingMode.Zeros);
+            var desdata = Security.TripleDES.Encode(resultByte, key, iv, CipherMode.ECB, PaddingMode.Zeros);
             return BitConverter.ToString(desdata).Replace("-", "").ToLower();
         }
 
         public static string DecryptECB(string data, byte[] key)
         {
             var hexSourceData = Hex2byte(data);
-            var unDesResult = DES3.Decode(hexSourceData, key, iv, DESCipherMode.ECB, DESPaddingMode.Zeros);
+            var unDesResult = Security.TripleDES.Decode(hexSourceData, key, iv, CipherMode.ECB, PaddingMode.Zeros);
             var dataSizeByte = new byte[4];
             dataSizeByte[0] = unDesResult[0];
             dataSizeByte[1] = unDesResult[1];

+ 1 - 1
src/Essensoft.AspNetCore.Payment.LianLianPay/Essensoft.AspNetCore.Payment.LianLianPay.csproj

@@ -13,13 +13,13 @@
     <PackageProjectUrl>https://github.com/Essensoft/Payment</PackageProjectUrl>
     <RepositoryUrl>https://github.com/Essensoft/Payment</RepositoryUrl>
     <RepositoryType>git</RepositoryType>
-    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
     <PackageId>Essensoft.AspNetCore.Payment.LianLianPay</PackageId>
     <PackageTags>Essensoft;Payment;LianLianPay;连连支付;</PackageTags>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
   </PropertyGroup>
 
   <ItemGroup>

+ 1 - 1
src/Essensoft.AspNetCore.Payment.QPay/Essensoft.AspNetCore.Payment.QPay.csproj

@@ -13,13 +13,13 @@
     <PackageProjectUrl>https://github.com/Essensoft/Payment</PackageProjectUrl>
     <RepositoryUrl>https://github.com/Essensoft/Payment</RepositoryUrl>
     <RepositoryType>git</RepositoryType>
-    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
     <PackageId>Essensoft.AspNetCore.Payment.QPay</PackageId>
     <PackageTags>Essensoft;Payment;QPay;QQ钱包;</PackageTags>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
   </PropertyGroup>
 
   <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">

+ 46 - 49
src/Essensoft.AspNetCore.Payment.Security/AES.cs

@@ -4,75 +4,72 @@ using System.Text;
 
 namespace Essensoft.AspNetCore.Payment.Security
 {
-    public enum AESCipherMode
-    {
-        CBC = CipherMode.CBC,
-        ECB = CipherMode.ECB
-    }
-
-    public enum AESPaddingMode
-    {
-        PKCS7 = PaddingMode.PKCS7
-    }
-
     public class AES
     {
-        public static string Encrypt(string data, string key, byte[] iv, AESCipherMode cipherMode, AESPaddingMode paddingMode)
+        public static string Encrypt(string data, string key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
         {
-            var rm = new RijndaelManaged
+            using (var aes = Aes.Create())
             {
-                IV = iv,
-                Key = Encoding.UTF8.GetBytes(key),
-                Mode = (CipherMode)cipherMode,
-                Padding = (PaddingMode)paddingMode
-            };
+                aes.Key = Encoding.UTF8.GetBytes(key);
+                aes.IV = iv;
+                aes.Mode = cipherMode;
+                aes.Padding = paddingMode;
 
-            var content = Encoding.UTF8.GetBytes(data);
-            var ctf = rm.CreateEncryptor();
-            return Convert.ToBase64String(ctf.TransformFinalBlock(content, 0, content.Length));
+                using (var ctf = aes.CreateEncryptor())
+                {
+                    var content = Encoding.UTF8.GetBytes(data);
+                    return Convert.ToBase64String(ctf.TransformFinalBlock(content, 0, content.Length));
+                }
+            }
         }
 
-        public static string Decrypt(string data, string key, byte[] iv, AESCipherMode cipherMode, AESPaddingMode paddingMode)
+        public static string Decrypt(string data, string key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
         {
-            var rm = new RijndaelManaged
+            using (var aes = Aes.Create())
             {
-                IV = iv,
-                Key = Encoding.UTF8.GetBytes(key),
-                Mode = (CipherMode)cipherMode,
-                Padding = (PaddingMode)paddingMode
-            };
+                aes.Key = Encoding.UTF8.GetBytes(key);
+                aes.IV = iv;
+                aes.Mode = cipherMode;
+                aes.Padding = paddingMode;
 
-            var content = Convert.FromBase64String(data);
-            var ctf = rm.CreateDecryptor();
-            return Encoding.UTF8.GetString(ctf.TransformFinalBlock(content, 0, content.Length));
+                using (var ctf = aes.CreateDecryptor())
+                {
+                    var content = Convert.FromBase64String(data);
+                    return Encoding.UTF8.GetString(ctf.TransformFinalBlock(content, 0, content.Length));
+                }
+            }
         }
 
-        public static string Encrypt(string data, string key, AESCipherMode cipherMode, AESPaddingMode paddingMode)
+        public static string Encrypt(string data, string key, CipherMode cipherMode, PaddingMode paddingMode)
         {
-            var rm = new RijndaelManaged
+            using (var aes = Aes.Create())
             {
-                Key = Encoding.UTF8.GetBytes(key),
-                Mode = (CipherMode)cipherMode,
-                Padding = (PaddingMode)paddingMode
-            };
+                aes.Key = Encoding.UTF8.GetBytes(key);
+                aes.Mode = cipherMode;
+                aes.Padding = paddingMode;
 
-            var content = Encoding.UTF8.GetBytes(data);
-            var ctf = rm.CreateEncryptor();
-            return Convert.ToBase64String(ctf.TransformFinalBlock(content, 0, content.Length));
+                using (var ctf = aes.CreateEncryptor())
+                {
+                    var content = Encoding.UTF8.GetBytes(data);
+                    return Convert.ToBase64String(ctf.TransformFinalBlock(content, 0, content.Length));
+                }
+            }
         }
 
-        public static string Decrypt(string data, string key, AESCipherMode cipherMode, AESPaddingMode paddingMode)
+        public static string Decrypt(string data, string key, CipherMode cipherMode, PaddingMode paddingMode)
         {
-            var rm = new RijndaelManaged
+            using (var aes = Aes.Create())
             {
-                Key = Encoding.UTF8.GetBytes(key),
-                Mode = (CipherMode)cipherMode,
-                Padding = (PaddingMode)paddingMode
-            };
+                aes.Key = Encoding.UTF8.GetBytes(key);
+                aes.Mode = cipherMode;
+                aes.Padding = paddingMode;
 
-            var content = Convert.FromBase64String(data);
-            var ctf = rm.CreateDecryptor();
-            return Encoding.UTF8.GetString(ctf.TransformFinalBlock(content, 0, content.Length));
+                using (var ctf = aes.CreateDecryptor())
+                {
+                    var content = Convert.FromBase64String(data);
+                    return Encoding.UTF8.GetString(ctf.TransformFinalBlock(content, 0, content.Length));
+                }
+            }
         }
     }
 }

+ 4 - 6
src/Essensoft.AspNetCore.Payment.Security/AES_CTR_NoPadding.cs

@@ -23,16 +23,14 @@ namespace Essensoft.AspNetCore.Payment.Security
 
         public static string Encrypt(string data, string key, byte[] iv)
         {
-            var cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
-            cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", Encoding.UTF8.GetBytes(key)), iv));
-            return Convert.ToBase64String(cipher.DoFinal(Encoding.UTF8.GetBytes(data)));
+            var encData = Encrypt(Encoding.UTF8.GetBytes(data), Encoding.UTF8.GetBytes(key), iv);
+            return Convert.ToBase64String(encData);
         }
 
         public static string Decrypt(string data, string key, byte[] iv)
         {
-            var cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
-            cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", Encoding.UTF8.GetBytes(key)), iv));
-            return Encoding.UTF8.GetString(cipher.DoFinal(Convert.FromBase64String(data)));
+            var decData = Decrypt(Convert.FromBase64String(data), Encoding.UTF8.GetBytes(key), iv);
+            return Encoding.UTF8.GetString(decData);
         }
     }
 }

+ 0 - 66
src/Essensoft.AspNetCore.Payment.Security/DES3.cs

@@ -1,66 +0,0 @@
-using System.IO;
-using System.Security.Cryptography;
-
-namespace Essensoft.AspNetCore.Payment.Security
-{
-    public enum DESCipherMode
-    {
-        CBC = CipherMode.CBC,
-        ECB = CipherMode.ECB
-    }
-
-    public enum DESPaddingMode
-    {
-        PKCS7 = PaddingMode.PKCS7,
-        Zeros = PaddingMode.Zeros
-    }
-
-    public class DES3
-    {
-        public static byte[] Encode(byte[] data, byte[] key, byte[] iv, DESCipherMode cipherMode, DESPaddingMode paddingMode)
-        {
-            try
-            {
-                var tdsp = new TripleDESCryptoServiceProvider
-                {
-                    Mode = (CipherMode)cipherMode,
-                    Padding = (PaddingMode)paddingMode
-                };
-                using (var stream = new MemoryStream())
-                using (var crypto = new CryptoStream(stream, tdsp.CreateEncryptor(key, iv), CryptoStreamMode.Write))
-                {
-                    crypto.Write(data, 0, data.Length);
-                    crypto.FlushFinalBlock();
-                    return stream.ToArray();
-                }
-            }
-            catch
-            {
-                return null;
-            }
-        }
-
-        public static byte[] Decode(byte[] data, byte[] key, byte[] iv, DESCipherMode cipherMode, DESPaddingMode paddingMode)
-        {
-            try
-            {
-                var tdsp = new TripleDESCryptoServiceProvider
-                {
-                    Mode = (CipherMode)cipherMode,
-                    Padding = (PaddingMode)paddingMode
-                };
-                using (var stream = new MemoryStream(data))
-                using (var crypto = new CryptoStream(stream, tdsp.CreateDecryptor(key, iv), CryptoStreamMode.Read))
-                {
-                    var fromEncrypt = new byte[data.Length];
-                    crypto.Read(fromEncrypt, 0, fromEncrypt.Length);
-                    return fromEncrypt;
-                }
-            }
-            catch
-            {
-                return null;
-            }
-        }
-    }
-}

+ 1 - 1
src/Essensoft.AspNetCore.Payment.Security/Essensoft.AspNetCore.Payment.Security.csproj

@@ -13,13 +13,13 @@
     <PackageProjectUrl>https://github.com/Essensoft/Payment</PackageProjectUrl>
     <RepositoryUrl>https://github.com/Essensoft/Payment</RepositoryUrl>
     <RepositoryType>git</RepositoryType>
-    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
     <PackageId>Essensoft.AspNetCore.Payment.Security</PackageId>
     <PackageTags>Essensoft;Payment;Security;</PackageTags>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
   </PropertyGroup>
 
   <ItemGroup>

+ 39 - 0
src/Essensoft.AspNetCore.Payment.Security/TripleDES.cs

@@ -0,0 +1,39 @@
+using System.Security.Cryptography;
+
+namespace Essensoft.AspNetCore.Payment.Security
+{
+    public class TripleDES
+    {
+        public static byte[] Encode(byte[] data, byte[] key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
+        {
+            using (var des = System.Security.Cryptography.TripleDES.Create())
+            {
+                des.Key = key;
+                des.IV = iv;
+                des.Mode = cipherMode;
+                des.Padding = paddingMode;
+
+                using (var ctf = des.CreateEncryptor())
+                {
+                    return ctf.TransformFinalBlock(data, 0, data.Length);
+                }
+            }
+        }
+
+        public static byte[] Decode(byte[] data, byte[] key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
+        {
+            using (var des = System.Security.Cryptography.TripleDES.Create())
+            {
+                des.Key = key;
+                des.IV = iv;
+                des.Mode = cipherMode;
+                des.Padding = paddingMode;
+
+                using (var ctf = des.CreateDecryptor())
+                {
+                    return ctf.TransformFinalBlock(data, 0, data.Length);
+                }
+            }
+        }
+    }
+}

+ 1 - 1
src/Essensoft.AspNetCore.Payment.UnionPay/Essensoft.AspNetCore.Payment.UnionPay.csproj

@@ -13,13 +13,13 @@
     <PackageProjectUrl>https://github.com/Essensoft/Payment</PackageProjectUrl>
     <RepositoryUrl>https://github.com/Essensoft/Payment</RepositoryUrl>
     <RepositoryType>git</RepositoryType>
-    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
     <PackageId>Essensoft.AspNetCore.Payment.UnionPay</PackageId>
     <PackageTags>Essensoft;Payment;UnionPay;银联支付;</PackageTags>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
   </PropertyGroup>
 
   <ItemGroup>

+ 1 - 1
src/Essensoft.AspNetCore.Payment.WeChatPay/Essensoft.AspNetCore.Payment.WeChatPay.csproj

@@ -13,13 +13,13 @@
     <PackageProjectUrl>https://github.com/Essensoft/Payment</PackageProjectUrl>
     <RepositoryUrl>https://github.com/Essensoft/Payment</RepositoryUrl>
     <RepositoryType>git</RepositoryType>
-    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
     <PackageId>Essensoft.AspNetCore.Payment.WeChatPay</PackageId>
     <PackageTags>Essensoft;Payment;WeChatPay;微信支付;</PackageTags>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
   </PropertyGroup>
 
   <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">

+ 3 - 1
src/Essensoft.AspNetCore.Payment.WeChatPay/WeChatPayNotifyClient.cs

@@ -1,4 +1,5 @@
 using System.IO;
+using System.Security.Cryptography;
 using System.Text;
 using System.Threading.Tasks;
 using Essensoft.AspNetCore.Payment.Security;
@@ -8,6 +9,7 @@ using Essensoft.AspNetCore.Payment.WeChatPay.Utility;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Options;
+using MD5 = Essensoft.AspNetCore.Payment.Security.MD5;
 
 namespace Essensoft.AspNetCore.Payment.WeChatPay
 {
@@ -49,7 +51,7 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay
             if (notify is WeChatPayRefundNotify)
             {
                 var key = MD5.Compute(options.Key).ToLower();
-                var data = AES.Decrypt((notify as WeChatPayRefundNotify).ReqInfo, key, AESCipherMode.ECB, AESPaddingMode.PKCS7);
+                var data = AES.Decrypt((notify as WeChatPayRefundNotify).ReqInfo, key, CipherMode.ECB, PaddingMode.PKCS7);
                 _logger.Log(options.LogLevel, "Decrypt Content:{data}", data); // AES-256-ECB 解密内容
                 notify = parser.Parse(body, data);
             }