Commit f31d29a2 by yuwei

Update: MD5加密密钥修改

parent 2a173a02
...@@ -12,10 +12,12 @@ import java.util.Base64; ...@@ -12,10 +12,12 @@ import java.util.Base64;
public class MD5Util { public class MD5Util {
//向量(同时拥有向量和密匙才能解密),此向量必须是8byte,多少都报错 /** 向量(同时拥有向量和密匙才能解密),此向量必须是8byte,多少都报错 */
private final byte[] DESIV = new byte[] { 0x22, 0x54, 0x36, 110, 0x40, (byte) 0xac, (byte) 0xad, (byte) 0xdf };// 向量 private final byte[] DESIV = new byte[] { 0x22, 0x54, 0x36, 110, 0x40, (byte) 0xac, (byte) 0xad, (byte) 0xdf };
private final String deSkey = "datax-cloud";// 自定义密钥,个数不能太短,太短报错,过长,它默认只取前N位(N的具体值,大家另行查找资料) /** 自定义密钥,个数不能太短,太短报错,过长,它默认只取前N位(N的具体值,大家另行查找资料) */
private AlgorithmParameterSpec iv = null;// 加密算法的参数接口 private final String deSkey = "datax-cloud";
/** 加密算法的参数接口 */
private AlgorithmParameterSpec iv = null;
private Key key = null; private Key key = null;
private String charset = "UTF-8"; private String charset = "UTF-8";
...@@ -26,10 +28,14 @@ public class MD5Util { ...@@ -26,10 +28,14 @@ public class MD5Util {
* @throws Exception * @throws Exception
*/ */
private MD5Util() throws Exception { private MD5Util() throws Exception {
DESKeySpec keySpec = new DESKeySpec(deSkey.getBytes(this.charset));// 设置密钥参数 // 设置密钥参数
iv = new IvParameterSpec(DESIV);// 设置向量 DESKeySpec keySpec = new DESKeySpec(deSkey.getBytes(this.charset));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂 // 设置向量
key = keyFactory.generateSecret(keySpec);// 得到密钥对象 iv = new IvParameterSpec(DESIV);
// 获得密钥工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 得到密钥对象
key = keyFactory.generateSecret(keySpec);
} }
public static MD5Util getInstance() throws Exception { public static MD5Util getInstance() throws Exception {
...@@ -63,8 +69,10 @@ public class MD5Util { ...@@ -63,8 +69,10 @@ public class MD5Util {
* @throws Exception * @throws Exception
*/ */
public String encode(String data) throws Exception { public String encode(String data) throws Exception {
Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher // 得到加密对象Cipher
enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量 Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
// 设置工作模式为加密模式,给出密钥和向量
enCipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] pasByte = enCipher.doFinal(data.getBytes(this.charset)); byte[] pasByte = enCipher.doFinal(data.getBytes(this.charset));
return Base64.getEncoder().encodeToString(pasByte); return Base64.getEncoder().encodeToString(pasByte);
} }
...@@ -99,10 +107,12 @@ public class MD5Util { ...@@ -99,10 +107,12 @@ public class MD5Util {
StringBuffer buf = new StringBuffer(""); StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) { for (int offset = 0; offset < b.length; offset++) {
i = b[offset]; i = b[offset];
if (i < 0) if (i < 0) {
i += 256; i += 256;
if (i < 16) }
if (i < 16) {
buf.append("0"); buf.append("0");
}
buf.append(Integer.toHexString(i)); buf.append(Integer.toHexString(i));
} }
result = buf.toString(); result = buf.toString();
......
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