📄 crypt.java
字号:
package com.richeninfo.crypt;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author xiaolie
* @version 1.0
*/
import java.security.*;
import javax.crypto.*;
import java.io.*;
public class Crypt {
//static {
// Security.addProvider(new com.sun.crypto.provider.SunJCE());
//}
private SecretKey deskey;
private String algorithm = "Blowfish";
Cipher c1;
private static Crypt crypt = null;
public static Crypt getCrypt(String keyFileName, String algorithm)
throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException {
//Security.addProvider(new com.sun.crypto.provider.SunJCE());
if (crypt == null) {
return new Crypt(keyFileName, algorithm);
} else {
return crypt;
}
}
public static Crypt getCrypt(String keyFileName)
throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException {
//Security.addProvider(new com.sun.crypto.provider.SunJCE());
if (crypt == null) {
return new Crypt(keyFileName);
} else {
return crypt;
}
}
private Crypt(String keyFileName, String algorithm)
throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException {
this.algorithm = algorithm;
ObjectInputStream in = new java.io.ObjectInputStream(new java.io.FileInputStream(keyFileName));
deskey = (SecretKey) in.readObject();
in.close();
c1 = Cipher.getInstance(algorithm);
}
private Crypt(String keyFileName)
throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException {
ObjectInputStream in = new java.io.ObjectInputStream(new java.io.FileInputStream(keyFileName));
deskey = (SecretKey) in.readObject();
in.close();
c1 = Cipher.getInstance(algorithm);
}
public byte[] encrypt(byte[] data, int offset, int len) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(data, offset, len);
}
public byte[] encrypt(byte[] data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(data);
}
public byte[] decrypt(byte[] data, int offset, int len) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(data, offset, len);
}
public byte[] decrypt(byte[] data) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(data);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -