Commit 5d39575f authored by yanzg's avatar yanzg

修改实例化关系

parent 28384f7e
package com.yanzuoguang.util.helper; package com.yanzuoguang.util.helper;
import com.yanzuoguang.util.contants.SystemContants;
import com.yanzuoguang.util.exception.CodeException;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory; import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Key; import java.security.Key;
import java.security.SecureRandom; import java.security.SecureRandom;
...@@ -12,7 +16,7 @@ import java.security.SecureRandom; ...@@ -12,7 +16,7 @@ import java.security.SecureRandom;
* *
* @author 颜佐光 * @author 颜佐光
*/ */
public class DESHelper { public class DesHelper {
private static final String ALGORITHM_DES = "des"; private static final String ALGORITHM_DES = "des";
...@@ -24,7 +28,13 @@ public class DESHelper { ...@@ -24,7 +28,13 @@ public class DESHelper {
* @return byte[] * @return byte[]
*/ */
public static String DESEncrypt(final String key, final String content) { public static String DESEncrypt(final String key, final String content) {
return RsaHelper.encodeBase64(processCipher(content.getBytes(), getSecretKey(key), Cipher.ENCRYPT_MODE, ALGORITHM_DES)); try {
byte[] from = content.getBytes(SystemContants.UTF8);
byte[] to = processCipher(from, getSecretKey(key), Cipher.ENCRYPT_MODE, ALGORITHM_DES);
return RsaHelper.encodeBase64(to);
} catch (UnsupportedEncodingException ex) {
throw new CodeException("加密失败:" + ex.getMessage(), ex);
}
} }
/** /**
...@@ -34,8 +44,14 @@ public class DESHelper { ...@@ -34,8 +44,14 @@ public class DESHelper {
* @param encoderContent 已加密内容 * @param encoderContent 已加密内容
* @return byte[] * @return byte[]
*/ */
public static byte[] DESDecrypt(final String key, final String encoderContent) { public static String DESDecrypt(final String key, final String encoderContent) {
return processCipher(RsaHelper.decodeBase64(encoderContent), getSecretKey(key), Cipher.DECRYPT_MODE, ALGORITHM_DES); try {
byte[] from = RsaHelper.decodeBase64(encoderContent);
byte[] to = processCipher(from, getSecretKey(key), Cipher.DECRYPT_MODE, ALGORITHM_DES);
return new String(to, SystemContants.UTF8);
} catch (UnsupportedEncodingException ex) {
throw new CodeException("解密失败:" + ex.getMessage(), ex);
}
} }
/** /**
......
package helper; package helper;
import com.yanzuoguang.util.helper.DESHelper; import com.yanzuoguang.util.helper.DesHelper;
import org.junit.Test; import org.junit.Test;
public class TestDes { public class TestDes {
...@@ -11,7 +11,7 @@ public class TestDes { ...@@ -11,7 +11,7 @@ public class TestDes {
@Test @Test
public void test() { public void test() {
System.out.println(str.length()); System.out.println(str.length());
String des = DESHelper.DESEncrypt(pwd, str); String des = DesHelper.DESEncrypt(pwd, str);
System.out.println(des.length()); System.out.println(des.length());
System.out.println(des); System.out.println(des);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment