好得很程序员自学网

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

java实现AES可逆加密算法

本文实例为大家分享了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 + "毫秒" );

  }

}

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

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

查看更多关于java实现AES可逆加密算法的详细内容...

  阅读:9次