Java原生方法实现 AES 算法示例

2025-05-29 0 21

本文实例讲述了java原生方法实现 aes 算法。分享给大家供大家参考,具体如下:

aes(advanced encryption standard)高级加密标准,在密码学中又称 rijndael 加密法,是美国联邦政府采用的一种区块加密标准 。 这个标准用来替代原先的 des ,已经被多方分析且广为全世界所使用 。 现已成为对称密钥加密中最流行的算法之一 。

?

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
/**

* aes 算法

* <p/>

* 算法采用加密模式:cbc;数据块:128;填充:pkcs5padding

* <p/>

* key 与向量字符串的长度为 16 位

*

* @author deniro li (lisq037@163.com)

* 2018/3/17

*/

public class aes {

/**

* 算法名称

*/

public static final string name = "aes";

/**

* 加密模式:cbc;数据块:128;填充:pkcs5padding

*/

public final string mode = "aes/cbc/pkcs5padding";

/**

* key 与 向量字符串的长度

*/

public static final int length = 16;

/**

* 加密用的 key

*/

private string key;

/**

* 向量,用于增加加密强度

*/

private string ivparameter;

/**

* @param key 加密用的 key

* @param ivparameter 偏移量

*/

public aes(string key, string ivparameter) {

if (key == null || key.length() != length) {

throw new aesexception("key 不存在,或者长度不为 " + length);

}

if (ivparameter == null || ivparameter.length() != length) {

throw new aesexception("ivparameter 不存在,或者长度不为 " + length);

}

this.key = key;

this.ivparameter = ivparameter;

}

/**

* 加密

*

* @param s 要加密的字符串

* @return 加密后的字符串

*/

public string encode(string s) {

string result;

try {

cipher cipher = cipher.getinstance(mode);

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

cipher.init(encrypt_mode, new secretkeyspec(key.getbytes(), name), iv);

byte[] bytes = cipher.dofinal(s.getbytes(encoding));

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

} catch (exception e) {

throw new aesexception("加密", e);

}

return result;

}

/**

* 解密

*

* @param s 待解密的字符串

* @return 解密后的字符串

*/

public string decode(string s) {

try {

secretkeyspec keyspec = new secretkeyspec(key.getbytes("ascii"), name);

cipher cipher = cipher.getinstance(mode);

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

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

return new string(cipher.dofinal(new base64decoder().decodebuffer(s)), encoding);

} catch (exception e) {

throw new aesexception("解密", e);

}

}

}

单元测试:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
public class aestest {

aes aes;

@before

public void init(){

aes=new aes("12345abcdef67890","1234567890abcdef");

}

@test

public void testencode() throws exception {

assert.assertequals("janei3lbvnlcaz2xddwhzw==", aes.encode("123456"));

}

@test

public void testdecode() throws exception {

assert.assertequals("123456", aes.decode("janei3lbvnlcaz2xddwhzw=="));

}

}

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含aes、des、rc4等):https://tool.zzvips.com/t/txtencode/

md5在线加密工具:https://tool.zzvips.com/t/md5/

unicode编码转换工具:https://tool.zzvips.com/t/unicode/

在线escape加密|UnEscape解密工具:https://tool.zzvips.com/t/escape/

在线sha1/sha224/sha256/sha384/sha512加密工具:https://tool.zzvips.com/t/sha/

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/deniro_li/article/details/79594025

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java原生方法实现 AES 算法示例 https://www.kuaiidc.com/109668.html

相关文章

发表评论
暂无评论