加解密规则
<h1>参数加密规则</h1>
<h2>基础数据</h2>
<p><strong>仅限测试使用</strong></p>
<pre><code>{
&quot;goodsId&quot;:&quot;20000102&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 = &quot;UTF-8&quot;;
private static final String ALGORITHM_NAME = &quot;SM4&quot;;
private static final String ALGORITHM_NAME_ECB_PADDING = &quot;SM4/ECB/PKCS7Padding&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 = &quot;{\&quot;goodsId\&quot;:\&quot;20000102\&quot;}&quot;;
String key = &quot;ortf9cc4hw5……&quot;;
String cipher = Sm4Utils.encryptEcb(key, json);
System.out.println(&quot;国密SM4加密解密:\r\n密钥:&quot; + key + &quot; \n加密内容:&quot; + json + &quot; \n加密后&quot; + cipher);
String encodedValue = URLEncoder.encode(cipher, &quot;UTF-8&quot;);
System.out.println(encodedValue);
String decodedValue = URLDecoder.decode(encodedValue, &quot;UTF-8&quot;);
System.out.println(&quot;Decoded value: &quot; + decodedValue);
// 比对加密解密信息
System.out.println(Sm4Utils.verifyEcb(key, decodedValue, json));// true
json = Sm4Utils.decryptEcb(key, cipher);
System.out.println(&quot;国密SM4加密解密:\n密钥:&quot; + key + &quot; \n加密内容:&quot; + cipher + &quot; \n解密后:&quot; + json);
} catch (Exception e) {
e.printStackTrace();
}
}
}</code></pre>
<h2>POM依赖</h2>
<pre><code> &lt;dependency&gt;
&lt;groupId&gt;org.bouncycastle&lt;/groupId&gt;
&lt;artifactId&gt;bcprov-jdk15on&lt;/artifactId&gt;
&lt;version&gt;1.70&lt;/version&gt;
&lt;/dependency&gt;</code></pre>