我是靠谱客的博主 高贵蜗牛,这篇文章主要介绍Java实现Base64、DES、AES、RSA加解密以及加密方式之间的区别,现在分享给大家,希望可以做个参考。

Base64 加密, 这个其实不可以算作加密方法,它可以看作是一种编码方式,它的用途只是二进制数字和字符串进行相互转化。

Base64是网络上最常见的用于传输 8Bit字节码的编码方式之一,是一种基于64个可打印字符来表示二进制数据的方法。Base64一般用于在 HTTP协议下传输二进制数据,由于 HTTP协议是文本协议,所以在 HTTP协议下传输二进制数据需要将二进制数据转换为字符数据。然而直接转换是不行的。因为网络传输只能传输可打印字符。

MD5加密 散列函数计算,一种不安全的加密算法,通常用于账号密码的加密,但是一般都需要加盐处理,单纯的MD5的加密后的字符串可以被破解,或者通过字典查出。

java 实现base64加解密:

复制代码
1
2
3
4
5
6
7
8
//Base64 加解密 //编码加密 String encodeStr = Base64.getEncoder().encodeToString(str.getBytes("UTF-8")); System.out.println("Base64加密后的字符串为:" + encodeStr); //解码解密 String decoderStr = new String(Base64.getDecoder().decode(encodeStr), StandardCharsets.UTF_8); // // 推荐使用StandardCharsets类指定 System.out.println("Base64解密后的字符串为" + decoderStr);

对称加密算法:DES、AES

DES算法加密的安全等级相对较低,通过同一个密钥进行加密解密。

Java实现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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/** * 偏移变量,固定占8位字节 */ private final static String IV_PARAMETER = "12345678"; /** * 密钥算法 */ private static final String ALGORITHM = "DES"; /** * 加密/解密算法-工作模式-填充模式 */ private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding"; /** * 默认编码 */ private static final String CHARSET = "utf-8"; /** * 生成key * * @param password * @return * @throws Exception */ private static Key generateKey(String password) throws Exception { DESKeySpec dks = new DESKeySpec(password.getBytes(CHARSET)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); return keyFactory.generateSecret(dks); } /** * DES加密字符串 * * @param password 加密密码,长度不能够小于8位 * @param data 待加密字符串 * @return 加密后内容 */ public static String encrypt(String password, String data) { if (password == null || password.length() < 8) { throw new RuntimeException("加密失败,key不能小于8位"); } if (data == null) { return null; } try { Key secretKey = generateKey(password); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); byte[] bytes = cipher.doFinal(data.getBytes(CHARSET)); //JDK1.8及以上可直接使用Base64,JDK1.7及以下可以使用BASE64Encoder //Android平台可以使用android.util.Base64 return new String(Base64.getEncoder().encode(bytes)); } catch (Exception e) { e.printStackTrace(); return data; } } /** * DES解密字符串 * * @param password 解密密码,长度不能够小于8位 * @param data 待解密字符串 * @return 解密后内容 */ public static String decrypt(String password, String data) { if (password == null || password.length() < 8) { throw new RuntimeException("加密失败,key不能小于8位"); } if (data == null) { return null; } try { Key secretKey = generateKey(password); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); return new String(cipher.doFinal(Base64.getDecoder().decode(data.getBytes(CHARSET))), CHARSET); } catch (Exception e) { e.printStackTrace(); return data; } } public static void main(String[] args) throws UnsupportedEncodingException { String str = "hello Base64 啊啊ADCDEFG"; //DES加解密 String password = "abcDEF123";//默认的密钥,根据情况修改 String encodeStrDes = encrypt(password, str); System.out.println("DES 加密后的字符串为: " + encodeStrDes); String decodeStrDes = decrypt(password, encodeStrDes); System.out.println("DES 解密后的字符串为: " + decodeStrDes); }

AES加密是一种安全性质比较高的加密算法,可以用作敏感数据的加密。从下图可以看到,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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** * 默认编码 */ private static final String CHARSET = "utf-8"; /** * 加解密算法/工作模式/填充方式 */ private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; /** * 填充向量 */ private static final String FILL_VECTOR = "1234560405060708"; private static final String ENCODING = "GBK"; private static final String KEY_ALGORITHM = "AES"; /** * 加密字符串 * * @param content 字符串 * @param password 密钥KEY * @return * @throws Exception */ public static String encryptAES(String content, String password) { if (StringUtils.isAnyEmpty(content, password)) { return null; } byte[] raw = hex2byte(password); SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); Cipher cipher = null; try { cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(FILL_VECTOR.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] anslBytes = content.getBytes(ENCODING); byte[] encrypted = cipher.doFinal(anslBytes); return byte2hex(encrypted).toUpperCase(); } catch (Exception e) { } return null; } /** * 解密 * * @param content 解密前的字符串 * @param password 解密KEY * @return * @throws Exception * @author cdduqiang * @date 2014年4月3日 */ public static String decryptAES(String content, String password) { if (StringUtils.isAnyEmpty(content, password)) { return null; } try { byte[] raw = hex2byte(password); SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(FILL_VECTOR.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = hex2byte(content); byte[] original = cipher.doFinal(encrypted1); return new String(original, ENCODING); } catch (Exception e) { } return null; } public static byte[] hex2byte(String strhex) { if (strhex == null) { return null; } int l = strhex.length(); if (l % 2 == 1) { return null; } byte[] b = new byte[l / 2]; for (int i = 0; i != l / 2; i++) { b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16); } return b; } public static String byte2hex(byte[] b) { String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length() == 1) { hs = hs + "0" + stmp; } else { hs = hs + stmp; } } return hs.toUpperCase(); } public static void main(String[] args) throws UnsupportedEncodingException { String str = "hello Base64 啊啊ADCDEFG"; //AES加解密 //必须为16位 String key = "9230967890982316"; //生成加密密钥 String key2 = byte2hex(key.getBytes()); System.out.println(key2); String encryptStr = encryptAES(str, key2); System.out.println(encryptStr); System.out.println(decryptAES(encryptStr, key2)); }

 RSA加密。典型的不对称加密算法,存在公钥和私钥。因为存在大量的运算,所以加解密的耗时会比对称加密算法更长。

最后

以上就是高贵蜗牛最近收集整理的关于Java实现Base64、DES、AES、RSA加解密以及加密方式之间的区别的全部内容,更多相关Java实现Base64、DES、AES、RSA加解密以及加密方式之间内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部