首页 > 编程知识 正文

java rsautilJAVA 通过RSA获取公钥私钥工具类,java通过类的地址获取类

时间:2023-05-06 16:22:07 阅读:190587 作者:2487

public class RSAUtil {

//生成秘钥对

public static KeyPair getKeyPair() throws Exception {

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

keyPairGenerator.initialize(1024);

KeyPair keyPair = keyPairGenerator.generateKeyPair();

return keyPair;

}

//获取公钥(Base64编码)

public static String getPublicKey(KeyPair keyPair) {

PublicKey publicKey = keyPair.getPublic();

byte[] bytes = publicKey.getEncoded();

return byte2Base64(bytes);

}

//获取私钥(Base64编码)

public static String getPrivateKey(KeyPair keyPair) {

PrivateKey privateKey = keyPair.getPrivate();

byte[] bytes = privateKey.getEncoded();

return byte2Base64(bytes);

}

//将Base64编码后的公钥转换成PublicKey对象

public static PublicKey string2PublicKey(String pubStr) throws Exception {

byte[] keyBytes = base642Byte(pubStr);

X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PublicKey publicKey = keyFactory.generatePublic(keySpec);

return publicKey;

}

//将Base64编码后的私钥转换成PrivateKey对象

public static PrivateKey string2PrivateKey(String priStr) throws Exception {

byte[] keyBytes = base642Byte(priStr);

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

return privateKey;

}

//公钥加密

public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception {

Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] bytes = cipher.doFinal(content);

return bytes;

}

//私钥解密

public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception {

Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.DECRYPT_MODE, privateKey);

byte[] bytes = cipher.doFinal(content);

return bytes;

}

//字节数组转Base64编码

public static String byte2Base64(byte[] bytes) {

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(bytes);

}

//Base64编码转字节数组

public static byte[] base642Byte(String base64Key) throws IOException {

BASE64Decoder decoder = new BASE64Decoder();

return decoder.decodeBuffer(base64Key);

}

public static void main(String[] args) {

}

}

如此便可以通过java得到公钥和私钥以方便后面使用。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。