📄 keytest.java
字号:
package test;
import java.security.Security;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
/**
* <p>Title: 网络信息安全统一管理中心</p>
*
* <p>Description: 中心端业务及页面程序:</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: 深圳市艾默特有限责任公司 corp. AMT</p>
*
* @author 凌微丰
* @version 1.0
*/
public class KeyTest {
public KeyTest() {
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
}
/**
* 算法
*/
private static final String Algorithm = "DESede";
/**
* 24字节的密钥
*/
private static final byte[] keyBytes = {
0x13, 0x23, 0x3F, 0x58, (byte) 0x87, 0x11, 0x41, 0x37
, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD, 0x53, 0x67
, 0x77, 0x29, 0x71, (byte) 0x97, 0x37, 0x43, 0x31, (byte) 0xE1};
/**
* 加密过程
* @param keybyte byte[]
* @param src byte[]
* @return byte[]
*/
private static byte[] encryptMode(byte[] keybyte, byte[] src) {
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
}
catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
}
catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
/**
* 解密过程
* @param keybyte byte[]
* @param src byte[]
* @return byte[]
*/
private static byte[] decryptMode(byte[] keybyte, byte[] src) {
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
}
catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
}
catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
/**
* 这是供用户调用的接口
* 输入要加密的字符串,输出加密后的字符串
* @param info String
* @return String
*/
public static String encrypt(String info) {
String res = null;
byte[] tem = null;
try {
tem = encryptMode(keyBytes, info.getBytes());
res = new String(tem, "iso-8859-1");
}
catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
* 这是供用户调用的接口
* 输入要解密的字符串,输出解密后的字符串
* @param info String
* @return String
*/
public static String decrypt(String info) {
String res = null;
byte[] tem = null;
try {
tem = decryptMode(keyBytes, info.getBytes("iso-8859-1"));
}
catch (Exception e) {
e.printStackTrace();
}
res = new String(tem);
return res;
}
// public static void main(String args[]){
// KeyTest keyTest=new KeyTest();
// String theKey = "2357";
// String key = keyTest.encrypt(theKey);
// String theDK = keyTest.decrypt(key);
// System.out.println("---------- "+key);
// System.out.println("========== "+theDK);
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -