好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

本文实例讲述了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测试数据)

  *     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.tuohang.net/t/txtencode/

md5在线加密工具: https://tool.tuohang.net/t/md5/

unicode编码转换工具 : https://tool.tuohang.net/t/unicode/

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

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

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

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

查看更多关于Java原生方法实现 AES 算法示例的详细内容...

  阅读:24次