c#对接顺丰图片文件
顺丰文档:https://open.sf-express.com/#/Api/expressApi/389652?level3=991999
文档使用的是AES加密,CBC模式,填充模式PKCS5Padding
实际上PKCS5Padding与PKCS7Padding基本上也是可以通用的,具体差异查看百度。
所以在c#中使用PaddingMode.PKCS7;
使用.net 5 控制台程序,编写测试代码
代码如下:
using System; using System.IO; using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; namespace SFPicture { class Program { private static string SECRET = "axjGikUwgYVKiJ3A"; public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static void Main(string[] args) { string responseMsg = "{\n" + " \"companyLogo\": \"SF\",\n" + " \"content\": \"njdEklz2DvgAnu6GNrtzrxZrta+jg6Z9DptuedRafk2aJ8Lbo6FH7c8Z4ZcS8I7MTV1Q02GaBacX\\nOrT" + "IfNesEsrVE1POLzPUXfnRznmVbXcPz0mknRfU1qtMSJBtBJdT3loY6Fnd30oatPmNaMey7G2x\\nO4Sh3V4EbeCt3GrBwEMQk/qwBA" + "IhS56B4rquN2lX3fvJENJt/boklc1pz0JybTHgyvp1yTRIyqkR\\nMjfcfrTks1OF/WJ5Iuy/dYGe8HFooHyYS+5y+agVJWjIXleOZ" + "gXUwm/WcLGTxsjJgiVdGDQhPsJU\\nwU86HOqapeJbGSFDw0Nzxzqu53rXVeqYxJ+UdsUGSiTL7+0kStTWmtknxoj/1XpeueUkytbN" + "jgP9\\nickUKA6V/k/+PkVKoWbyKO4K7CGtljrAvOIwnWPwTsygnFRTsiEG7w+QzAQuMqIFwkSBSjKesv7d\\nLkizTOENT18CkcNi" + "tSZJ2fOv0liqdU0gfGpFK3hxlKf5fr4Q5YKl0UfZdr3YEQl0QqW+pViRjezI\\ndS7Fyk4984DHab4L+se6HTUf1v+OTzQ47cnKPc" + "SNpYjRRB8HSeI+xLgAI7k+oUU/1xgWXZlMO2Jh\\nMG96iRI92cXHj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6t/ucjPRnE+Emcyco\\nt4oLVP3jaSrebQgg7TIXL84Ib9we/YbT63u0LDZp95" + "jnZkrmjBPSz9YhVuD/oRgznuVjm8L5VTkS\\nWCQZ0opBwYfaW/NVaZhg5cDVcv+4wRCLp4S3zmMerM02FaUtoxShK9VwQ6K4nq4al" + "/1mgFRR4ktG\\nGyhT0WEhNbW08FWuet8=\",\n" + " \"waybillNo\": \"SF1011854328486\"\n" + "}"; transferToImage(responseMsg); } public static void transferToImage(string msg) { var jsonObj=JsonConvert.DeserializeObject<ReceiveMsg>(msg); byte[] decode = Convert.FromBase64String(jsonObj.content); decode = AES_Decode(decode, SECRET); decode = Convert.FromBase64String(Encoding.UTF8.GetString(decode)); using (FileStream fs = new FileStream("E:/解密顺丰/SF.png", FileMode.Create)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(decode, 0, decode.Length); } } public static byte[] AES_Decode(byte[] textBytes, string key) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] encryptedData = textBytes; byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return plainText; } } public class ReceiveMsg { public string companyLogo { get; set; } public string content { get; set; } public string waybillNo { get; set; } } }
执行后,能顺利解析出文档上提供的图片