java模拟百度登录_大神们看看模拟百度登陆的问题

这个Java代码段实现了RSA加密算法的工具类,包括生成密钥对、公钥加密、私钥解密等功能。它能创建并保存公钥和私钥文件,并提供了从字符串中获取公钥和私钥的方法。此外,还提供了使用公钥加密和私钥解密的便捷方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

public class RSAUtil {

private static Cipher cipher;

static {

try {

cipher = Cipher.getInstance("RSA");

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

}

}

/**

* 生成密钥对

*

* @param filePath

* 生成密钥的路径

* @return

*/

public static Map generateKeyPair(String filePath) {

try {

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

// 密钥位数

keyPairGen.initialize(1024);

// 密钥对

KeyPair keyPair = keyPairGen.generateKeyPair();

// 公钥

PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

// 私钥

PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

// 得到公钥字符串

String publicKeyString = getKeyString(publicKey);

// 得到私钥字符串

String privateKeyString = getKeyString(privateKey);

// 将密钥对写入到文件

FileWriter pubfw = new FileWriter(filePath + "/publicKey.keystore");

FileWriter prifw = new FileWriter(filePath + "/privateKey.keystore");

BufferedWriter pubbw = new BufferedWriter(pubfw);

BufferedWriter pribw = new BufferedWriter(prifw);

pubbw.write(publicKeyString);

pribw.write(privateKeyString);

pubbw.flush();

pubbw.close();

pubfw.close();

pribw.flush();

pribw.close();

prifw.close();

// 将生成的密钥对返回

Map map = new HashMap();

map.put("publicKey", publicKeyString);

map.put("privateKey", privateKeyString);

return map;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 得到公钥

*

* @param key

* 密钥字符串(经过base64编码)

* @throws Exception

*/

public static PublicKey getPublicKey(String key) throws Exception {

byte[] keyBytes;

keyBytes = (new BASE64Decoder()).decodeBuffer(key);

X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

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

PublicKey publicKey = keyFactory.generatePublic(keySpec);

return publicKey;

}

/**

* 得到私钥

*

* @param key

* 密钥字符串(经过base64编码)

* @throws Exception

*/

public static PrivateKey getPrivateKey(String key) throws Exception {

byte[] keyBytes;

keyBytes = (new BASE64Decoder()).decodeBuffer(key);

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

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

PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

return privateKey;

}

/**

* 得到密钥字符串(经过base64编码)

*

* @return

*/

public static String getKeyString(Key key) throws Exception {

byte[] keyBytes = key.getEncoded();

String s = (new BASE64Encoder()).encode(keyBytes);

return s;

}

/**

* 使用公钥对明文进行加密,返回BASE64编码的字符串

*

* @param publicKey

* @param plainText

* @return

*/

public static String encrypt(PublicKey publicKey, String plainText) {

try {

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] enBytes = cipher.doFinal(plainText.getBytes());

return (new BASE64Encoder()).encode(enBytes);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

}

return null;

}

/**

* 使用keystore对明文进行加密

*

* @param publicKeystore

* 公钥文件路径

* @param plainText

* 明文

* @return

*/

public static String encrypt(String publicKeyString, String plainText) {

try {

// FileReader fr = new FileReader(publicKeystore);

// BufferedReader br = new BufferedReader(fr);

// String publicKeyString = "";

// String str;

// while ((str = br.readLine()) != null) {

// publicKeyString += str;

// }

// br.close();

// fr.close();

cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKeyString));

byte[] enBytes = cipher.doFinal(plainText.getBytes());

return (new BASE64Encoder()).encode(enBytes);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 使用私钥对明文密文进行解密

*

* @param privateKey

* @param enStr

* @return

*/

public static String decrypt(PrivateKey privateKey, String enStr) {

try {

cipher.init(Cipher.DECRYPT_MODE, privateKey);

byte[] deBytes = cipher.doFinal((new BASE64Decoder())

.decodeBuffer(enStr));

return new String(deBytes);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* 使用keystore对密文进行解密

*

* @param privateKeystore

* 私钥路径

* @param enStr

* 密文

* @return

*/

public static String decrypt(String privateKeystore, String enStr) {

try {

FileReader fr = new FileReader(privateKeystore);

BufferedReader br = new BufferedReader(fr);

String privateKeyString = "";

String str;

while ((str = br.readLine()) != null) {

privateKeyString += str;

}

br.close();

fr.close();

cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKeyString));

byte[] deBytes = cipher.doFinal((new BASE64Decoder())

.decodeBuffer(enStr));

return new String(deBytes);

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值