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

Aes encryption

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

Share

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

Package com.zyhao.openec.util

Import java.io.File

Import java.io.FileInputStream

Import java.io.FileOutputStream

Import java.io.IOException

Import java.security.InvalidKeyException

Import java.security.NoSuchAlgorithmException

Import java.security.SecureRandom

Import javax.crypto.BadPaddingException

Import javax.crypto.Cipher

Import javax.crypto.IllegalBlockSizeException

Import javax.crypto.KeyGenerator

Import javax.crypto.NoSuchPaddingException

Import javax.crypto.SecretKey

Import javax.crypto.spec.SecretKeySpec

Public class MainTest {

/ / AES: advanced encryption standard, new generation standard, faster encryption speed and higher security (not to mention priority)

Public static void main (String [] args) throws Exception {

String aesKey = getKey ()

System.out.println (aesKey)

/ encryption

String oriString = "I am the encrypted text: hello world | hi!"

Byte [] oriText = oriString.getBytes ()

Byte [] be = getAESEncode (hexStringToBytes (aesKey), oriText)

String encodeString = byteToHexString (be)

System.out.println (encodeString)

/ / decrypt

Byte [] encodeByte=hexStringToBytes (encodeString)

Byte [] bd = getAESDecode (hexStringToBytes (aesKey), encodeByte)

System.out.println (new String (bd))

}

/ * *

* AES generates key

* automatically generate AES128-bit keys

* pass the path to save the key file

* filePath indicates the file storage path plus the file name; for example, d:\ aes.txt

* @ throws NoSuchAlgorithmException

* @ throws IOException

, /

Public static byte [] getAutoCreateAESKey () throws NoSuchAlgorithmException, IOException {

KeyGenerator kg = KeyGenerator.getInstance ("AES")

Kg.init (128); / / how many bits to generate, you just need to modify it here to 128,192 or 256.

SecretKey sk = kg.generateKey ()

Byte [] b = sk.getEncoded ()

Return b

}

/ * *

* encryption

* encrypt using a symmetric key

* keyFilePath key storage path

* Byte array to be encrypted by text

* return a byte array after encryption

* @ throws IOException

* @ throws NoSuchPaddingException

* @ throws NoSuchAlgorithmException

* @ throws InvalidKeyException

* @ throws BadPaddingException

* @ throws IllegalBlockSizeException

, /

Public static byte [] getAESEncode (byte [] key,byte [] text) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

SecretKeySpec sKeySpec = new SecretKeySpec (key, "AES")

Cipher cipher = Cipher.getInstance ("AES")

Cipher.init (Cipher.ENCRYPT_MODE, sKeySpec)

Byte [] bjiamihou = cipher.doFinal (text)

Return bjiamihou

}

/ * *

* decryption

* decrypt using a symmetric key

* keyFilePath key storage path

* an array of bytes to be decrypted by text

* return a byte array after decryption

* @ throws IOException

* @ throws NoSuchPaddingException

* @ throws NoSuchAlgorithmException

* @ throws InvalidKeyException

* @ throws BadPaddingException

* @ throws IllegalBlockSizeException

, /

Public static byte [] getAESDecode (byte [] key,byte [] text) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

SecretKeySpec sKeySpec = new SecretKeySpec (key, "AES")

Cipher cipher = Cipher.getInstance ("AES")

Cipher.init (Cipher.DECRYPT_MODE, sKeySpec)

Byte [] bjiemihou = cipher.doFinal (text)

Return bjiemihou

}

/ * *

* randomly generate secret key

, /

Public static String getKey () {

Try {

KeyGenerator kg = KeyGenerator.getInstance ("AES")

Kg.init (128)

/ / how many bits to generate, you only need to modify here to 128192 or 256

SecretKey sk = kg.generateKey ()

Byte [] b = sk.getEncoded ()

String s = byteToHexString (b)

System.out.println (s)

System.out.println ("hexadecimal key length" + s.length ())

System.out.println ("length of binary key" + s.length () * 4)

Return s

} catch (NoSuchAlgorithmException e) {

E.printStackTrace ()

System.out.println ("there is no such algorithm.")

}

Return null

}

/ / * *

/ / * generate the secret key using the specified string

/ / /

/ / public static void getKeyByPass () {

/ generate secret key

/ / String password= "testkey"

/ / try {

/ / KeyGenerator kg = KeyGenerator.getInstance ("AES")

/ kg.init (128); / / to generate how many bits, you only need to modify here to 128,192 or 256

/ SecureRandom is to generate a safe random number sequence, password.getBytes () is the seed, as long as the seed is the same, the sequence is the same, so the generated key is the same.

/ / kg.init (128, new SecureRandom (password.getBytes ()

/ / SecretKey sk = kg.generateKey ()

/ / byte [] b = sk.getEncoded ()

/ / String s = byteToHexString (b)

/ / System.out.println (s)

/ / System.out.println ("hexadecimal key length is" + s.length ())

/ / System.out.println ("length of binary key" + s.length () * 4)

/ /} catch (NoSuchAlgorithmException e) {

/ / e.printStackTrace ()

/ / System.out.println ("there is no such algorithm.")

/ /}

/ /}

/ * *

* convert byte array to hexadecimal string

* @ param bytes

* @ return

, /

Public static String byteToHexString (byte [] bytes) {

StringBuffer sb = new StringBuffer ()

For (int I = 0; I

< bytes.length; i++) { String strHex=Integer.toHexString(bytes[i]); if(strHex.length() >

3) {

Sb.append (strHex.substring (6))

} else {

If (strHex.length () < 2) {

Sb.append ("0" + strHex)

} else {

Sb.append (strHex)

}

}

}

Return sb.toString ()

}

/ * *

* Convert hex string to byte []

* @ param hexString the hex string

* @ return byte []

, /

Public static byte [] hexStringToBytes (String hexString) {

If (hexString = = null | | hexString.equals ("")) {

Return null

}

HexString = hexString.toUpperCase ()

Int length = hexString.length () / 2

Char [] hexChars = hexString.toCharArray ()

Byte [] d = new byte [length]

For (int I = 0; I < length; iTunes +) {

Int pos = I * 2

D [I] = (byte) (charToByte (hexChars [pos]))

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