AES对称若何加密?

1年前 (2023-01-02)阅读2回复1
花花
花花
  • 管理员
  • 注册排名3
  • 经验值463260
  • 级别管理员
  • 主题92652
  • 回复0
楼主

  1 什么是AES

AES是一种对称的私钥加密手艺。它撑持128,192,256位加密。

2 AES和Java

从j2se1。4。2起头,集成了JCE包。

如今的java撑持128位key的加密。(下面的法式也是以128位为例讲解的)

3 若何利用JCE

Java代码

import java。

  security。*;

import javax。crypto。*;

import javax。crypto。spec。*;

import java。io。*;

* This program generates a AES key, retrieves its raw bytes, and

* then reinstantiates a AES key from the key bytes。

* The reinstantiated key is used to initialize a AES cipher for

* encryption and decryption。

public class AES {

* Turns array of bytes into string

* @param buf  Array of bytes to convert to hex string

* @return Generated hex string

public static String asHex (byte buf[]) {

StringBuffer strbuf = new StringBuffer(buf。

  length * 2);

int i;

for (i = 0; i   append(Long。toString((int) buf[i] 0xff, 16));

return strbuf。toString();

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

String message="This is just an example";

// Get the KeyGenerator

KeyGenerator kgen = KeyGenerator。

  getInstance("AES");

kgen。init(128); // 192 and 256 bits may not be available

// Generate the secret key specs。

SecretKey skey = kgen。

  generateKey();

byte[] raw = skey。getEncoded();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

// Instantiate the cipher

Cipher cipher = Cipher。

  getInstance("AES");

cipher。init(Cipher。ENCRYPT_MODE, skeySpec);

byte[] encrypted =

cipher。doFinal((args。length == 0 ?

"This is just an example" : args[0])。

  getBytes());

System。out。println("encrypted string: " + asHex(encrypted));

cipher。init(Cipher。DECRYPT_MODE, skeySpec);

byte[] original =

cipher。

  doFinal(encrypted);

String originalString = new String(original);

System。out。println("Original string: " +

originalString + " " + asHex(original));

import java。

  security。*;

import javax。crypto。*;

import javax。crypto。spec。*;

import java。io。*;

* This program generates a AES key, retrieves its raw bytes, and

* then reinstantiates a AES key from the key bytes。

* The reinstantiated key is used to initialize a AES cipher for

* encryption and decryption。

public class AES {

* Turns array of bytes into string

* @param bufArray of bytes to convert to hex string

* @returnGenerated hex string

public static String asHex (byte buf[]) {

StringBuffer strbuf = new StringBuffer(buf。

  length * 2);

int i;

for (i = 0; i   append(Long。toString((int) buf[i] 0xff, 16));

return strbuf。toString();

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

String message="This is just an example";

// Get the KeyGenerator

KeyGenerator kgen = KeyGenerator。

  getInstance("AES");

kgen。init(128); // 192 and 256 bits may not be available

// Generate the secret key specs。

SecretKey skey = kgen。

  generateKey();

byte[] raw = skey。getEncoded();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

// Instantiate the cipher

Cipher cipher = Cipher。

  getInstance("AES");

cipher。init(Cipher。ENCRYPT_MODE, skeySpec);

byte[] encrypted =

cipher。doFinal((args。length == 0 ?

"This is just an example" : args[0])。

  getBytes());

System。out。println("encrypted string: " + asHex(encrypted));

cipher。init(Cipher。DECRYPT_MODE, skeySpec);

byte[] original =

cipher。

  doFinal(encrypted);

String originalString = new String(original);

System。out。println("Original string: " +

originalString + " " + asHex(original));

0
回帖

AES对称若何加密? 相关回复(1)

雨梦潇湘微凉
雨梦潇湘微凉
沙发
AES对称加密安全可靠,保护数据隐私。
吐槽1天前 (05-27 10:50)回复00
取消
载入表情清单……
载入颜色清单……
插入网络图片

取消确定

图片上传中
编辑器信息
提示信息