背景:
使用公钥加密数据,然后使用私钥解密。网上找的现成的java方法,然后转成kotlin。
import org.apache.commons.codec.binary.Base64
import java.security.KeyFactory
import java.security.KeyPairGenerator
import java.security.PrivateKey
import java.security.PublicKey
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
import java.time.LocalDate
import javax.crypto.Cipher
/**
* @Author: anson
* @Date: 2022/2/4 4:02 PM
*/
object RSAUtils {
/**
* 公钥加密私钥解密
*/
fun test1(keyPair: RSAKeyPair, source: String) {
println("***************** 公钥加密私钥解密开始 *****************")
println("加密前:$source")
val text1 = encryptByPublicKey(keyPair.publicKey, source)
println("加密后:$text1")
val text2 = decryptByPrivateKey(keyPair.privateKey, text1)
println("解密后:$text2")
if (source == text2) {
println("解密字符串和原始字符串一致,解密成功")
} else {
println("解密字符串和原始字符串不一致,解密失败")
}
println("***************** 公钥加密私钥解密结束 *****************")
}
/**
* 私钥加密公钥解密
*
* @throws Exception
*/
fun test2(keyPair: RSAKeyPair, source: String) {
println("***************** 私钥加密公钥解密开始 *****************")
val text1 = encryptByPrivateKey(keyPair.privateKey, source)
val text2 = decryptByPublicKey(keyPair.publicKey, text1)
println("加密前:$source")
println("加密后:$text1")
println("解密后:$text2")
if (source == text2) {
println("解密字符串和原始字符串一致,解密成功")
} else {
println("解密字符串和原始字符串不一致,解密失败")
}
println("***************** 私钥加密公钥解密结束 *****************")
}
/**
* 公钥解密
*
* @param publicKeyText
* @param text
* @return
*/
fun decryptByPublicKey(publicKeyText: String, text: String): String {
val x509EncodedKeySpec = X509EncodedKeySpec(Base64.decodeBase64(publicKeyText))
val keyFactory: KeyFactory = KeyFactory.getInstance("RSA")
val publicKey: PublicKey = keyFactory.generatePublic(x509EncodedKeySpec)
val cipher: Cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.DECRYPT_MODE, publicKey)
val result: ByteArray = cipher.doFinal(Base64.decodeBase64(text))
return String(result)
}
/**
* 私钥加密
*
* @param privateKeyText
* @param text
*/
fun encryptByPrivateKey(privateKeyText: String, text: String): String {
val pkcs8EncodedKeySpec = PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText))
val keyFactory: KeyFactory = KeyFactory.getInstance("RSA")
val privateKey: PrivateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec)
val cipher: Cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.ENCRYPT_MODE, privateKey)
val result: ByteArray = cipher.doFinal(text.toByteArray())
return Base64.encodeBase64String(result)
}
/**
* 私钥解密
*
* @param privateKeyText
* @param text
* @return
* @throws Exception
*/
fun decryptByPrivateKey(privateKeyText: String, text: String): String {
val pkcs8EncodedKeySpec5 = PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText))
val keyFactory: KeyFactory = KeyFactory.getInstance("RSA")
val privateKey: PrivateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5)
val cipher: Cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.DECRYPT_MODE, privateKey)
val result: ByteArray = cipher.doFinal(Base64.decodeBase64(text))
return String(result)
}
/**
* 公钥加密
*
* @param publicKeyText
* @param text
* @return
*/
fun encryptByPublicKey(publicKeyText: String, text: String): String {
val x509EncodedKeySpec2 = X509EncodedKeySpec(Base64.decodeBase64(publicKeyText))
val keyFactory: KeyFactory = KeyFactory.getInstance("RSA")
val publicKey: PublicKey = keyFactory.generatePublic(x509EncodedKeySpec2)
val cipher: Cipher = Cipher.getInstance("RSA")
cipher.init(Cipher.ENCRYPT_MODE, publicKey)
val result: ByteArray = cipher.doFinal(text.toByteArray())
return Base64.encodeBase64String(result)
}
/**
* 构建RSA密钥对
*
*/
fun generateKeyPair(): RSAKeyPair {
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
keyPairGenerator.initialize(512)
val keyPair = keyPairGenerator.generateKeyPair()
val rsaPublicKey = keyPair.public as RSAPublicKey
val rsaPrivateKey = keyPair.private as RSAPrivateKey
val publicKeyString: String = Base64.encodeBase64String(rsaPublicKey.encoded)
val privateKeyString: String = Base64.encodeBase64String(rsaPrivateKey.encoded)
return RSAKeyPair(publicKeyString, privateKeyString)
}
/**
* RSA密钥对对象
*/
data class RSAKeyPair(val publicKey: String, val privateKey: String)
}