📄 des.java
字号:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* @author leo
* @since jkd1.4
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DESAlgorithm {
private static String algorithm = "DESede";
/**
* generator a SecretKey
*
* @param keyByte
* @return SecreKey
* @throws Exception
*/
public static SecretKey genDESKey(byte[] keyByte) throws Exception {
SecretKey key = new SecretKeySpec(keyByte, algorithm);
return key;
}
/**
* encode String in des
*
* @param key
* @param crypt
* @return @throws
* Exception
*/
public static byte[] desEncrypt(SecretKey key, String crypt)
throws Exception {
return desEncrypt(key, crypt.getBytes());
}
/**
* encode byte array in des
*
* @param key
* @param crypt
* @return byte[]
* @throws Exception
*/
public static byte[] desEncrypt(SecretKey key, byte[] crypt)
throws Exception {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(crypt);
}
/**
* decode String in des
*
* @param key
* @param crypt
* @return @throws
* Exception
*/
public static byte[] desDecrypt(SecretKey key, String crypt)
throws Exception {
return desDecrypt(key, crypt.getBytes());
}
/**
* decode byte array in des
*
* @param key
* @param crypt
* @return byte[]
* @throws Exception
*/
public static byte[] desDecrypt(SecretKey key, byte[] crypt)
throws Exception {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(crypt);
}
/**
* @return Returns the algorithm.
*/
public static String getAlgorithm() {
return algorithm;
}
/**
* @param algorithm
* The algorithm to set.
*/
public static void setAlgorithm(String algorithm) {
DESAlgorithm.algorithm = algorithm;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -