1.字符串加密工具类
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class EncodeUtil {
public static String binary(byte[] bytes, int radix) {
return new BigInteger(1, bytes).toString(radix);
}
public static String base64Encode(byte[] bytes) {
return new String(Base64.encodeBase64Chunked(bytes));
}
public static byte[] base64Decode(String base64Code) throws Exception {
return Base64.decodeBase64(base64Code.getBytes());
}
public static byte[] md5(byte[] bytes) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes);
return md.digest();
}
public static byte[] md5(String msg) throws Exception {
return md5(msg.getBytes());
}
public static String md5Encrypt(String msg) throws Exception {
return base64Encode(md5(msg));
}
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(encryptKey.getBytes());
kgen.init(128, random);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(1, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
public static String aesEncrypt(String content, String encryptKey) throws Exception {
return base64Encode(aesEncryptToBytes(content, encryptKey));
}
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(decryptKey.getBytes());
kgen.init(128, random);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(2, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
return aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
}
public static void main(String[] args) {
try {
//
System.out.println(EncodeUtil.aesEncrypt("123", "PlatformSecurity"));//加密
System.out.println(EncodeUtil.aesDecrypt("BD0v9KC5OGPdFM1yfsRC9w==", "PlatformSecurity"));//解密
} catch (Exception var2) {
var2.printStackTrace();
}
}
}
2.文件加密工具类
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.servlet.ServletOutputStream;
public class FileSecurity {
private Key key;
public SimuFileSecurity(String str) {
getKey(str);
}
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(strKey.getBytes());
_generator.init(secureRandom);
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause:" + e);
}
}
//文件加密并保存到指定目录
public void encrypt(InputStream is, String destFile) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, this.key);
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}
//文件解密
public void decrypt(InputStream is, ServletOutputStream servletOutputStream) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, this.key);
CipherOutputStream cos = new CipherOutputStream(servletOutputStream, cipher);
byte[] buffer = new byte[102400];
int r;
while((r = is.read(buffer)) > 0) {
cos.write(buffer, 0, r);
}
cos.close();
is.close();
servletOutputStream.close();
}
public void decrypts(InputStream is, OutputStream out) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, this.key);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[102400];
int r;
while((r = is.read(buffer)) > 0) {
cos.write(buffer, 0, r);
}
cos.close();
is.close();
out.close();
}
}