Skip to content

Commit 860f205

Browse files
committed
update codes and docs
1 parent de1dcde commit 860f205

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3330
-1170
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
### [Java 高级](docs/advanced)
4444

4545
- [Java 正则](docs/advanced/java-regex.md)
46-
- [Java 编码和加密](docs/advanced/java-crypto.md)
46+
- [Java 编码和加密](docs/advanced/java-crypto.md) - 关键词:`Base64``消息摘要``数字签名``对称加密``非对称加密``MD5``SHA``HMAC``AES``DES``DESede``RSA`
4747
- [Java 本地化](docs/advanced/java-locale.md)
4848
- [Java JDK8](docs/advanced/jdk8.md)
4949

@@ -61,15 +61,16 @@
6161

6262
![img](http://dunwu.test.upcdn.net/snap/20200221175827.png)
6363

64-
- [Java 并发简介](docs/concurrent/java-concurrent-introduction.md) - 关键词:`进程``线程`
65-
- [Java 线程基础](docs/concurrent/java-thread.md) - 关键词:`Thread``Runnable``Callable``Future`
64+
- [Java 并发简介](docs/concurrent/java-concurrent-introduction.md) - 关键词:`进程``线程``安全性``活跃性``性能``死锁``饥饿``上下文切换`
65+
- [Java 线程基础](docs/concurrent/java-thread.md) - 关键词:`Thread``Runnable``Callable``Future``wait``notify``notifyAll``join``sleep``yeild``线程状态``线程通信`
6666
- [Java 并发核心机制](docs/concurrent/java-concurrent-basic-mechanism.md) - 关键词:`synchronized``volatile``CAS``ThreadLocal`
6767
- [Java 并发锁](docs/concurrent/java-lock.md) - 关键词:`AQS``ReentrantLock``ReentrantReadWriteLock``Condition`
6868
- [Java 原子类](docs/concurrent/java-atomic-class.md) - 关键词:`CAS``Atomic`
6969
- [Java 并发容器](docs/concurrent/java-concurrent-container.md) - 关键词:`ConcurrentHashMap``CopyOnWriteArrayList`
7070
- [Java 线程池](docs/concurrent/java-thread-pool.md) - 关键词:`Executor``ExecutorService``ThreadPoolExecutor``Executors`
7171
- [Java 并发工具类](docs/concurrent/java-concurrent-tools.md) - 关键词:`CountDownLatch``CyclicBarrier``Semaphore`
72-
- [Java 内存模型](docs/concurrent/java-memory-model.md) - 关键词:`JMM``原子性``可见性``有序性``Happens-Before``内存屏障`
72+
- [Java 内存模型](docs/concurrent/java-memory-model.md) - 关键词:`JMM``volatile``synchronized``final``Happens-Before``内存屏障`
73+
- [Java Fork Join](docs/concurrent/java-fork-join.md) - 关键词:
7374

7475
### [Java IO](docs/io)
7576

@@ -118,9 +119,16 @@
118119
- [《Java 程序员面试宝典》](https://item.jd.com/11772823.html)
119120
- **教程、社区**
120121
- [Runoob Java 教程](https://www.runoob.com/java/java-tutorial.html)
121-
- [JavaGuide](https://github.com/Snailclimb/JavaGuide)
122-
- [Java](https://github.com/TheAlgorithms/Java)
123122
- [java-design-patterns](https://github.com/iluwatar/java-design-patterns)
123+
- [Java](https://github.com/TheAlgorithms/Java)
124+
- [Java 核心技术面试精讲](https://time.geekbang.org/column/intro/82)
125+
- [Java 性能调优实战](https://time.geekbang.org/column/intro/100028001)
126+
- [Java 业务开发常见错误 100 例](https://time.geekbang.org/column/intro/100047701)
127+
- [深入拆解 Java 虚拟机](https://time.geekbang.org/column/intro/100010301)
128+
- [Java 并发编程实战](https://time.geekbang.org/column/intro/100023901)
129+
- **面试**
130+
- [CS-Notes](https://github.com/CyC2018/CS-Notes)
131+
- [JavaGuide](https://github.com/Snailclimb/JavaGuide)
124132
- [advanced-java](https://github.com/doocs/advanced-java)
125133

126134
## 🚪 传送

assets/JavaCore.xmind

12.8 KB
Binary file not shown.

assets/并发/Java并发.xmind

-391 KB
Binary file not shown.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.github.dunwu.javacore.crypto;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.security.*;
5+
import java.util.Base64;
6+
import javax.crypto.*;
7+
import javax.crypto.spec.IvParameterSpec;
8+
9+
/**
10+
* AES安全编码:对称加密算法。DES的替代方案。
11+
*
12+
* @author Zhang Peng
13+
* @since 2016年7月14日
14+
*/
15+
public class AESCoder {
16+
17+
public static final String KEY_ALGORITHM_AES = "AES";
18+
19+
public static final String CIPHER_AES_DEFAULT = "AES";
20+
21+
public static final String CIPHER_AES_ECB_PKCS5PADDING = "AES/ECB/PKCS5Padding"; // 算法/模式/补码方式
22+
23+
public static final String CIPHER_AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5Padding";
24+
25+
public static final String CIPHER_AES_CBC_NOPADDING = "AES/CBC/NoPadding";
26+
27+
private static final String SEED = "%%%today is nice***"; // 用于生成随机数的种子
28+
29+
private Key key;
30+
31+
private Cipher cipher;
32+
33+
private String transformation;
34+
35+
public AESCoder() throws NoSuchAlgorithmException, NoSuchPaddingException {
36+
this.key = initKey();
37+
this.cipher = Cipher.getInstance(CIPHER_AES_DEFAULT);
38+
this.transformation = CIPHER_AES_DEFAULT;
39+
}
40+
41+
/**
42+
* 根据随机数种子生成一个密钥
43+
*
44+
* @return Key
45+
* @throws NoSuchAlgorithmException
46+
* @author Zhang Peng
47+
* @since 2016年7月14日
48+
*/
49+
private Key initKey() throws NoSuchAlgorithmException {
50+
// 根据种子生成一个安全的随机数
51+
SecureRandom secureRandom = null;
52+
secureRandom = new SecureRandom(SEED.getBytes());
53+
54+
KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM_AES);
55+
keyGen.init(secureRandom);
56+
return keyGen.generateKey();
57+
}
58+
59+
public AESCoder(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException {
60+
this.key = initKey();
61+
this.cipher = Cipher.getInstance(transformation);
62+
this.transformation = transformation;
63+
}
64+
65+
/**
66+
* 加密
67+
*
68+
* @param input 明文
69+
* @return byte[] 密文
70+
* @throws InvalidKeyException
71+
* @throws IllegalBlockSizeException
72+
* @throws BadPaddingException
73+
* @throws InvalidAlgorithmParameterException
74+
* @author Zhang Peng
75+
* @since 2016年7月20日
76+
*/
77+
public byte[] encrypt(byte[] input) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
78+
InvalidAlgorithmParameterException {
79+
if (transformation.equals(CIPHER_AES_CBC_PKCS5PADDING) || transformation.equals(CIPHER_AES_CBC_NOPADDING)) {
80+
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(getIV()));
81+
} else {
82+
cipher.init(Cipher.ENCRYPT_MODE, key);
83+
}
84+
return cipher.doFinal(input);
85+
}
86+
87+
/**
88+
* 解密
89+
*
90+
* @param input 密文
91+
* @return byte[] 明文
92+
* @throws InvalidKeyException
93+
* @throws IllegalBlockSizeException
94+
* @throws BadPaddingException
95+
* @throws InvalidAlgorithmParameterException
96+
* @author Zhang Peng
97+
* @since 2016年7月20日
98+
*/
99+
public byte[] decrypt(byte[] input) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
100+
InvalidAlgorithmParameterException {
101+
if (transformation.equals(CIPHER_AES_CBC_PKCS5PADDING) || transformation.equals(CIPHER_AES_CBC_NOPADDING)) {
102+
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(getIV()));
103+
} else {
104+
cipher.init(Cipher.DECRYPT_MODE, key);
105+
}
106+
return cipher.doFinal(input);
107+
}
108+
109+
private byte[] getIV() {
110+
String iv = "0123456789ABCDEF"; // IV length: must be 16 bytes long
111+
return iv.getBytes();
112+
}
113+
114+
public static void main(String[] args) throws Exception {
115+
AESCoder aes = new AESCoder(CIPHER_AES_CBC_PKCS5PADDING);
116+
117+
String msg = "Hello World!";
118+
System.out.println("[AES加密、解密]");
119+
System.out.println("message: " + msg);
120+
byte[] encoded = aes.encrypt(msg.getBytes(StandardCharsets.UTF_8));
121+
String encodedBase64 = Base64.getUrlEncoder().encodeToString(encoded);
122+
System.out.println("encoded: " + encodedBase64);
123+
124+
byte[] decodedBase64 = Base64.getDecoder().decode(encodedBase64);
125+
byte[] decoded = aes.decrypt(decodedBase64);
126+
System.out.println("decoded: " + new String(decoded));
127+
}
128+
129+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.dunwu.javacore.crypto;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Base64;
5+
6+
/**
7+
* Base64编码、解码范例
8+
*
9+
* @author Zhang Peng
10+
* @since 2016年7月21日
11+
*/
12+
public class Base64Demo {
13+
14+
public static void main(String[] args) {
15+
String url = "https://www.baidu.com";
16+
System.out.println("url:" + url);
17+
// 标准的 Base64 编码、解码
18+
byte[] encoded = Base64.getEncoder().encode(url.getBytes(StandardCharsets.UTF_8));
19+
byte[] decoded = Base64.getDecoder().decode(encoded);
20+
System.out.println("Url Safe Base64 encoded:" + new String(encoded));
21+
System.out.println("Url Safe Base64 decoded:" + new String(decoded));
22+
// URL 安全的 Base64 编码、解码
23+
byte[] encoded2 = Base64.getUrlEncoder().encode(url.getBytes(StandardCharsets.UTF_8));
24+
byte[] decoded2 = Base64.getUrlDecoder().decode(encoded2);
25+
System.out.println("Base64 encoded:" + new String(encoded2));
26+
System.out.println("Base64 decoded:" + new String(decoded2));
27+
}
28+
29+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package io.github.dunwu.javacore.crypto;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.security.*;
5+
import java.util.Base64;
6+
import javax.crypto.*;
7+
import javax.crypto.spec.IvParameterSpec;
8+
9+
/**
10+
* DES安全编码:是经典的对称加密算法。密钥仅56位,且迭代次数偏少。已被视为并不安全的加密算法。
11+
*
12+
* @author Zhang Peng
13+
* @since 2016年7月14日
14+
*/
15+
public class DESCoder {
16+
17+
public static final String KEY_ALGORITHM_DES = "DES";
18+
19+
public static final String CIPHER_DES_DEFAULT = "DES";
20+
21+
public static final String CIPHER_DES_ECB_PKCS5PADDING = "DES/ECB/PKCS5Padding"; // 算法/模式/补码方式
22+
23+
public static final String CIPHER_DES_CBC_PKCS5PADDING = "DES/CBC/PKCS5Padding";
24+
25+
public static final String CIPHER_DES_CBC_NOPADDING = "DES/CBC/NoPadding";
26+
27+
private static final String SEED = "%%%today is nice***"; // 用于生成随机数的种子
28+
29+
private Key key;
30+
31+
private Cipher cipher;
32+
33+
private String transformation;
34+
35+
public DESCoder() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException {
36+
this.key = initKey();
37+
this.cipher = Cipher.getInstance(CIPHER_DES_DEFAULT);
38+
this.transformation = CIPHER_DES_DEFAULT;
39+
}
40+
41+
/**
42+
* 根据随机数种子生成一个密钥
43+
*
44+
* @return Key
45+
* @throws NoSuchAlgorithmException
46+
* @throws NoSuchProviderException
47+
* @author Zhang Peng
48+
* @since 2016年7月14日
49+
*/
50+
private Key initKey() throws NoSuchAlgorithmException, NoSuchProviderException {
51+
// 根据种子生成一个安全的随机数
52+
SecureRandom secureRandom = null;
53+
secureRandom = new SecureRandom(SEED.getBytes());
54+
55+
KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM_DES);
56+
keyGen.init(secureRandom);
57+
return keyGen.generateKey();
58+
}
59+
60+
public DESCoder(String transformation)
61+
throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException {
62+
this.key = initKey();
63+
this.cipher = Cipher.getInstance(transformation);
64+
this.transformation = transformation;
65+
}
66+
67+
/**
68+
* 加密
69+
*
70+
* @param input 明文
71+
* @return byte[] 密文
72+
* @throws InvalidKeyException
73+
* @throws IllegalBlockSizeException
74+
* @throws BadPaddingException
75+
* @throws InvalidAlgorithmParameterException
76+
* @author Zhang Peng
77+
* @since 2016年7月20日
78+
*/
79+
public byte[] encrypt(byte[] input) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
80+
InvalidAlgorithmParameterException {
81+
if (transformation.equals(CIPHER_DES_CBC_PKCS5PADDING) || transformation.equals(CIPHER_DES_CBC_NOPADDING)) {
82+
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(getIV()));
83+
} else {
84+
cipher.init(Cipher.ENCRYPT_MODE, key);
85+
}
86+
return cipher.doFinal(input);
87+
}
88+
89+
/**
90+
* 解密
91+
*
92+
* @param input 密文
93+
* @return byte[] 明文
94+
* @throws InvalidKeyException
95+
* @throws IllegalBlockSizeException
96+
* @throws BadPaddingException
97+
* @throws InvalidAlgorithmParameterException
98+
* @author Zhang Peng
99+
* @since 2016年7月20日
100+
*/
101+
public byte[] decrypt(byte[] input) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
102+
InvalidAlgorithmParameterException {
103+
if (transformation.equals(CIPHER_DES_CBC_PKCS5PADDING) || transformation.equals(CIPHER_DES_CBC_NOPADDING)) {
104+
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(getIV()));
105+
} else {
106+
cipher.init(Cipher.DECRYPT_MODE, key);
107+
}
108+
return cipher.doFinal(input);
109+
}
110+
111+
private byte[] getIV() {
112+
String iv = "01234567"; // IV length: must be 8 bytes long
113+
return iv.getBytes();
114+
}
115+
116+
public static void main(String[] args) throws Exception {
117+
DESCoder aes = new DESCoder(CIPHER_DES_CBC_PKCS5PADDING);
118+
119+
String msg = "Hello World!";
120+
System.out.println("原文: " + msg);
121+
byte[] encoded = aes.encrypt(msg.getBytes(StandardCharsets.UTF_8));
122+
String encodedBase64 = Base64.getUrlEncoder().encodeToString(encoded);
123+
System.out.println("密文: " + encodedBase64);
124+
125+
byte[] decodedBase64 = Base64.getUrlDecoder().decode(encodedBase64);
126+
byte[] decoded = aes.decrypt(decodedBase64);
127+
System.out.println("明文: " + new String(decoded));
128+
}
129+
130+
}

0 commit comments

Comments
 (0)