JAVA RSA类


public class RsaUtils {
public static final String KEY_ALGORITHM = "RSA";
private static final int KEY_SIZE = 1024;
private static final int ENCRYPT_SEGMENT_SIZE = 117;
private static final int DECRYPT_SEGMENT_SIZE = 128; public static HashMap<String, Object> getKeys() throws NoSuchAlgorithmException { HashMap<String, Object> map = new HashMap<>(2); KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGen.initialize(KEY_SIZE); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); map.put("public", publicKey); map.put("private", privateKey); return map; } public static RSAPublicKey getPublicKey(String modulus, String exponent, int radix) throws Exception { BigInteger b1 = new BigInteger(modulus, radix); BigInteger b2 = new BigInteger(exponent, radix); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2); return (RSAPublicKey) keyFactory.generatePublic(keySpec); } public static RSAPrivateKey getPrivateKey(String modulus, String exponent, int radix) { try { BigInteger b1 = new BigInteger(modulus, radix); BigInteger b2 = new BigInteger(exponent, radix); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2); return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { e.printStackTrace(); return null; } } public static byte[] encryptByPublicKey(byte[] data, RSAPublicKey key) throws Exception { List<byte[]> list = new ArrayList<>(); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey pubKey = key; Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, pubKey); int index = 0; while (index < data.length) { byte[] bytes; if (index + ENCRYPT_SEGMENT_SIZE > data.length) { bytes = Arrays.copyOfRange(data, index, data.length); } else { bytes = Arrays.copyOfRange(data, index, index + ENCRYPT_SEGMENT_SIZE); } byte[] encrypt = cipher.doFinal(bytes); list.add(encrypt); index += ENCRYPT_SEGMENT_SIZE; } return BitUtils.concatAll(list); } public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey key) throws Exception { if (data.length % DECRYPT_SEGMENT_SIZE != 0) { throw new RuntimeException("RSA密文长度不正确"); } List<byte[]> list = new ArrayList<>(); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateKey = key; Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); int index = 0; while (index < data.length) { byte[] bytes = Arrays.copyOfRange(data, index, index + DECRYPT_SEGMENT_SIZE); byte[] encrypt = cipher.doFinal(bytes); list.add(encrypt); index += DECRYPT_SEGMENT_SIZE; } return BitUtils.concatAll(list); } public static RSAPublicKey getPublicKey(String modulus, String exponent) { try { BigInteger b1 = new BigInteger(modulus); BigInteger b2 = new BigInteger(exponent); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2); return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getModulus(RSAPublicKey publicKey) { return publicKey.getModulus().toString(16).toUpperCase(); } public static String getExponent(RSAPublicKey publicKey) { return publicKey.getPublicExponent().toString(16); } public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, pubKey); return cipher.doFinal(data); } public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception { PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } public static RSAPrivateKey getPrivateKey(String modulus, String exponent) { try { BigInteger b1 = new BigInteger(modulus); BigInteger b2 = new BigInteger(exponent); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2); return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (Exception e) { e.printStackTrace(); return null; } }

Like (0)
Donate 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Previous 2023年12月26日 下午2:03
Next 2024年8月15日 上午8:17

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信