|
|
@@ -16,22 +16,23 @@ public class DESUtils {
|
|
|
/**
|
|
|
* 随意定一个私钥(长度必须为24位)
|
|
|
*/
|
|
|
- public static final String SECRET_KEY = "7947A28811702D0EE20F944F";
|
|
|
+ private static final String SECRET_KEY = "7947A28811702D0EE20F944F";
|
|
|
+
|
|
|
+ private static final String INSTANCE_STR = "DESede";
|
|
|
|
|
|
/**
|
|
|
* 加密
|
|
|
*
|
|
|
- * @param inStr 需要加密的内容
|
|
|
- * @param secretKey 密钥
|
|
|
+ * @param inStr 需要加密的内容
|
|
|
* @return 加密后的数据
|
|
|
*/
|
|
|
|
|
|
- public static String encrypt(String inStr, String secretKey) {
|
|
|
- SecretKey deskey = new SecretKeySpec(secretKey.getBytes(), "DESede");
|
|
|
+ public static String encrypt(String inStr) {
|
|
|
+ SecretKey deskey = new SecretKeySpec(SECRET_KEY.getBytes(), INSTANCE_STR);
|
|
|
Cipher cipher;
|
|
|
String outStr = null;
|
|
|
try {
|
|
|
- cipher = Cipher.getInstance("DESede");
|
|
|
+ cipher = Cipher.getInstance(INSTANCE_STR);
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, deskey);
|
|
|
outStr = byte2hex(cipher.doFinal(inStr.getBytes()));
|
|
|
} catch (Exception e) {
|
|
|
@@ -44,15 +45,14 @@ public class DESUtils {
|
|
|
* 解密
|
|
|
*
|
|
|
* @param inStr 需要解密的内容
|
|
|
- * @param secretKey 密钥
|
|
|
* @return 解密后的数据
|
|
|
*/
|
|
|
- public static String decrypt(String inStr, String secretKey) {
|
|
|
- SecretKey deskey = new SecretKeySpec(secretKey.getBytes(), "DESede");
|
|
|
+ public static String decrypt(String inStr) {
|
|
|
+ SecretKey deskey = new SecretKeySpec(SECRET_KEY.getBytes(), INSTANCE_STR);
|
|
|
Cipher cipher;
|
|
|
String outStr = null;
|
|
|
try {
|
|
|
- cipher = Cipher.getInstance("DESede");
|
|
|
+ cipher = Cipher.getInstance(INSTANCE_STR);
|
|
|
cipher.init(Cipher.DECRYPT_MODE, deskey);
|
|
|
outStr = new String(cipher.doFinal(hex2byte(inStr)));
|
|
|
} catch (Exception e) {
|
|
|
@@ -108,9 +108,9 @@ public class DESUtils {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
String data = "05367892";
|
|
|
- String key = DESUtils.encrypt(data,SECRET_KEY);
|
|
|
+ String key = DESUtils.encrypt(data);
|
|
|
System.out.println("加密内容="+key);
|
|
|
- String res = DESUtils.decrypt(key,SECRET_KEY);
|
|
|
+ String res = DESUtils.decrypt(key);
|
|
|
System.out.println("解密内容="+res);
|
|
|
}
|
|
|
}
|