金磐石权益服务平台


加解密规则

<h1>参数加密规则</h1> <h2>基础数据</h2> <p><strong>仅限测试使用</strong></p> <pre><code>{ &amp;quot;goodsId&amp;quot;:&amp;quot;20000102&amp;quot;// 商品ID }</code></pre> <h2>加密说明</h2> <p>1)将明文参数转成JSON字符串后通过国密SM4加密生成密文,密钥为appKey; 2)将密文字符串使用URLEncoder进行编码; 3)接收方对编码后的密文使用URLDecoder进行解码可得到原始密文; 4)将原始密文通过国密SM4解密得到明文参数。</p> <h2>JAVA示例</h2> <pre><code>import java.net.URLDecoder; import java.net.URLEncoder; import java.security.Key; import java.security.SecureRandom; import java.security.Security; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import org.bouncycastle.jce.provider.BouncyCastleProvider; public class Sm4Utils { private static final String ENCODING = &amp;quot;UTF-8&amp;quot;; private static final String ALGORITHM_NAME = &amp;quot;SM4&amp;quot;; private static final String ALGORITHM_NAME_ECB_PADDING = &amp;quot;SM4/ECB/PKCS7Padding&amp;quot;; static { Security.addProvider(new BouncyCastleProvider()); } /** * 生成ECB模式的加密/解密器 * * @param mode 加密/解密模式 * @param key 密钥 * @return ECB模式的加密/解密器 * @throws Exception */ private static Cipher generateEcbCipher(int mode, byte[] key) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_ECB_PADDING, BouncyCastleProvider.PROVIDER_NAME); Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME); cipher.init(mode, sm4Key); return cipher; } /** * 生成指定长度的SM4密钥 * * @param keySize 密钥长度,单位为比特 * @return SM4密钥 * @throws Exception * @explain */ public static byte[] generateKey(int keySize) throws Exception { KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME); kg.init(keySize, new SecureRandom()); return kg.generateKey().getEncoded(); } /** * 使用ECB模式和PKCS7Padding填充对数据进行加密 * * @param hexKey 16进制密钥 * @param paramStr 待加密字符串 * @return 加密后的密文 * @throws Exception */ public static String encryptEcb(String hexKey, String paramStr) throws Exception { byte[] cipherArray = encryptEcbPadding(hexKey.getBytes(ENCODING), paramStr.getBytes(ENCODING)); return Base64.encodeBase64String(cipherArray); } /** * 使用ECB模式和PKCS7Padding填充对数据进行加密 * * @param key 密钥 * @param data 待加密的数据 * @return 加密后的密文 * @throws Exception * @explain */ public static byte[] encryptEcbPadding(byte[] key, byte[] data) throws Exception { Cipher cipher = generateEcbCipher(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(data); } /** * 使用ECB模式和PKCS7Padding填充对密文进行解密 * * @param hexKey 16进制密钥 * @param cipherText 待解密的密文 * @return 解密后的明文 * @throws Exception */ public static String decryptEcb(String hexKey, String cipherText) throws Exception { byte[] srcData = decryptEcbPadding(hexKey.getBytes(ENCODING), Base64.decodeBase64(cipherText)); return new String(srcData, ENCODING); } /** * 使用ECB模式和PKCS7Padding填充对密文进行解密 * * @param key 密钥 * @param cipherText 待解密的密文 * @return 解密后的明文 * @throws Exception * @explain */ public static byte[] decryptEcbPadding(byte[] key, byte[] cipherText) throws Exception { Cipher cipher = generateEcbCipher(Cipher.DECRYPT_MODE, key); return cipher.doFinal(cipherText); } /** * 验证密文是否与明文一致 * * @param hexKey 16进制密钥 * @param cipherText 待验证的密文 * @param paramStr 明文 * @return 密文是否与明文一致 * @throws Exception * @explain */ public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception { byte[] decryptData = decryptEcbPadding(hexKey.getBytes(ENCODING), Base64.decodeBase64(cipherText)); return Arrays.equals(decryptData, paramStr.getBytes(ENCODING)); } public static void main(String[] args) { try { String json = &amp;quot;{\&amp;quot;goodsId\&amp;quot;:\&amp;quot;20000102\&amp;quot;}&amp;quot;; String key = &amp;quot;ortf9cc4hw5……&amp;quot;; String cipher = Sm4Utils.encryptEcb(key, json); System.out.println(&amp;quot;国密SM4加密解密:\r\n密钥:&amp;quot; + key + &amp;quot; \n加密内容:&amp;quot; + json + &amp;quot; \n加密后&amp;quot; + cipher); String encodedValue = URLEncoder.encode(cipher, &amp;quot;UTF-8&amp;quot;); System.out.println(encodedValue); String decodedValue = URLDecoder.decode(encodedValue, &amp;quot;UTF-8&amp;quot;); System.out.println(&amp;quot;Decoded value: &amp;quot; + decodedValue); // 比对加密解密信息 System.out.println(Sm4Utils.verifyEcb(key, decodedValue, json));// true json = Sm4Utils.decryptEcb(key, cipher); System.out.println(&amp;quot;国密SM4加密解密:\n密钥:&amp;quot; + key + &amp;quot; \n加密内容:&amp;quot; + cipher + &amp;quot; \n解密后:&amp;quot; + json); } catch (Exception e) { e.printStackTrace(); } } }</code></pre> <h2>POM依赖</h2> <pre><code> &amp;lt;dependency&amp;gt; &amp;lt;groupId&amp;gt;org.bouncycastle&amp;lt;/groupId&amp;gt; &amp;lt;artifactId&amp;gt;bcprov-jdk15on&amp;lt;/artifactId&amp;gt; &amp;lt;version&amp;gt;1.70&amp;lt;/version&amp;gt; &amp;lt;/dependency&amp;gt;</code></pre>

页面列表

ITEM_HTML