Skip to content

Instantly share code, notes, and snippets.

@foundkey
Created April 10, 2019 08:04
Show Gist options
  • Save foundkey/5fbb3d2002f65211a64f69930ee5a364 to your computer and use it in GitHub Desktop.
Save foundkey/5fbb3d2002f65211a64f69930ee5a364 to your computer and use it in GitHub Desktop.
package com.foundkey.example;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class CipherUtils {
public static byte[] rsaPublicKey;
public static byte[] rsaPrivateKey;
static {
updateRASKey();
}
// 获取信息的MD5值
public static byte[] getMD2(byte[] content) {
byte[] hash = null;
try {
hash = MessageDigest.getInstance("MD2").digest(content);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hash;
}
// 获取信息的MD5值
public static byte[] getMD5(byte[] content) {
byte[] hash = null;
try {
hash = MessageDigest.getInstance("MD5").digest(content);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hash;
}
// 获取信息的SHA-1
public static byte[] getSHA1(byte[] content) {
byte[] hash = null;
try {
hash = MessageDigest.getInstance("SHA-1").digest(content);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hash;
}
// 获取信息的SHA-256
public static byte[] getSHA256(byte[] content) {
byte[] hash = null;
try {
hash = MessageDigest.getInstance("SHA-256").digest(content);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hash;
}
// 更新RAS的密钥对
public static void updateRASKey() {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
rsaPublicKey = keyPair.getPublic().getEncoded();
rsaPrivateKey = keyPair.getPrivate().getEncoded();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
// RAS加密
public static byte[] rsaEncrypt(byte[] content) {
byte[] result = null;
try {
Cipher cipher = Cipher.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(rsaPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
result = cipher.doFinal(content);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return result;
}
// RSA解密
public static byte[] rsaDecrypt(byte[] content) {
byte[] result = null;
try {
Cipher cipher = Cipher.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(rsaPrivateKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
result = cipher.doFinal(content);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return result;
}
// AES使用CBC模式需要提供初始化向量,由密码的MD5前16位生成
private static byte[] getIV(byte[] password) {
byte[] key = new byte[16];
byte[] passwordMD5 = getMD5(password);
System.arraycopy(passwordMD5, 0, key, 0, 16);
return key;
}
// 根据密码生成AES密钥
private static SecretKey generateAESKey(byte[] password) {
SecretKey secretKey = null;
KeyGenerator keyGenerator = null;
try {
keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); // SHA1 Pseudo random number generator
random.setSeed(password);
keyGenerator.init(128, random);
secretKey = keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return secretKey;
}
// AES加密
public static byte[] aesEncrypt(byte[] content, byte[] password) {
byte[] result = null;
SecretKey key = new SecretKeySpec(generateAESKey(password).getEncoded(), "AES");
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
IvParameterSpec iv = new IvParameterSpec(getIV(password)); // 测试方便,这里初始化向量使用密码
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
result = cipher.doFinal(content);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return result;
}
// AES解密
public static byte[] aesDecrypt(byte[] content, byte[] password) {
byte[] result = null;
SecretKey key = new SecretKeySpec(generateAESKey(password).getEncoded(), "AES");
try{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
IvParameterSpec iv = new IvParameterSpec(getIV(password)); // 测试方便,这里初始化向量使用密码
cipher.init(Cipher.DECRYPT_MODE, key, iv);
result = cipher.doFinal(content);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return result;
}
public static String toHexString(byte[] hash) {
if (hash == null) {
return "";
}
StringBuilder sb = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xff) < 0x10) {
sb.append("0");
}
sb.append(Integer.toHexString(b & 0xff));
}
return sb.toString();
}
public static String toBase64(byte[] content) {
if (content == null) {
return "";
}
return Base64.getEncoder().encodeToString(content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment