| | |
| | | package com.dayu.qiheonlinelibrary.net; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.KeyFactory; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.PublicKey; |
| | | import java.security.cert.CertificateFactory; |
| | | import java.security.cert.X509Certificate; |
| | | import java.security.spec.InvalidKeySpecException; |
| | | import java.security.spec.X509EncodedKeySpec; |
| | | import java.util.Base64; |
| | | |
| | | import javax.crypto.BadPaddingException; |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.IllegalBlockSizeException; |
| | | import javax.crypto.NoSuchPaddingException; |
| | | |
| | | /** |
| | | * author: zuo |
| | |
| | | */ |
| | | public class RSAUtile { |
| | | |
| | | public static byte[] encryptByPublicKey(String data, String publicKey) throws Exception { |
| | | // 得到公钥 |
| | | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey.getBytes(StandardCharsets.UTF_8)); |
| | | KeyFactory kf = KeyFactory.getInstance("RSA"); |
| | | PublicKey keyPublic = kf.generatePublic(keySpec); |
| | | |
| | | // 加密数据 |
| | | Cipher cp = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
| | | cp.init(Cipher.ENCRYPT_MODE, keyPublic); |
| | | return cp.doFinal(data.getBytes(StandardCharsets.UTF_8)); |
| | | } |
| | | |
| | | |
| | | public static String encryptByPublicKey(String data, String publicKeyPEM) { |
| | | try { |
| | | // 得到公钥 |
| | | publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----", "") |
| | | .replace("-----END PUBLIC KEY-----", "") |
| | | .replaceAll("\\s+", ""); |
| | | // 解码Base64编码的公钥 |
| | | byte[] decoded = new byte[0]; |
| | | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { |
| | | decoded = Base64.getDecoder().decode(publicKeyPEM); |
| | | } else { |
| | | decoded = android.util.Base64.decode(publicKeyPEM, android.util.Base64.DEFAULT); |
| | | } |
| | | // 生成公钥 |
| | | X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded); |
| | | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| | | PublicKey publicKey = keyFactory.generatePublic(spec); |
| | | // 加密数据 |
| | | Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
| | | cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
| | | byte[] encryptedBytes = cipher.doFinal(data.getBytes("UTF-8")); |
| | | String encryptedData = null; |
| | | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { |
| | | encryptedData = Base64.getEncoder().encodeToString(encryptedBytes); |
| | | } else { |
| | | encryptedData = android.util.Base64.encodeToString(encryptedBytes, android.util.Base64.DEFAULT); |
| | | } |
| | | return encryptedData; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | |
| | | } |