左晓为主开发手持机充值管理机
zuoxiao
2025-03-06 7f55711fd88ff5dc67bbd386fbecc7bd50bd98c2
qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/net/RSAUtile.java
@@ -1,11 +1,22 @@
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
@@ -15,18 +26,42 @@
 */
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 "";
    }
}