123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- /*
- * *********************************************************************************************************************
- *
- * !!
- * .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()
- /**
- * 解密<br></br>
- * 用私钥解密
- *
- * @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()
- /**
- * 解密<br></br>
- * 用公钥解密
- *
- * @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, Any>): String? {
- val key = keyMap[PRIVATE_KEY] as Key
- return Base64.getEncoder().encodeToString(key.encoded)
- } // Fun getPrivateKey()
- /**
- * 取得公钥
- *
- * @param keyMap
- * @return
- */
- fun getPublicKey(keyMap: Map<String, Any>): 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<String, Any> {
- 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<String, Any>(2)
- keyMap[PUBLIC_KEY] = publicKey
- keyMap[PRIVATE_KEY] = privateKey
- return keyMap
- } // Fun initKey()
- }
- } // Class SRSACoderUtil
|