mirror of
https://github.com/Estom/notes.git
synced 2026-02-03 18:44:19 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
54
Java/JavaDemo/codedemo/encrypt/AES.java
Normal file
54
Java/JavaDemo/codedemo/encrypt/AES.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
51
Java/JavaDemo/codedemo/encrypt/Blowfish.java
Normal file
51
Java/JavaDemo/codedemo/encrypt/Blowfish.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
125
Java/JavaDemo/codedemo/encrypt/EncryptAndDecrypt.java
Normal file
125
Java/JavaDemo/codedemo/encrypt/EncryptAndDecrypt.java
Normal 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
|
||||
* 加密方式,如:AES,Blowfish。详情查看<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 加密方式,如:AES,Blowfish。详情查看<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 解密方式,如:AES,Blowfish。详情查看<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;
|
||||
}
|
||||
|
||||
}
|
||||
53
Java/JavaDemo/codedemo/encrypt/HmacSha1.java
Normal file
53
Java/JavaDemo/codedemo/encrypt/HmacSha1.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
70
Java/JavaDemo/codedemo/encrypt/PerformanceCompare.java
Normal file
70
Java/JavaDemo/codedemo/encrypt/PerformanceCompare.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user