API渠道服务-接口文档

API渠道服务,提供除充值API以外的标准接口能力。


AES加解密规则

<ul> <li>采用标准AES加解密</li> <li>iv偏移量:空</li> <li>算法类型: .NET:AES/ECB/PKCS7Padding(本API接口采用.NET C#开发) Java:AES/ECB/PKCS5Padding</li> </ul> <p><strong>.NET C# 示例代码</strong></p> <pre><code class="language-csharp">using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities.Encoders; namespace Tool; /// &amp;lt;summary&amp;gt; /// AES加解密 /// &amp;lt;/summary&amp;gt; public static class AES { /// &amp;lt;summary&amp;gt; /// 加密 /// &amp;lt;/summary&amp;gt; /// &amp;lt;param name=&amp;quot;data&amp;quot;&amp;gt;待加密原文数据&amp;lt;/param&amp;gt; /// &amp;lt;param name=&amp;quot;key&amp;quot;&amp;gt;密钥&amp;lt;/param&amp;gt; /// &amp;lt;param name=&amp;quot;iv&amp;quot;&amp;gt;偏移量,ECB模式不用填写!&amp;lt;/param&amp;gt; /// &amp;lt;param name=&amp;quot;algorithm&amp;quot;&amp;gt;算法类型&amp;lt;/param&amp;gt; /// &amp;lt;returns&amp;gt;密文数据&amp;lt;/returns&amp;gt; public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv = null, string algorithm = &amp;quot;AES/ECB/PKCS7Padding&amp;quot;) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (key == null) { throw new ArgumentNullException(nameof(key)); } var cipher = CipherUtilities.GetCipher(algorithm); if (iv == null) { cipher.Init(true, ParameterUtilities.CreateKeyParameter(&amp;quot;AES&amp;quot;, key)); } else { cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter(&amp;quot;AES&amp;quot;, key), iv)); } return cipher.DoFinal(data); } /// &amp;lt;summary&amp;gt; /// 解密 /// &amp;lt;/summary&amp;gt; /// &amp;lt;param name=&amp;quot;data&amp;quot;&amp;gt;待解密数据&amp;lt;/param&amp;gt; /// &amp;lt;param name=&amp;quot;key&amp;quot;&amp;gt;密钥&amp;lt;/param&amp;gt; /// &amp;lt;param name=&amp;quot;iv&amp;quot;&amp;gt;偏移量,ECB模式不用填写!&amp;lt;/param&amp;gt; /// &amp;lt;param name=&amp;quot;algorithm&amp;quot;&amp;gt;算法类型&amp;lt;/param&amp;gt; /// &amp;lt;returns&amp;gt;未加密原文数据&amp;lt;/returns&amp;gt; public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv = null, string algorithm = &amp;quot;AES/ECB/PKCS7Padding&amp;quot;) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (key == null) { throw new ArgumentNullException(nameof(key)); } var cipher = CipherUtilities.GetCipher(algorithm); if (iv == null) { cipher.Init(false, ParameterUtilities.CreateKeyParameter(&amp;quot;AES&amp;quot;, key)); } else { cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter(&amp;quot;AES&amp;quot;, key), iv)); } return cipher.DoFinal(data); } }</code></pre> <pre><code class="language-csharp"> string aesKey = &amp;quot;b3c5d1d8945ea04402faf304c76f9494&amp;quot;; string originalText = &amp;quot;{\&amp;quot;phone\&amp;quot;:\&amp;quot;17620132013\&amp;quot;}&amp;quot;; //加密 var encryptedText = Convert.ToBase64String(AES.Encrypt(Encoding.UTF8.GetBytes(originalText), Encoding.UTF8.GetBytes(aesKey), null, &amp;quot;AES/ECB/PKCS7Padding&amp;quot;)); //解密 var decryptedText = Encoding.UTF8.GetString(AES.Decrypt(Convert.FromBase64String(encryptedText), Encoding.UTF8.GetBytes(aesKey), null, &amp;quot;AES/ECB/PKCS7Padding&amp;quot;)); </code></pre> <p><strong>Java 示例代码</strong></p> <pre><code class="language-java">package tool; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Base64; public class AES { /** * 加密 * @param plainText 明文 * @param secretKey 密钥 * @param algorithm 算法类型 AES/ECB/PKCS5Padding * @return Base64编码的加密结果 */ public static String encrypt(String plainText, String secretKey, String algorithm) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), &amp;quot;AES&amp;quot;); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encryptedBytes); } /** * 解密 * @param encryptedText Base64编码的密文 * @param secretKey 密钥 * @param algorithm 算法类型 AES/ECB/PKCS5Padding * @return 解密后的明文 */ public static String decrypt(String encryptedText, String secretKey, String algorithm) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), &amp;quot;AES&amp;quot;); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decodedBytes = Base64.getDecoder().decode(encryptedText); byte[] decryptedBytes = cipher.doFinal(decodedBytes); return new String(decryptedBytes, StandardCharsets.UTF_8); } public static void main(String[] args) throws Exception { String aesKey = &amp;quot;b3c5d1d8945ea04402faf304c76f9494&amp;quot;; String originalText = &amp;quot;{\&amp;quot;phone\&amp;quot;:\&amp;quot;17620132013\&amp;quot;}&amp;quot;; // 加密 String encryptedText = encrypt(originalText, aesKey, &amp;quot;AES/ECB/PKCS5Padding&amp;quot;); System.out.println(&amp;quot;加密结果: &amp;quot; + encryptedText); // 解密 String decryptedText = decrypt(encryptedText, aesKey, &amp;quot;AES/ECB/PKCS5Padding&amp;quot;); System.out.println(&amp;quot;解密结果: &amp;quot; + decryptedText); } }</code></pre>

页面列表

ITEM_HTML