共计 709 个字符,预计需要花费 2 分钟才能阅读完成。
java 中会用到很多的 RSA 加密解密。不过个别能够通过接口获取到他人的公钥。而后进行数据的加密。在传给他人。他人会拿着密钥进行解密获取数据。
/** | |
* RSA 公钥加密 | |
* | |
* @param password 待加密的明码 | |
* @param publicKey 公钥 | |
* @return 密文 | |
*/ | |
public static String encryptRSA(String password, String publicKey) { | |
// base64 编码的公钥 | |
try {byte[] decoded = Base64.decode(publicKey); | |
RSAPublicKey pubKey = | |
(RSAPublicKey) | |
KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); | |
// RSA 加密 | |
Cipher cipher = Cipher.getInstance("RSA"); | |
cipher.init(Cipher.ENCRYPT_MODE, pubKey); | |
//** 此处 Base64 编码,开发者能够应用本人的库 ** | |
String outStr = Base64.encode(cipher.doFinal(password.getBytes("UTF-8"))); | |
return outStr; | |
} catch (Exception e) { } | |
return null; | |
} |
这个就是 java 应用 RSA 加密。
用 python 去应用他人的公钥进行加密也是很简略的。
首先先下载相干包
pip install rsa | |
pip install pycryptodome |
在应用办法进行加密解密
这样就能够通过获取他人的公钥加密,发送给他人。
正文完