-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
878 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
codes/javacore-advanced/src/main/java/io/github/dunwu/javacore/bean/Query.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.dunwu.javacore.bean; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class Query { | ||
|
||
private Long id; | ||
private String status; | ||
|
||
public Query(Long id, String status) { | ||
this.id = id; | ||
this.status = status; | ||
} | ||
|
||
} |
129 changes: 0 additions & 129 deletions
129
codes/javacore-advanced/src/main/java/io/github/dunwu/javacore/crypto/AESCoder.java
This file was deleted.
Oops, something went wrong.
172 changes: 172 additions & 0 deletions
172
codes/javacore-advanced/src/main/java/io/github/dunwu/javacore/crypto/AesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package io.github.dunwu.javacore.crypto; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.GeneralSecurityException; | ||
import java.security.Key; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
import java.util.Base64; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
/** | ||
* AES 对称加密算法。DES的替代方案。 | ||
* | ||
* @author Zhang Peng | ||
* @since 2016年7月14日 | ||
*/ | ||
public class AesUtil { | ||
|
||
public static final String AES = "AES"; | ||
public static final String DEFAULT_SEED = "%%%today is nice***"; | ||
public static final String DEFAULT_IV = "2098432527847288"; | ||
|
||
private byte[] keyBytes; | ||
private byte[] ivBytes; | ||
|
||
private AesAlgorithmEnum algorithm; | ||
|
||
public byte[] getKeyBytes() { | ||
return this.keyBytes; | ||
} | ||
|
||
public String getBase64Key() { | ||
return Base64.getEncoder().encodeToString(this.keyBytes); | ||
} | ||
|
||
public Key getKey() { | ||
return new SecretKeySpec(keyBytes, AES); | ||
} | ||
|
||
public byte[] encrypt(byte[] data) throws GeneralSecurityException { | ||
return encrypt(algorithm, data, keyBytes, this.ivBytes); | ||
} | ||
|
||
public byte[] decrypt(byte[] data) throws GeneralSecurityException { | ||
return decrypt(algorithm, data, keyBytes, this.ivBytes); | ||
} | ||
|
||
public static AesUtil newInstance(byte[] ivBytes, String seed) throws GeneralSecurityException { | ||
Key key = generateKey(seed.getBytes(StandardCharsets.UTF_8)); | ||
AesUtil util = new AesUtil(); | ||
util.algorithm = AesAlgorithmEnum.AES; | ||
util.keyBytes = key.getEncoded(); | ||
util.ivBytes = ivBytes; | ||
return util; | ||
} | ||
|
||
public static AesUtil newInstance(AesAlgorithmEnum algorithm, byte[] ivBytes, String seed) | ||
throws GeneralSecurityException { | ||
Key key = generateKey(seed.getBytes(StandardCharsets.UTF_8)); | ||
AesUtil util = new AesUtil(); | ||
util.algorithm = algorithm; | ||
util.keyBytes = key.getEncoded(); | ||
util.ivBytes = ivBytes; | ||
return util; | ||
} | ||
|
||
public static AesUtil newInstance(AesAlgorithmEnum algorithm, byte[] ivBytes, byte[] keyBytes) { | ||
AesUtil util = new AesUtil(); | ||
util.algorithm = algorithm; | ||
util.keyBytes = keyBytes; | ||
util.ivBytes = ivBytes; | ||
return util; | ||
} | ||
|
||
public static byte[] encrypt(AesAlgorithmEnum algorithm, byte[] data, byte[] keyBytes, byte[] ivBytes) | ||
throws GeneralSecurityException { | ||
Cipher cipher = Cipher.getInstance(algorithm.getCode()); | ||
Key key = new SecretKeySpec(keyBytes, AES); | ||
switch (algorithm) { | ||
case AES_CBC_PKCS5PADDING: | ||
case AES_CBC_NOPADDING: | ||
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(ivBytes)); | ||
break; | ||
case AES: | ||
case AES_ECB_PKCS5PADDING: | ||
default: | ||
cipher.init(Cipher.ENCRYPT_MODE, key); | ||
break; | ||
} | ||
return cipher.doFinal(data); | ||
} | ||
|
||
public static byte[] decrypt(AesAlgorithmEnum algorithm, byte[] data, byte[] keyBytes, byte[] ivBytes) | ||
throws GeneralSecurityException { | ||
Cipher cipher = Cipher.getInstance(algorithm.getCode()); | ||
Key key = new SecretKeySpec(keyBytes, AES); | ||
switch (algorithm) { | ||
case AES_CBC_PKCS5PADDING: | ||
case AES_CBC_NOPADDING: | ||
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivBytes)); | ||
break; | ||
case AES: | ||
case AES_ECB_PKCS5PADDING: | ||
default: | ||
cipher.init(Cipher.DECRYPT_MODE, key); | ||
break; | ||
} | ||
return cipher.doFinal(data); | ||
} | ||
|
||
public static Key generateKey(byte[] seed) throws NoSuchAlgorithmException { | ||
// 根据种子生成一个安全的随机数 | ||
SecureRandom secureRandom = new SecureRandom(seed); | ||
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES); | ||
keyGenerator.init(secureRandom); | ||
return keyGenerator.generateKey(); | ||
} | ||
|
||
public enum AesAlgorithmEnum { | ||
|
||
AES("AES"), | ||
|
||
AES_ECB_PKCS5PADDING("AES/ECB/PKCS5Padding"), | ||
|
||
AES_CBC_PKCS5PADDING("AES/CBC/PKCS5Padding"), | ||
|
||
AES_CBC_NOPADDING("AES/CBC/NoPadding"); | ||
|
||
private final String code; | ||
|
||
AesAlgorithmEnum(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public static AesAlgorithmEnum getEnumByCode(String algorithm) { | ||
if (StrUtil.isBlank(algorithm)) { | ||
return null; | ||
} | ||
for (AesAlgorithmEnum type : AesAlgorithmEnum.values()) { | ||
if (type.getCode().equals(algorithm)) { | ||
return type; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
AesUtil aes = AesUtil.newInstance(AesAlgorithmEnum.AES_ECB_PKCS5PADDING, DEFAULT_IV.getBytes(), DEFAULT_SEED); | ||
|
||
String msg = "Hello World!"; | ||
System.out.println("[AES加密、解密]"); | ||
System.out.println("message: " + msg); | ||
byte[] encoded = aes.encrypt(msg.getBytes(StandardCharsets.UTF_8)); | ||
String encodedBase64 = Base64.getUrlEncoder().encodeToString(encoded); | ||
System.out.println("encoded: " + encodedBase64); | ||
|
||
byte[] decodedBase64 = Base64.getUrlDecoder().decode(encodedBase64); | ||
byte[] decoded = aes.decrypt(decodedBase64); | ||
System.out.println("decoded: " + new String(decoded)); | ||
} | ||
|
||
} |
Oops, something went wrong.