1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; import java.nio.charset.StandardCharsets; import java.security.*; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
public class SM2Util {
private static final String ALGORITHM_NAME = "SM2";
static { if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { Security.addProvider(new BouncyCastleProvider()); } }
public static KeyPair generateKeyPair() throws GeneralSecurityException { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME); keyPairGenerator.initialize(256, new SecureRandom()); return keyPairGenerator.generateKeyPair(); } catch (Exception e) { throw new GeneralSecurityException("Failed to generate SM2 key pair", e); } }
public static String sign(byte[] data, PrivateKey privateKey) throws GeneralSecurityException { try { Signature signer = Signature.getInstance("SM3withSM2", BouncyCastleProvider.PROVIDER_NAME); signer.initSign(privateKey); signer.update(data); byte[] signature = signer.sign(); return Hex.toHexString(signature); } catch (Exception e) { throw new GeneralSecurityException("SM2 signature failed", e); } }
public static String sign(String data, PrivateKey privateKey) throws GeneralSecurityException { return sign(data.getBytes(), privateKey); }
public static boolean verify(byte[] data, String sign, PublicKey publicKey) throws GeneralSecurityException { try { Signature verifier = Signature.getInstance("SM3withSM2", BouncyCastleProvider.PROVIDER_NAME); verifier.initVerify(publicKey); verifier.update(data); byte[] signature = Hex.decode(sign); return verifier.verify(signature); } catch (Exception e) { throw new GeneralSecurityException("SM2 signature verification failed", e); } }
public static boolean verify(String data, String sign, PublicKey publicKey) throws GeneralSecurityException { return verify(data.getBytes(), sign, publicKey); }
public static PublicKey decodePublicKey(byte[] encoded) throws GeneralSecurityException { try { KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded); return keyFactory.generatePublic(keySpec); } catch (Exception e) { throw new GeneralSecurityException("Failed to decode public key", e); } }
public static PrivateKey decodePrivateKey(byte[] encoded) throws GeneralSecurityException { try { KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); return keyFactory.generatePrivate(keySpec); } catch (Exception e) { throw new GeneralSecurityException("Failed to decode private key", e); } }
public static String encrypt(byte[] data, PublicKey publicKey) throws GeneralSecurityException { try { javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("SM2", "BC"); cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal(data); return org.bouncycastle.util.encoders.Hex.toHexString(encryptedBytes); } catch (Exception e) { throw new GeneralSecurityException("SM2 encryption failed", e); } }
public static String encrypt(String data, PublicKey publicKey) throws GeneralSecurityException { return encrypt(data.getBytes(StandardCharsets.UTF_8), publicKey); }
public static byte[] decrypt(byte[] data, PrivateKey privateKey) throws GeneralSecurityException { try { javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("SM2", "BC"); cipher.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } catch (Exception e) { throw new GeneralSecurityException("SM2 decryption failed", e); } }
public static String decrypt(String data, PrivateKey privateKey) throws GeneralSecurityException { byte[] decryptedBytes = decrypt(org.bouncycastle.util.encoders.Hex.decode(data), privateKey); return new String(decryptedBytes, StandardCharsets.UTF_8); } }
|