java实现AES可逆加密算法

2025-05-29 0 12

本文实例为大家分享了java实现aes可逆加密算法的具体代码,供大家参考,具体内容如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89
package com.hdu.encode;

import javax.crypto.cipher;

import javax.crypto.spec.ivparameterspec;

import javax.crypto.spec.secretkeyspec;

import sun.misc.base64decoder;

import sun.misc.base64encoder;

/**

* aes 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行aes加密后,在进行base64编码转化;

*/

public class aesoperator {

/*

* 加密用的key 可以用26个字母和数字组成 此处使用aes-128-cbc加密模式,key需要为16位。

*/

// a0b891c2d563e4f7

private string skey = "abcdef0123456789";

private string ivparameter = "0123456789abcdef";

private static aesoperator instance = null;

private aesoperator() {

}

public static aesoperator getinstance() {

if (instance == null)

instance = new aesoperator();

return instance;

}

// 加密

public string encrypt(string ssrc){

string result = "";

try {

cipher cipher;

cipher = cipher.getinstance("aes/cbc/pkcs5padding");

byte[] raw = skey.getbytes();

secretkeyspec skeyspec = new secretkeyspec(raw, "aes");

ivparameterspec iv = new ivparameterspec(ivparameter.getbytes());// 使用cbc模式,需要一个向量iv,可增加加密算法的强度

cipher.init(cipher.encrypt_mode, skeyspec, iv);

byte[] encrypted = cipher.dofinal(ssrc.getbytes("utf-8"));

result = new base64encoder().encode(encrypted);

} catch (exception e) {

e.printstacktrace();

}

// 此处使用base64做转码。

return result;

}

// 解密

public string decrypt(string ssrc){

try {

byte[] raw = skey.getbytes("ascii");

secretkeyspec skeyspec = new secretkeyspec(raw, "aes");

cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");

ivparameterspec iv = new ivparameterspec(ivparameter.getbytes());

cipher.init(cipher.decrypt_mode, skeyspec, iv);

byte[] encrypted1 = new base64decoder().decodebuffer(ssrc);// 先用base64解密

byte[] original = cipher.dofinal(encrypted1);

string originalstring = new string(original, "utf-8");

return originalstring;

} catch (exception ex) {

ex.printstacktrace();

return null;

}

}

public static void main(string[] args){

// 需要加密的字串

string csrc = "测试";

system.out.println(csrc + " 长度为" + csrc.length());

// 加密

long lstart = system.currenttimemillis();

string enstring = aesoperator.getinstance().encrypt(csrc);

system.out.println("加密后的字串是:" + enstring + "长度为" + enstring.length());

long lusetime = system.currenttimemillis() - lstart;

system.out.println("加密耗时:" + lusetime + "毫秒");

// 解密

lstart = system.currenttimemillis();

string destring = aesoperator.getinstance().decrypt(enstring);

system.out.println("解密后的字串是:" + destring);

lusetime = system.currenttimemillis() - lstart;

system.out.println("解密耗时:" + lusetime + "毫秒");

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/u011768325/article/details/40391045

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java实现AES可逆加密算法 https://www.kuaiidc.com/109378.html

相关文章

发表评论
暂无评论