Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the decryption demo method for java version

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report