BouncyCastle SM2/SM3/SM4
BouncyCastle SM2/SM3/SM4 为啥这些人命名的不是SM1, SM3 非对称SM2 SM4 对称!-- BouncyCastle 国密核心依赖 -- dependency groupIdorg.bouncycastle/groupId artifactIdbcprov-jdk18on/artifactId version1.78.5/version /dependencyimport org.bouncycastle.jce.provider.BouncyCastleProvider; import java.security.Security; // 全局注册一次即可 static { if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) null) { Security.addProvider(new BouncyCastleProvider()); } }import org.bouncycastle.crypto.digests.SM3Digest; import org.bouncycastle.util.encoders.Hex; /** * SM3 哈希加密 * param data 明文 * return 64位十六进制哈希串 */ public static String sm3Encrypt(String data) { byte[] srcData data.getBytes(); SM3Digest sm3Digest new SM3Digest(); sm3Digest.update(srcData, 0, srcData.length); byte[] digest new byte[sm3Digest.getDigestSize()]; sm3Digest.doFinal(digest, 0); // 转为十六进制字符串 return Hex.toHexString(digest); } // 测试 public static void main(String[] args) { String text 测试国密SM3加密; String result sm3Encrypt(text); System.out.println(SM3哈希值 result); }import org.bouncycastle.crypto.engines.SM4Engine; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.paddings.PKCS7Padding; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithIV; import org.bouncycastle.util.encoders.Hex; import java.nio.charset.StandardCharsets; import java.security.SecureRandom; /** * SM4 对称加密CBC模式 */ public class SM4Util { private static final int SM4_KEY_LENGTH 16; // SM4 密钥固定16字节 private static final int IV_LENGTH 16; // 生成随机 SM4 密钥16字节 public static String generateSm4Key() { byte[] key new byte[SM4_KEY_LENGTH]; new SecureRandom().nextBytes(key); return Hex.toHexString(key); } // 生成随机 IV public static String generateIv() { byte[] iv new byte[IV_LENGTH]; new SecureRandom().nextBytes(iv); return Hex.toHexString(iv); } /** * SM4 加密 */ public static String sm4Encrypt(String plainText, String keyHex, String ivHex) throws Exception { byte[] key Hex.decode(keyHex); byte[] iv Hex.decode(ivHex); byte[] data plainText.getBytes(StandardCharsets.UTF_8); PaddedBufferedBlockCipher cipher new PaddedBufferedBlockCipher( new CBCBlockCipher(new SM4Engine()), new PKCS7Padding()); cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); byte[] encrypted new byte[cipher.getOutputSize(data.length)]; int len cipher.processBytes(data, 0, data.length, encrypted, 0); len cipher.doFinal(encrypted, len); return Hex.toHexString(encrypted, 0, len); } /** * SM4 解密 */ public static String sm4Decrypt(String cipherText, String keyHex, String ivHex) throws Exception { byte[] key Hex.decode(keyHex); byte[] iv Hex.decode(ivHex); byte[] data Hex.decode(cipherText); PaddedBufferedBlockCipher cipher new PaddedBufferedBlockCipher( new CBCBlockCipher(new SM4Engine()), new PKCS7Padding()); cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); byte[] decrypted new byte[cipher.getOutputSize(data.length)]; int len cipher.processBytes(data, 0, data.length, decrypted, 0); len cipher.doFinal(decrypted, len); return new String(decrypted, 0, len, StandardCharsets.UTF_8); } // 测试 public static void main(String[] args) throws Exception { String text 测试国密SM4加密; String key generateSm4Key(); String iv generateIv(); String encrypt sm4Encrypt(text, key, iv); String decrypt sm4Decrypt(encrypt, key, iv); System.out.println(密钥 key); System.out.println(加密结果 encrypt); System.out.println(解密结果 decrypt); } }import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.util.PrivateKeyFactory; import org.bouncycastle.crypto.util.PublicKeyFactory; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; import java.security.Security; // SM2 工具类 public class SM2Util { static { if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) null) { Security.addProvider(new BouncyCastleProvider()); } } /** * 生成 SM2 密钥对公钥私钥 */ public static AsymmetricCipherKeyPair generateSm2KeyPair() { org.bouncycastle.crypto.generators.ECKeyPairGenerator generator new org.bouncycastle.crypto.generators.ECKeyPairGenerator(); generator.init(new org.bouncycastle.crypto.params.ECKeyGenerationParameters( org.bouncycastle.jce.ECNamedCurveTable.getParameterSpec(sm2p256v1).getDomainParameters(), new java.security.SecureRandom() )); return generator.generateKeyPair(); } // 公钥转十六进制 public static String publicKeyToHex(ECPublicKeyParameters publicKey) { return Hex.toHexString(publicKey.getQ().getEncoded(false)); } // 私钥转十六进制 public static String privateKeyToHex(ECPrivateKeyParameters privateKey) { return Hex.toHexString(privateKey.getD().toByteArray()); } /** * SM2 公钥加密 */ public static String sm2Encrypt(String data, String publicKeyHex) throws Exception { ECPublicKeyParameters publicKey (ECPublicKeyParameters) PublicKeyFactory.createKey(Hex.decode(publicKeyHex)); org.bouncycastle.crypto.engines.SM2Engine engine new org.bouncycastle.crypto.engines.SM2Engine(); engine.init(true, publicKey); byte[] encrypted engine.processBlock(data.getBytes(), 0, data.getBytes().length); return Hex.toHexString(encrypted); } /** * SM2 私钥解密 */ public static String sm2Decrypt(String cipherText, String privateKeyHex) throws Exception { ECPrivateKeyParameters privateKey (ECPrivateKeyParameters) PrivateKeyFactory.createKey(Hex.decode(privateKeyHex)); org.bouncycastle.crypto.engines.SM2Engine engine new org.bouncycastle.crypto.engines.SM2Engine(); engine.init(false, privateKey); byte[] decrypted engine.processBlock(Hex.decode(cipherText), 0, Hex.decode(cipherText).length); return new String(decrypted); } // 测试 public static void main(String[] args) throws Exception { // 生成密钥对 AsymmetricCipherKeyPair keyPair generateSm2KeyPair(); ECPublicKeyParameters publicKey (ECPublicKeyParameters) keyPair.getPublic(); ECPrivateKeyParameters privateKey (ECPrivateKeyParameters) keyPair.getPrivate(); String publicKeyHex publicKeyToHex(publicKey); String privateKeyHex privateKeyToHex(privateKey); String text 测试国密SM2加密; String encrypt sm2Encrypt(text, publicKeyHex); String decrypt sm2Decrypt(encrypt, privateKeyHex); System.out.println(公钥 publicKeyHex); System.out.println(私钥 privateKeyHex); System.out.println(加密 encrypt); System.out.println(解密 decrypt); } }emo国标加密解密
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2507927.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!