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
| import java.io.*; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils;
public class RSASignUtil {
private static final String SIGNATURE_INSTANCE = "SHA256WithRSA"; private static final String RSA_INSTANCE = "RSA"; private static final int MAX_ENCRYPT_BLOCK = 245; private static final int MAX_DECRYPT_BLOCK = 256;
public static String signBySHA256WithRSA(String pvkString, String source) throws Exception { Signature signature = Signature.getInstance(SIGNATURE_INSTANCE);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.decodeBase64(pvkString.getBytes())); KeyFactory ky = KeyFactory.getInstance(RSA_INSTANCE); PrivateKey privateKey = ky.generatePrivate(spec);
signature.initSign(privateKey); signature.update(source.getBytes()); byte result[] = signature.sign();
return Base64.encodeBase64String(result); }
public static boolean verifySignBySHA256WithRSA(String pukString, String signValue, String source) throws Exception { X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.decodeBase64(pukString.getBytes())); KeyFactory ky = KeyFactory.getInstance(RSA_INSTANCE); PublicKey pukKey = ky.generatePublic(spec);
Signature signature = Signature.getInstance(SIGNATURE_INSTANCE); signature.initVerify(pukKey); signature.update(source.getBytes()); return signature.verify(Base64.decodeBase64(signValue.getBytes())); }
public static byte[] encryptByPuk(byte[] data, String publicKey) throws Exception { byte[] decoded = Base64.decodeBase64(publicKey); PublicKey pubKey = KeyFactory.getInstance(RSA_INSTANCE).generatePublic(new X509EncodedKeySpec(decoded)); try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { Cipher cipher = Cipher.getInstance(RSA_INSTANCE); cipher.init(Cipher.ENCRYPT_MODE, pubKey); int offSet = 0; while (data.length > offSet) { int length = data.length - offSet > MAX_ENCRYPT_BLOCK ? MAX_ENCRYPT_BLOCK : data.length - offSet; byte[] cache = cipher.doFinal(data, offSet, length); out.write(cache, 0, cache.length); offSet += length; } return out.toByteArray(); } }
public static byte[] decryptByPvk(byte[] data, String privateKey) throws Exception { byte[] decoded = Base64.decodeBase64(privateKey); PrivateKey priKey = KeyFactory.getInstance(RSA_INSTANCE).generatePrivate(new PKCS8EncodedKeySpec(decoded)); try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { Cipher cipher = Cipher.getInstance(RSA_INSTANCE); cipher.init(Cipher.DECRYPT_MODE, priKey); int offSet = 0; while (data.length > offSet) { int length = data.length - offSet > MAX_DECRYPT_BLOCK ? MAX_DECRYPT_BLOCK : data.length - offSet; byte[] cache = cipher.doFinal(data, offSet, length); out.write(cache, 0, cache.length); offSet += length; } return out.toByteArray(); } }
private static String getPemPukkey(String pukPath) { String pukString = null; try (InputStream in = new FileInputStream(pukPath)) { pukString = IOUtils.toString(in); pukString = pukString.replace("-----BEGIN PUBLIC KEY-----", ""); pukString = pukString.replace("-----END PUBLIC KEY-----", ""); pukString = pukString.replaceAll("\n", ""); } catch (Exception e) { e.printStackTrace(); } return pukString; }
private static String getPemPriKey(String pvkPath) { String pvkString = null; try (InputStream in = new FileInputStream(pvkPath)) { pvkString = IOUtils.toString(in); pvkString = pvkString.replace("-----BEGIN PRIVATE KEY-----", ""); pvkString = pvkString.replace("-----END PRIVATE KEY-----", ""); pvkString = pvkString.replaceAll("\n", ""); } catch (Exception e) { e.printStackTrace(); } return pvkString; }
public static Pair<RSAPublicKey, RSAPrivateKey> initKey() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(RSA_INSTANCE); keyPairGen.initialize(2048); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); return new Pair<>(publicKey, privateKey); }
public static void main(String[] args) throws Exception { String pukPath = "C:/Users/zm/Desktop/puk.pem"; String publicKey = getPemPukkey(pukPath);
String pvkPath = "C:/Users/zm/Desktop/pvk.pem"; String privateKey = getPemPriKey(pvkPath);
String value = "43554656786889";
byte[] encrypt = encryptByPuk(value.getBytes(), publicKey); System.out.println("encrypt:" + Base64.encodeBase64String(encrypt)); byte[] decrypt = decryptByPvk(encrypt, privateKey); System.out.println("decrypt:" + new String(decrypt));
String sign = signBySHA256WithRSA(privateKey, value); System.out.println("sign:" + sign); boolean isSuccess = verifySignBySHA256WithRSA(publicKey, sign, value); System.out.println(isSuccess); } }
|