/*
* *********************************************************************************************************************
*
* !!
* .F88X
* X8888Y
* .}888888N;
* i888888N; .:! .I$WI:
* R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
* .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
* +888888N; .8888888Y "&&8Y.}8,
* ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
* ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
* 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
* .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
* 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
* i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
* .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
* .:R888888I
* .&888888I Copyright (c) 2016-2020. 博锐尚格科技股份有限公司
* ~8888'
* .!88~ All rights reserved.
*
* *********************************************************************************************************************
*/
package com.persagy.service.utils.crypt
import javax.crypto.BadPaddingException
import javax.crypto.Cipher
import javax.crypto.IllegalBlockSizeException
import javax.crypto.NoSuchPaddingException
import java.io.IOException
import java.security.*
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.security.spec.InvalidKeySpecException
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
import java.util.*
/**
* RSA安全编码组件
*
* @author Andy
*/
abstract class SRSACoderUtil : SCoderUtil() {
companion object {
const val KEY_ALGORITHM = "RSA"
const val SIGNATURE_ALGORITHM = "MD5withRSA"
private const val PUBLIC_KEY = "RSAPublicKey"
private const val PRIVATE_KEY = "RSAPrivateKey"
/**
* 用私钥对信息生成数字签名
*
* @param data 加密数据
* @param privateKey 私钥
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws InvalidKeyException
* @throws SignatureException
*/
@Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, InvalidKeyException::class, SignatureException::class)
fun sign(data: ByteArray, privateKey: String): String? {
// 解密由base64编码的私钥
val keyBytes = Base64.getDecoder().decode(privateKey)
// 构造PKCS8EncodedKeySpec对象
val pkcs8KeySpec = PKCS8EncodedKeySpec(keyBytes!!)
// KEY_ALGORITHM 指定的加密算法
val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
// 取私钥匙对象
val priKey = keyFactory.generatePrivate(pkcs8KeySpec)
// 用私钥对信息生成数字签名
val signature = Signature.getInstance(SIGNATURE_ALGORITHM)
signature.initSign(priKey)
signature.update(data)
return Base64.getEncoder().encodeToString(signature.sign())
} // Fun sign()
/**
* 校验数字签名
*
* @param data 加密数据
* @param publicKey 公钥
* @param sign 数字签名
* @return 校验成功返回true 失败返回false
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws InvalidKeyException
* @throws SignatureException
*/
@Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, InvalidKeyException::class, SignatureException::class)
fun verify(data: ByteArray, publicKey: String, sign: String): Boolean {
// 解密由base64编码的公钥
val keyBytes = Base64.getDecoder().decode(publicKey)
// 构造X509EncodedKeySpec对象
val keySpec = X509EncodedKeySpec(keyBytes!!)
// KEY_ALGORITHM 指定的加密算法
val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
// 取公钥匙对象
val pubKey = keyFactory.generatePublic(keySpec)
val signature = Signature.getInstance(SIGNATURE_ALGORITHM)
signature.initVerify(pubKey)
signature.update(data)
// 验证签名是否正常
return signature.verify(Base64.getDecoder().decode(sign))
} // Fun verify()
/**
* 解密
* 用私钥解密
*
* @param data
* @param key
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
@Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, NoSuchPaddingException::class, InvalidKeyException::class, IllegalBlockSizeException::class, BadPaddingException::class)
fun decryptByPrivateKey(data: ByteArray, key: String): ByteArray {
// 对密钥解密
val keyBytes = Base64.getDecoder().decode(key)
// 取得私钥
val pkcs8KeySpec = PKCS8EncodedKeySpec(keyBytes!!)
val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
val privateKey = keyFactory.generatePrivate(pkcs8KeySpec)
// 对数据解密
val cipher = Cipher.getInstance(keyFactory.algorithm)
cipher.init(Cipher.DECRYPT_MODE, privateKey)
return cipher.doFinal(data)
} // Fun decryptByPrivateKey()
/**
* 解密
* 用公钥解密
*
* @param data
* @param key
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
@Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, NoSuchPaddingException::class, InvalidKeyException::class, IllegalBlockSizeException::class, BadPaddingException::class)
fun decryptByPublicKey(data: ByteArray, key: String): ByteArray {
// 对密钥解密
val keyBytes = Base64.getDecoder().decode(key)
// 取得公钥
val x509KeySpec = X509EncodedKeySpec(keyBytes!!)
val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
val publicKey = keyFactory.generatePublic(x509KeySpec)
// 对数据解密
val cipher = Cipher.getInstance(keyFactory.algorithm)
cipher.init(Cipher.DECRYPT_MODE, publicKey)
return cipher.doFinal(data)
} // Fun decryptByPublicKey()
/**
* 用公钥加密
*
* @param data
* @param key
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
@Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, NoSuchPaddingException::class, InvalidKeyException::class, IllegalBlockSizeException::class, BadPaddingException::class)
fun encryptByPublicKey(data: ByteArray, key: String): ByteArray {
// 对公钥解密
val keyBytes = Base64.getDecoder().decode(key)
// 取得公钥
val x509KeySpec = X509EncodedKeySpec(keyBytes!!)
val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
val publicKey = keyFactory.generatePublic(x509KeySpec)
// 对数据加密
val cipher = Cipher.getInstance(keyFactory.algorithm)
cipher.init(Cipher.ENCRYPT_MODE, publicKey)
return cipher.doFinal(data)
} // Fun encryptByPublicKey()
/**
* 用私钥加密
*
* @param data
* @param key
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
@Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, NoSuchPaddingException::class, InvalidKeyException::class, IllegalBlockSizeException::class, BadPaddingException::class)
fun encryptByPrivateKey(data: ByteArray, key: String): ByteArray {
// 对密钥解密
val keyBytes = Base64.getDecoder().decode(key)
// 取得私钥
val pkcs8KeySpec = PKCS8EncodedKeySpec(keyBytes!!)
val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
val privateKey = keyFactory.generatePrivate(pkcs8KeySpec)
// 对数据加密
val cipher = Cipher.getInstance(keyFactory.algorithm)
cipher.init(Cipher.ENCRYPT_MODE, privateKey)
return cipher.doFinal(data)
} // Fun encryptByPrivateKey()
/**
* 取得私钥
*
* @param keyMap
* @return
*/
fun getPrivateKey(keyMap: Map): String? {
val key = keyMap[PRIVATE_KEY] as Key
return Base64.getEncoder().encodeToString(key.encoded)
} // Fun getPrivateKey()
/**
* 取得公钥
*
* @param keyMap
* @return
*/
fun getPublicKey(keyMap: Map): String? {
val key = keyMap[PUBLIC_KEY] as Key
return Base64.getEncoder().encodeToString(key.encoded)
} // Fun getPublicKey()
/**
* 初始化密钥
*
* @return
* @throws NoSuchAlgorithmException
*/
@Throws(NoSuchAlgorithmException::class)
fun initKey(): Map {
val keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM)
keyPairGen.initialize(1024)
val keyPair = keyPairGen.generateKeyPair()
// 公钥
val publicKey = keyPair.public as RSAPublicKey
// 私钥
val privateKey = keyPair.private as RSAPrivateKey
val keyMap = HashMap(2)
keyMap[PUBLIC_KEY] = publicKey
keyMap[PRIVATE_KEY] = privateKey
return keyMap
} // Fun initKey()
}
} // Class SRSACoderUtil