java aes128加密解密_java AES 128 位加密解密算法

最近在做app后台的服务器,使用到AES加密解密算法,无奈网上的都不符合要求,于是自己借鉴着写了一个AES加密解密工具。

密钥长度问题

默认 Java 中仅支持 128 位密钥,当使用 256 位密钥的时候,会报告密钥长度错误

Invalid AES key length

你需要下载一个支持更长密钥的包。这个包叫做 JavaCryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6,可以从这里下载,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html

下载之后,解压后,可以看到其中包含两个包:

local_policy.jar

US_export_policy.jar

看一下你的 JRE 环境,将 JRE 环境中 lib\lib\security 中的同名包替换掉。

废话不多说,上代码

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**

* 算法工具

* @author Babylon 214750838@qq.com

* @date 2014-8-15 上午8:41:49

*/

public class AlgorithmUtil {

public final static String ENCODING = "UTF-8";

/**将二进制转换成16进制

* @param buf

* @return

*/

public static String parseByte2HexStr(byte buf[]) {

StringBuffer sb = new StringBuffer();

for (int i = 0; i < buf.length; i++) {

String hex = Integer.toHexString(buf[i] & 0xFF);

if (hex.length() == 1) {

hex = '0' + hex;

}

sb.append(hex.toUpperCase());

}

return sb.toString();

}

/**将16进制转换为二进制

* @param hexStr

* @return

*/

public static byte[] parseHexStr2Byte(String hexStr) {

if (hexStr.length() < 1)

return null;

byte[] result = new byte[hexStr.length()/2];

for (int i = 0;i< hexStr.length()/2; i++) {

int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);

int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);

result[i] = (byte) (high * 16 + low);

}

return result;

}

/**

* 生成密钥

* 自动生成base64 编码后的AES128位密钥

* @throws NoSuchAlgorithmException

* @throws UnsupportedEncodingException

*/

public static String getAESKey() throws Exception {

KeyGenerator kg = KeyGenerator.getInstance("AES");

kg.init(128);//要生成多少位,只需要修改这里即可128, 192或256

SecretKey sk = kg.generateKey();

byte[] b = sk.getEncoded();

return parseByte2HexStr(b);

}

/**

* AES 加密

* @param base64Key base64编码后的 AES key

* @param text 待加密的字符串

* @return 加密后的byte[] 数组

* @throws Exception

*/

public static byte[] getAESEncode(String base64Key, String text) throws Exception{

byte[] key = parseHexStr2Byte(base64Key);

SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);

byte[] bjiamihou = cipher.doFinal(text.getBytes(ENCODING));

return bjiamihou;

}

/**

* AES解密

* @param base64Key base64编码后的 AES key

* @param text 待解密的字符串

* @return 解密后的byte[] 数组

* @throws Exception

*/

public static byte[] getAESDecode(String base64Key, byte[] text) throws Exception{

byte[] key = parseHexStr2Byte(base64Key);

SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODE, sKeySpec);

byte[] bjiemihou = cipher.doFinal(text);

return bjiemihou;

}

}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

90

91

92

93

94

95

96

97

98

99

100

1011

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

90

91

92

93

94

95

96

97

98

99

100

101

import com.util.AlgorithmUtil;

/**

* 算法测试

*

* @author babylon

* 2016年5月16日

*/

public class AlgorithmTest {

public static void main(String[] args) {

try {

String hexKey = new AlgorithmUtil().getAESKey();

System.out.println("16进制秘钥:"+hexKey);

byte[] encoded = AlgorithmUtil.getAESEncode(hexKey, "我要把你嘿嘿嘿");

// 注意,这里的encoded是不能强转成string类型字符串的

byte[] decoded = AlgorithmUtil.getAESDecode(hexKey, encoded);

System.out.println(new String(decoded, "UTF-8"));

} catch (Exception e) {

e.printStackTrace();

}

}

}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

261

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在PHP和Java之间进行AES加密和解密时出现乱码问题,可能是因为两个语言之间使用了不同的编码方式。为了解决这个问题,我们需要在两个语言之间选择一种通用的编码方式,以确保加密和解密时的数据一致。 一种通用的编码方式是Base64编码。在PHP中,可以使用base64_encode()函数将加密后的数据转换为Base64编码。在Java中,可以使用java.util.Base64类进行编码和解码。在进行解密操作之前,需要先将Base64编码的数据解码成原始二进制数据。 下面是一个PHP和Java之间进行AES加密和解密的示例代码: PHP代码: ``` $key = 'your_key'; $data = 'your_data'; // 加密 $encrypted = openssl_encrypt($data, 'AES-128-ECB', $key, OPENSSL_RAW_DATA); $base64_encrypted = base64_encode($encrypted); // 将加密后的数据传递给Java ``` Java代码: ``` import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; public class AESUtils { private static final String ALGO = "AES"; private static final String MODE = "ECB"; private static final String PADDING = "PKCS5Padding"; public static byte[] decrypt(byte[] key, byte[] encryptedData) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGO); Cipher cipher = Cipher.getInstance(ALGO + "/" + MODE + "/" + PADDING); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return cipher.doFinal(encryptedData); } public static void main(String[] args) throws Exception { String keyStr = "your_key"; String encryptedStr = "your_base64_encrypted_data"; // 解码Base64编码的数据 byte[] encrypted = Base64.getDecoder().decode(encryptedStr); byte[] key = keyStr.getBytes(); // 解密 byte[] decrypted = AESUtils.decrypt(key, encrypted); String data = new String(decrypted); System.out.println(data); } } ``` 在上面的示例中,PHP代码将数据加密并转换为Base64编码,然后将其传递给Java代码。Java代码解码Base64编码的数据,使用AES算法进行解密,并将解密后的数据转换为字符串输出。注意,在实际应用中,需要确保加密和解密使用相同的密钥、算法、模式和填充方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值