Java内容重新整理删除过期的东西

This commit is contained in:
estom
2025-09-14 03:49:42 -04:00
parent 9b8524ff80
commit 885b795e45
413 changed files with 643 additions and 1340 deletions

View File

@@ -0,0 +1,54 @@
package cn.aofeng.demo.encrypt;
import static cn.aofeng.demo.util.LogUtil.log;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base64;
/**
* AES加密与解密。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class AES extends EncryptAndDecrypt {
public final String encryptType = "AES/CBC/PKCS5Padding";
public final String algorithmParam = "abcdefgh12345678";
public final String key = "abcdefgh_1234567";
public void execute(String data) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
SecretKey secretKey = createSecretKey("AES", key);
byte[] secretData = encrypt(encryptType, secretKey, data,
algorithmParam);
log("使用%s加密后的数据", encryptType);
log(Base64.encodeBase64String(secretData));
String srcStr = decrypt(encryptType, secretKey, secretData,
algorithmParam);
log("解密后的数据:\n%s", srcStr);
}
public static void main(String[] args) throws UnsupportedEncodingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
String data = "炎黄汉字english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-";
log("待加密的数据:\n%s", data);
AES aes = new AES();
aes.execute(data);
}
}

View File

@@ -0,0 +1,51 @@
package cn.aofeng.demo.encrypt;
import static cn.aofeng.demo.util.LogUtil.log;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base64;
/**
* Blowfish加密解密。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class Blowfish extends EncryptAndDecrypt {
public final String encryptType = "Blowfish";
public final String key = "abcdefgh_1234567";
public void execute(String data) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
SecretKey secretKey = createSecretKey(encryptType, key);
byte[] secretData = encrypt(encryptType, secretKey, data);
log("使用%s加密后的数据", encryptType);
log(Base64.encodeBase64String(secretData));
String srcStr = decrypt(encryptType, secretKey, secretData);
log("解密后的数据:\n%s", srcStr);
}
public static void main(String[] args) throws UnsupportedEncodingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
String data = "炎黄汉字english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-";
log("待加密的数据:\n%s", data);
Blowfish bf = new Blowfish();
bf.execute(data);
}
}

View File

@@ -0,0 +1,125 @@
package cn.aofeng.demo.encrypt;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* 加密与解密。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class EncryptAndDecrypt {
public final static String CHARSET = "utf8";
/**
* 创建安全密钥。
*
* @param encryptType
* 加密方式AESBlowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a>
* @param keyStr
* 密钥明文
* @return 安全密钥
* @throws UnsupportedEncodingException
* 不支持指定的字符集编码
*/
public SecretKey createSecretKey(String encryptType, String keyStr)
throws UnsupportedEncodingException {
byte[] secretKeyData = keyStr.getBytes(CHARSET);
SecretKeySpec sks = new SecretKeySpec(secretKeyData, encryptType);
return sks;
}
/**
* 加密数据。
*
* @param encryptType 加密方式AESBlowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a>
* @param secretKey 密钥
* @param srcData 待加密的源数据
* @return 加密后的二进制数据(字节数组)
* @see #encrypt(String, SecretKey, String, String)
*/
public byte[] encrypt(String encryptType, SecretKey secretKey,
String srcData) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
return encrypt(encryptType, secretKey, srcData, null);
}
/**
* 加密数据。
*
* @param encryptType 加密类型AES/CBC/PKCS5Padding
* @param secretKey 密钥
* @param srcData 待加密的源数据
* @param algorithmParam 某些加密算法的附加参数
* @return 加密后的二进制数据(字节数组)
*/
public byte[] encrypt(String encryptType, SecretKey secretKey,
String srcData, String algorithmParam) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
Cipher encrpyt = Cipher.getInstance(encryptType);
if (null == algorithmParam) {
encrpyt.init(Cipher.ENCRYPT_MODE, secretKey);
} else {
IvParameterSpec iv = new IvParameterSpec(
algorithmParam.getBytes(CHARSET));
encrpyt.init(Cipher.ENCRYPT_MODE, secretKey, iv);
}
byte[] secretData = encrpyt.doFinal(srcData.getBytes(CHARSET));
return secretData;
}
/**
* 解密数据。
*
* @param decryptType 解密方式AESBlowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a>
* @param secretKey 密钥
* @param secretData 待解密的数据
* @return 解密后的数据
* @see #decrypt(String, SecretKey, byte[], String)
*/
public String decrypt(String decryptType, SecretKey secretKey,
byte[] secretData) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
return decrypt(decryptType, secretKey, secretData, null);
}
public String decrypt(String decryptType, SecretKey secretKey,
byte[] secretData, String algorithmParam)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidAlgorithmParameterException {
Cipher decrypt = Cipher.getInstance(decryptType);
if (null == algorithmParam) {
decrypt.init(Cipher.DECRYPT_MODE, secretKey);
} else {
IvParameterSpec iv = new IvParameterSpec(
algorithmParam.getBytes(CHARSET));
decrypt.init(Cipher.DECRYPT_MODE, secretKey, iv);
}
byte[] srcData = decrypt.doFinal(secretData);
String srcStr = new String(srcData, CHARSET);
return srcStr;
}
}

View File

@@ -0,0 +1,53 @@
package cn.aofeng.demo.encrypt;
import static cn.aofeng.demo.util.LogUtil.log;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base64;
/**
* HMAC-SHA1签名算法。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class HmacSha1 {
public final String encryptType = "HmacSHA1";
public final String key = "abcdefgh_1234567";
public void execute(String data) throws UnsupportedEncodingException,
NoSuchAlgorithmException, InvalidKeyException {
EncryptAndDecrypt ead = new EncryptAndDecrypt();
byte[] srcData = data.getBytes(EncryptAndDecrypt.CHARSET);
SecretKey secretKey = ead.createSecretKey(encryptType, key); // 生成密钥对象
Mac mac = Mac.getInstance(encryptType);
mac.init(secretKey);
byte[] result = mac.doFinal(srcData);
log("使用%s签名后的数据", encryptType);
log(Base64.encodeBase64String(result));
}
public static void main(String[] args) throws InvalidKeyException,
UnsupportedEncodingException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
String data = "炎黄汉字english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-";
log("待签名的数据:\n%s", data);
HmacSha1 hs = new HmacSha1();
hs.execute(data);
}
}

View File

@@ -0,0 +1,70 @@
package cn.aofeng.demo.encrypt;
import static cn.aofeng.demo.util.LogUtil.log;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
/**
* 加密解密性能比较。
*
* @author <a href="mailto:nieyong@ucweb.com">聂勇</a>
*/
public class PerformanceCompare extends EncryptAndDecrypt {
public void blowfishPerformence(String data)
throws UnsupportedEncodingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidAlgorithmParameterException {
Blowfish bf = new Blowfish();
SecretKey secretKey = createSecretKey(bf.encryptType, bf.key);
long startTime = System.currentTimeMillis();
for (int j = 0; j < 100000; j++) {
bf.encrypt(bf.encryptType, secretKey, data+j);
}
long endTime = System.currentTimeMillis();
long usedTime = endTime - startTime;
log("使用%s进行%d次加密消耗时间%d毫秒", bf.encryptType, 100000, usedTime);
}
public void aesPerformence(String data)
throws UnsupportedEncodingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidAlgorithmParameterException {
AES aes = new AES();
SecretKey secretKey = createSecretKey("AES", aes.key);
long startTime = System.currentTimeMillis();
for (int j = 0; j < 100000; j++) {
aes.encrypt(aes.encryptType, secretKey, data+j,
aes.algorithmParam);
}
long endTime = System.currentTimeMillis();
long usedTime = endTime - startTime;
log("使用%s进行%d次加密消耗时间%d毫秒", aes.encryptType, 100000, usedTime);
}
public static void main(String[] args) throws InvalidKeyException,
UnsupportedEncodingException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
String data = "炎黄汉字english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-";
log("待加密的数据:\n%s", data);
PerformanceCompare pc = new PerformanceCompare();
// AES
pc.aesPerformence(data);
// Blowfish
pc.blowfishPerformence(data);
}
}