|
@@ -4,6 +4,7 @@ import org.apache.commons.codec.binary.Base64;
|
|
|
|
|
|
import javax.crypto.*;
|
|
import javax.crypto.*;
|
|
import javax.crypto.spec.DESKeySpec;
|
|
import javax.crypto.spec.DESKeySpec;
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
@@ -25,7 +26,7 @@ public class EncrypDES {
|
|
private byte[] cipherByte;
|
|
private byte[] cipherByte;
|
|
|
|
|
|
public EncrypDES() throws Exception {
|
|
public EncrypDES() throws Exception {
|
|
- DESKeySpec desKey = new DESKeySpec(key.getBytes());
|
|
|
|
|
|
+ DESKeySpec desKey = new DESKeySpec(key.getBytes("utf-8"));
|
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
// 生成密钥
|
|
// 生成密钥
|
|
deskey = keyFactory.generateSecret(desKey);
|
|
deskey = keyFactory.generateSecret(desKey);
|
|
@@ -43,10 +44,10 @@ public class EncrypDES {
|
|
* @throws BadPaddingException
|
|
* @throws BadPaddingException
|
|
*/
|
|
*/
|
|
public String encrytor(String str) throws InvalidKeyException,
|
|
public String encrytor(String str) throws InvalidKeyException,
|
|
- IllegalBlockSizeException, BadPaddingException {
|
|
|
|
|
|
+ IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
|
|
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
|
|
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
|
|
c.init(Cipher.ENCRYPT_MODE, deskey);
|
|
c.init(Cipher.ENCRYPT_MODE, deskey);
|
|
- byte[] src = str.getBytes();
|
|
|
|
|
|
+ byte[] src = str.getBytes("utf-8");
|
|
// 加密,结果保存进cipherByte
|
|
// 加密,结果保存进cipherByte
|
|
cipherByte = c.doFinal(src);
|
|
cipherByte = c.doFinal(src);
|
|
return Base64.encodeBase64String(cipherByte);
|
|
return Base64.encodeBase64String(cipherByte);
|
|
@@ -61,13 +62,13 @@ public class EncrypDES {
|
|
* @throws IllegalBlockSizeException
|
|
* @throws IllegalBlockSizeException
|
|
* @throws BadPaddingException
|
|
* @throws BadPaddingException
|
|
*/
|
|
*/
|
|
- public byte[] decryptor(String str) throws InvalidKeyException,
|
|
|
|
- IllegalBlockSizeException, BadPaddingException {
|
|
|
|
|
|
+ public String decryptor(String str) throws InvalidKeyException,
|
|
|
|
+ IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
|
|
byte[] buff = Base64.decodeBase64(str);
|
|
byte[] buff = Base64.decodeBase64(str);
|
|
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
|
|
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
|
|
c.init(Cipher.DECRYPT_MODE, deskey);
|
|
c.init(Cipher.DECRYPT_MODE, deskey);
|
|
cipherByte = c.doFinal(buff);
|
|
cipherByte = c.doFinal(buff);
|
|
- return cipherByte;
|
|
|
|
|
|
+ return new String(cipherByte, "utf-8");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|