What is the decryption demo method for java version
This article mainly explains the "java version of the decryption demo method is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "java version of the decryption demo method is what" it!
1.
Join pom.xml
Org.bouncycastle
Bcprov-jdk15on
1.59
two。
Import com.alibaba.fastjson.JSONObject
Import com.sun.org.apache.xerces.internal.impl.dv.util.Base64
Import java.security.AlgorithmParameters
Import java.security.Security
Import java.util.Arrays
Import javax.crypto.Cipher
Import javax.crypto.spec.IvParameterSpec
Import javax.crypto.spec.SecretKeySpec
Import org.bouncycastle.jce.provider.BouncyCastleProvider
/ * *
*
* decryption method
, /
Public class SecretUtils {
Public static JSONObject unEncrypted (String encryptedData, String sessionKey, String iv) {
/ / encrypted data
Byte [] dataByte = Base64.decode (encryptedData)
/ / encryption key
Byte [] keyByte = Base64.decode (sessionKey)
/ / offset
Byte [] ivByte = Base64.decode (iv)
Try {
/ / if the key is less than 16 bits, then make it up. The content in this if is very important.
Int base = 16
If (keyByte.length% base! = 0) {
Int groups = keyByte.length / base + (keyByte.length% base! = 0? 1: 0)
Byte [] temp = new byte [groups * base]
Arrays.fill (temp, (byte) 0)
System.arraycopy (keyByte, 0, temp, 0, keyByte.length)
KeyByte = temp
}
/ / initialize
Security.addProvider (new BouncyCastleProvider ())
Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS7Padding", "BC")
SecretKeySpec spec = new SecretKeySpec (keyByte, "AES")
AlgorithmParameters parameters = AlgorithmParameters.getInstance ("AES")
Parameters.init (new IvParameterSpec (ivByte))
Cipher.init (Cipher.DECRYPT_MODE, spec, parameters); / / initialize
Byte [] resultByte = cipher.doFinal (dataByte)
If (null! = resultByte & & resultByte.length > 0) {
String result = new String (resultByte, "UTF-8")
Return JSONObject.parseObject (result)
}
} catch (Exception e) {
E.printStackTrace ()
}
Return null
}
}
Thank you for your reading, the above is the "java version of the decryption demo method is what" the content, after the study of this article, I believe you on the java version of the decryption demo method is what this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!