/* * ******************************************************************************************************************** * * iFHS7. * ;BBMBMBMc rZMBMBR BMB * MBEr:;PBM, 7MBMMEOBB: BBB RBW * XK: BO SB. :SZ MBM. c;; ir BBM :FFr :SSF: ;xBMB:r iuGXv. i:. iF2; * DBBM0r. :D S7 ;XMBMB GMBMu. MBM: BMB MBMBBBMBMS WMBMBMBBK MBMBMBM BMBRBMBW .MBMBMBMBB * :JMRMMD .. , 1MMRM1; ;MBMBBR: MBM ;MB: BMB: MBM. RMBr sBMH BM0 UMB, BMB. KMBv * ;. XOW B1; :uM: 1RE, i .2BMBs rMB. MBO MBO JMB; MBB MBM BBS 7MBMBOBM: MBW :BMc * OBRJ.SEE MRDOWOR, 3DE:7OBM . ;BMB RMR7BM BMB MBB. BMB ,BMR .BBZ MMB rMB, BMM rMB7 * :FBRO0D0 RKXSXPR. JOKOOMPi BMBSSWBMB; BMBB: MBMB0ZMBMS .BMBOXRBMB MBMDE RBM2;SMBM; MBB xBM2 * iZGE O0SHSPO. uGZ7. sBMBMBDL :BMO OZu:BMBK, rRBMB0; ,EBMB xBMBr:ER. RDU :OO; * ,BZ, 1D0 RPSFHXR. xWZ .SMr . .BBB * :0BMRDG RESSSKR. 2WOMBW; BMBMR * i0BM: SWKHKGO MBDv * .UB OOGDM. MK, Copyright (c) 2015-2019. 斯伯坦机器人 * , XMW .. * r All rights reserved. * * ******************************************************************************************************************** */ package com.sybotan.service.utils.crypt import javax.crypto.Cipher import javax.crypto.KeyGenerator import javax.crypto.spec.SecretKeySpec import java.io.IOException import java.security.Key import java.security.NoSuchAlgorithmException import java.security.SecureRandom import java.util.* /** * AES安全编码组件 * * @author Andy */ abstract class SAESCoderUtil : SCoderUtil() { companion object { const val ALGORITHM = "AES" /** * 转换密钥 * * @param key * @return * @throws Exception */ @Throws(Exception::class) private fun toKey(key: ByteArray): Key { return SecretKeySpec(key, ALGORITHM) } /** * 解密 * * @param data * @param key * @return * @throws Exception */ @Throws(Exception::class) fun decrypt(data: ByteArray, key: String): ByteArray { val k = toKey(Base64.getDecoder().decode(key)) val cipher = Cipher.getInstance(ALGORITHM) cipher.init(Cipher.DECRYPT_MODE, k) return cipher.doFinal(data) } // Fun decrypt() /** * 加密 * * @param data * @param key * @return * @throws Exception */ @Throws(Exception::class) fun encrypt(data: ByteArray, key: String): ByteArray { val k = toKey(Base64.getDecoder().decode(key)) val cipher = Cipher.getInstance(ALGORITHM) cipher.init(Cipher.ENCRYPT_MODE, k) return cipher.doFinal(data) } // Fun encrypt() /** * 生成密钥 * * @param seed * @return * @throws IOException * @throws NoSuchAlgorithmException */ @Throws(IOException::class, NoSuchAlgorithmException::class) @JvmOverloads fun initKey(seed: String? = null): String? { val secureRandom = if (seed != null) { SecureRandom(Base64.getDecoder().decode(seed)) } else { SecureRandom() } val kg = KeyGenerator.getInstance(ALGORITHM) kg.init(secureRandom) val secretKey = kg.generateKey() return Base64.getEncoder().encodeToString(secretKey.encoded) } // Fun initKey() } // companion object } // Class SAESCoderUtil