SRSACoderUtil.kt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2016-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. package com.persagy.service.utils.crypt
  27. import javax.crypto.BadPaddingException
  28. import javax.crypto.Cipher
  29. import javax.crypto.IllegalBlockSizeException
  30. import javax.crypto.NoSuchPaddingException
  31. import java.io.IOException
  32. import java.security.*
  33. import java.security.interfaces.RSAPrivateKey
  34. import java.security.interfaces.RSAPublicKey
  35. import java.security.spec.InvalidKeySpecException
  36. import java.security.spec.PKCS8EncodedKeySpec
  37. import java.security.spec.X509EncodedKeySpec
  38. import java.util.*
  39. /**
  40. * RSA安全编码组件
  41. *
  42. * @author Andy
  43. */
  44. abstract class SRSACoderUtil : SCoderUtil() {
  45. companion object {
  46. const val KEY_ALGORITHM = "RSA"
  47. const val SIGNATURE_ALGORITHM = "MD5withRSA"
  48. private const val PUBLIC_KEY = "RSAPublicKey"
  49. private const val PRIVATE_KEY = "RSAPrivateKey"
  50. /**
  51. * 用私钥对信息生成数字签名
  52. *
  53. * @param data 加密数据
  54. * @param privateKey 私钥
  55. * @return
  56. * @throws IOException
  57. * @throws NoSuchAlgorithmException
  58. * @throws InvalidKeySpecException
  59. * @throws InvalidKeyException
  60. * @throws SignatureException
  61. */
  62. @Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, InvalidKeyException::class, SignatureException::class)
  63. fun sign(data: ByteArray, privateKey: String): String? {
  64. // 解密由base64编码的私钥
  65. val keyBytes = Base64.getDecoder().decode(privateKey)
  66. // 构造PKCS8EncodedKeySpec对象
  67. val pkcs8KeySpec = PKCS8EncodedKeySpec(keyBytes!!)
  68. // KEY_ALGORITHM 指定的加密算法
  69. val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
  70. // 取私钥匙对象
  71. val priKey = keyFactory.generatePrivate(pkcs8KeySpec)
  72. // 用私钥对信息生成数字签名
  73. val signature = Signature.getInstance(SIGNATURE_ALGORITHM)
  74. signature.initSign(priKey)
  75. signature.update(data)
  76. return Base64.getEncoder().encodeToString(signature.sign())
  77. } // Fun sign()
  78. /**
  79. * 校验数字签名
  80. *
  81. * @param data 加密数据
  82. * @param publicKey 公钥
  83. * @param sign 数字签名
  84. * @return 校验成功返回true 失败返回false
  85. * @throws IOException
  86. * @throws NoSuchAlgorithmException
  87. * @throws InvalidKeySpecException
  88. * @throws InvalidKeyException
  89. * @throws SignatureException
  90. */
  91. @Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, InvalidKeyException::class, SignatureException::class)
  92. fun verify(data: ByteArray, publicKey: String, sign: String): Boolean {
  93. // 解密由base64编码的公钥
  94. val keyBytes = Base64.getDecoder().decode(publicKey)
  95. // 构造X509EncodedKeySpec对象
  96. val keySpec = X509EncodedKeySpec(keyBytes!!)
  97. // KEY_ALGORITHM 指定的加密算法
  98. val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
  99. // 取公钥匙对象
  100. val pubKey = keyFactory.generatePublic(keySpec)
  101. val signature = Signature.getInstance(SIGNATURE_ALGORITHM)
  102. signature.initVerify(pubKey)
  103. signature.update(data)
  104. // 验证签名是否正常
  105. return signature.verify(Base64.getDecoder().decode(sign))
  106. } // Fun verify()
  107. /**
  108. * 解密<br></br>
  109. * 用私钥解密
  110. *
  111. * @param data
  112. * @param key
  113. * @return
  114. * @throws IOException
  115. * @throws NoSuchAlgorithmException
  116. * @throws InvalidKeySpecException
  117. * @throws NoSuchPaddingException
  118. * @throws InvalidKeyException
  119. * @throws BadPaddingException
  120. * @throws IllegalBlockSizeException
  121. */
  122. @Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, NoSuchPaddingException::class, InvalidKeyException::class, IllegalBlockSizeException::class, BadPaddingException::class)
  123. fun decryptByPrivateKey(data: ByteArray, key: String): ByteArray {
  124. // 对密钥解密
  125. val keyBytes = Base64.getDecoder().decode(key)
  126. // 取得私钥
  127. val pkcs8KeySpec = PKCS8EncodedKeySpec(keyBytes!!)
  128. val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
  129. val privateKey = keyFactory.generatePrivate(pkcs8KeySpec)
  130. // 对数据解密
  131. val cipher = Cipher.getInstance(keyFactory.algorithm)
  132. cipher.init(Cipher.DECRYPT_MODE, privateKey)
  133. return cipher.doFinal(data)
  134. } // Fun decryptByPrivateKey()
  135. /**
  136. * 解密<br></br>
  137. * 用公钥解密
  138. *
  139. * @param data
  140. * @param key
  141. * @return
  142. * @throws IOException
  143. * @throws NoSuchAlgorithmException
  144. * @throws InvalidKeySpecException
  145. * @throws NoSuchPaddingException
  146. * @throws InvalidKeyException
  147. * @throws BadPaddingException
  148. * @throws IllegalBlockSizeException
  149. */
  150. @Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, NoSuchPaddingException::class, InvalidKeyException::class, IllegalBlockSizeException::class, BadPaddingException::class)
  151. fun decryptByPublicKey(data: ByteArray, key: String): ByteArray {
  152. // 对密钥解密
  153. val keyBytes = Base64.getDecoder().decode(key)
  154. // 取得公钥
  155. val x509KeySpec = X509EncodedKeySpec(keyBytes!!)
  156. val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
  157. val publicKey = keyFactory.generatePublic(x509KeySpec)
  158. // 对数据解密
  159. val cipher = Cipher.getInstance(keyFactory.algorithm)
  160. cipher.init(Cipher.DECRYPT_MODE, publicKey)
  161. return cipher.doFinal(data)
  162. } // Fun decryptByPublicKey()
  163. /**
  164. * 用公钥加密
  165. *
  166. * @param data
  167. * @param key
  168. * @return
  169. * @throws IOException
  170. * @throws NoSuchAlgorithmException
  171. * @throws InvalidKeySpecException
  172. * @throws NoSuchPaddingException
  173. * @throws InvalidKeyException
  174. * @throws BadPaddingException
  175. * @throws IllegalBlockSizeException
  176. */
  177. @Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, NoSuchPaddingException::class, InvalidKeyException::class, IllegalBlockSizeException::class, BadPaddingException::class)
  178. fun encryptByPublicKey(data: ByteArray, key: String): ByteArray {
  179. // 对公钥解密
  180. val keyBytes = Base64.getDecoder().decode(key)
  181. // 取得公钥
  182. val x509KeySpec = X509EncodedKeySpec(keyBytes!!)
  183. val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
  184. val publicKey = keyFactory.generatePublic(x509KeySpec)
  185. // 对数据加密
  186. val cipher = Cipher.getInstance(keyFactory.algorithm)
  187. cipher.init(Cipher.ENCRYPT_MODE, publicKey)
  188. return cipher.doFinal(data)
  189. } // Fun encryptByPublicKey()
  190. /**
  191. * 用私钥加密
  192. *
  193. * @param data
  194. * @param key
  195. * @return
  196. * @throws IOException
  197. * @throws NoSuchAlgorithmException
  198. * @throws InvalidKeySpecException
  199. * @throws NoSuchPaddingException
  200. * @throws InvalidKeyException
  201. * @throws BadPaddingException
  202. * @throws IllegalBlockSizeException
  203. */
  204. @Throws(IOException::class, NoSuchAlgorithmException::class, InvalidKeySpecException::class, NoSuchPaddingException::class, InvalidKeyException::class, IllegalBlockSizeException::class, BadPaddingException::class)
  205. fun encryptByPrivateKey(data: ByteArray, key: String): ByteArray {
  206. // 对密钥解密
  207. val keyBytes = Base64.getDecoder().decode(key)
  208. // 取得私钥
  209. val pkcs8KeySpec = PKCS8EncodedKeySpec(keyBytes!!)
  210. val keyFactory = KeyFactory.getInstance(KEY_ALGORITHM)
  211. val privateKey = keyFactory.generatePrivate(pkcs8KeySpec)
  212. // 对数据加密
  213. val cipher = Cipher.getInstance(keyFactory.algorithm)
  214. cipher.init(Cipher.ENCRYPT_MODE, privateKey)
  215. return cipher.doFinal(data)
  216. } // Fun encryptByPrivateKey()
  217. /**
  218. * 取得私钥
  219. *
  220. * @param keyMap
  221. * @return
  222. */
  223. fun getPrivateKey(keyMap: Map<String, Any>): String? {
  224. val key = keyMap[PRIVATE_KEY] as Key
  225. return Base64.getEncoder().encodeToString(key.encoded)
  226. } // Fun getPrivateKey()
  227. /**
  228. * 取得公钥
  229. *
  230. * @param keyMap
  231. * @return
  232. */
  233. fun getPublicKey(keyMap: Map<String, Any>): String? {
  234. val key = keyMap[PUBLIC_KEY] as Key
  235. return Base64.getEncoder().encodeToString(key.encoded)
  236. } // Fun getPublicKey()
  237. /**
  238. * 初始化密钥
  239. *
  240. * @return
  241. * @throws NoSuchAlgorithmException
  242. */
  243. @Throws(NoSuchAlgorithmException::class)
  244. fun initKey(): Map<String, Any> {
  245. val keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM)
  246. keyPairGen.initialize(1024)
  247. val keyPair = keyPairGen.generateKeyPair()
  248. // 公钥
  249. val publicKey = keyPair.public as RSAPublicKey
  250. // 私钥
  251. val privateKey = keyPair.private as RSAPrivateKey
  252. val keyMap = HashMap<String, Any>(2)
  253. keyMap[PUBLIC_KEY] = publicKey
  254. keyMap[PRIVATE_KEY] = privateKey
  255. return keyMap
  256. } // Fun initKey()
  257. }
  258. } // Class SRSACoderUtil