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
| package com.crypto.gm;
import java.math.BigInteger;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
public class SMHelper { private static final String DEFAULT_ENCODING = "UTF-8";
public static String sm4Encrypt(String key, String text) { try { byte[] data = SM4Util.encryptEbc(key.getBytes(DEFAULT_ENCODING), text.getBytes(DEFAULT_ENCODING)); return ByteUtils.toHexString(data); } catch (Exception e) { throw new RuntimeException(e); } }
public static String sm4Decrypt(String key, String cipherText) { try { byte[] data = SM4Util.decryptEbc(key.getBytes(DEFAULT_ENCODING), ByteUtils.fromHexString(cipherText)); return new String(data, DEFAULT_ENCODING); } catch (Exception e) { throw new RuntimeException(e); } }
public static String sm3Hash(String text) { try { return ByteUtils.toHexString(SM3Util.hash(text.getBytes(DEFAULT_ENCODING))); } catch (Exception e) { throw new RuntimeException(e); } }
public static boolean sm3verify(String text, String hash) { return hash.equals(sm3Hash(text)); }
public static SM2Keys sm2GengenerateKeys() { AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPair();
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters)keyPair.getPrivate(); String priKeyStr = ByteUtils.toHexString(priKey.getD().toByteArray());
ECPublicKeyParameters pubKey = (ECPublicKeyParameters)keyPair.getPublic(); String pubKeyStr = ByteUtils.toHexString(pubKey.getQ().getEncoded(false));
return new SM2Keys(priKeyStr, pubKeyStr); }
public static String sm2Encrypt(String publicKey, String text) { try { ECDomainParameters domainParams = SM2Util.getDomainParams(); ECPoint point = domainParams.getCurve().decodePoint(ByteUtils.fromHexString(publicKey)); ECPublicKeyParameters ecpKey = new ECPublicKeyParameters(point, domainParams); byte[] encData = SM2Util.encrypt(ecpKey, text.getBytes(DEFAULT_ENCODING)); return ByteUtils.toHexString(encData); } catch (Exception e) { throw new RuntimeException(e); } }
public static String sm2Decrypt(String privateKey, String cipherText) { try { BigInteger biKey = new BigInteger(privateKey, 16); ECPrivateKeyParameters ecpKey = new ECPrivateKeyParameters(biKey, SM2Util.getDomainParams()); byte[] decData = SM2Util.decrypt(ecpKey, ByteUtils.fromHexString(cipherText)); return new String(decData, DEFAULT_ENCODING); } catch (Exception e) { throw new RuntimeException(e); } }
public static String sm2Sign(String privateKey, String text) { try { BigInteger biKey = new BigInteger(privateKey, 16); ECPrivateKeyParameters ecpKey = new ECPrivateKeyParameters(biKey, SM2Util.getDomainParams()); byte[] signData = SM2Util.sign(ecpKey, null, text.getBytes(DEFAULT_ENCODING)); return ByteUtils.toHexString(signData); } catch (Exception e) { throw new RuntimeException(e); } }
public static boolean sm2Verify(String publicKey, String text, String sign) { try { ECDomainParameters domainParams = SM2Util.getDomainParams(); ECPoint point = domainParams.getCurve().decodePoint(ByteUtils.fromHexString(publicKey)); ECPublicKeyParameters ecpKey = new ECPublicKeyParameters(point, domainParams); return SM2Util.verify(ecpKey, null, text.getBytes(DEFAULT_ENCODING), ByteUtils.fromHexString(sign)); } catch (Exception e) { throw new RuntimeException(e); } }
public static class SM2Keys { private String privateKey; private String publicKey; public SM2Keys(String privateKey, String publicKey) { this.privateKey = privateKey; this.publicKey = publicKey; } public String getPrivateKey() { return privateKey; } public void setPrivateKey(String privateKey) { this.privateKey = privateKey; } public String getPublicKey() { return publicKey; } public void setPublicKey(String publicKey) { this.publicKey = publicKey; } } }
|