java rsa加密
程序员文章站
2022-07-04 14:42:59
...
为了保证信息传输的安全性,需要对重要数据进行加密传输,本文提供了java的rsa加密方法供参考。
外面再封装一层。
欢迎留言讨论。
package test.rsa; import java.io.ByteArrayOutputStream; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; public class RSAUtils { public static final String KEY_ALGORITHM = "RSA"; public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; private static final String PUBLIC_KEY = "RSAPublicKey"; private static final String PRIVATE_KEY = "RSAPrivateKey"; private static final int MAX_ENCRYPT_BLOCK = 117; private static final int MAX_DECRYPT_BLOCK = 128; /** * 获得公钥、私钥 * * @return * @throws Exception */ public static Map<String, Object> genKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance(KEY_ALGORITHM); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } /** * 签名 * @param data * @param privateKey * @return * @throws Exception */ public static String sign(byte[] data, String privateKey) throws Exception { byte[] keyBytes = Base64.getDecoder().decode(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(privateK); signature.update(data); return Base64.getEncoder().encodeToString(signature.sign()); } /** * 验证签名 * @param data * @param publicKey * @param sign * @return * @throws Exception */ public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { byte[] keyBytes = Base64.getDecoder().decode(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey publicK = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(publicK); signature.update(data); return signature.verify(Base64.getDecoder().decode(sign)); } /** * 私钥解密 * * @param encryptedData * @param privateKey * @return * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception { byte[] keyBytes = Base64.getDecoder().decode(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateK); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; int i = 0; byte[] cache; byte[] decryptedData; try { while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_DECRYPT_BLOCK; } decryptedData = out.toByteArray(); } catch (Exception e) { throw e; } finally { out.close(); } return decryptedData; } /** * 公钥加密 * * @param data * @param publicKey * @return * @throws Exception */ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { byte[] keyBytes = Base64.getDecoder().decode(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicK); int inputLen = data.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; int i = 0; byte[] cache; byte[] encryptedData; try { while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); } else { cache = cipher.doFinal(data, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_ENCRYPT_BLOCK; } encryptedData = out.toByteArray(); } catch (Exception e) { throw e; } finally { out.close(); } return encryptedData; } /** * 获得私钥 * * @param keyMap * @return * @throws Exception */ public static String getPrivateKey(final Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PRIVATE_KEY); return Base64.getEncoder().encodeToString(key.getEncoded()); } /** * 获得公钥 * * @param keyMap * @return * @throws Exception */ public static String getPublicKey(final Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PUBLIC_KEY); return Base64.getEncoder().encodeToString(key.getEncoded()); } }
外面再封装一层。
package test.rsa; import java.util.Base64; public class RSADecrypter { /** * 私钥解密 * @param encryptedDate * @param privateKey * @return * @throws Exception */ public static String decryptByPrivateKey(String encryptedDate, String privateKey) throws Exception { byte[] byte_data = Base64.getDecoder().decode(encryptedDate); byte[] decryptedData = RSAUtils.decryptByPrivateKey(byte_data, privateKey); return new String(decryptedData); } /** * 公钥加密 * @param mes * @param publishKey * @return * @throws Exception */ public static String encryptByPublishKey(String mes, String publishKey) throws Exception { byte[] encryptedData = RSAUtils.encryptByPublicKey(mes.getBytes(), publishKey); return Base64.getEncoder().encodeToString(encryptedData); } /** * 私钥解密 * @param encryptedDate * @return * @throws Exception */ public static String decryptByPrivateKey(String encryptedDate) throws Exception { return decryptByPrivateKey(encryptedDate, "私钥内容"); } /** * 公钥加密 * @param mes * @return * @throws Exception */ public static String encryptByPublishKey(String mes) throws Exception { return encryptByPublishKey(mes, "公钥内容"); } }
欢迎留言讨论。
上一篇: Java&keytool生成RSA密钥
下一篇: adb工具包使用方法 adb工具包adb