5 changed files with 163 additions and 14 deletions
-
100src/main/java/com/example/demo/Util/BaseDES.java
-
2src/main/java/com/example/demo/controller/DetailYController.java
-
4src/main/java/com/example/demo/mapper/DetailYMapper.java
-
69src/main/java/com/example/demo/serviceImpl/OtherServiceImpl.java
-
2src/main/java/com/example/demo/sevice/OtherService.java
@ -0,0 +1,100 @@ |
|||
package com.example.demo.Util; |
|||
|
|||
|
|||
import javax.crypto.Cipher; |
|||
import java.security.*; |
|||
|
|||
|
|||
public class BaseDES { |
|||
private static String strDefaultKey = "98hltleg"; |
|||
|
|||
private Cipher encryptCipher = null; |
|||
|
|||
private Cipher decryptCipher = null; |
|||
|
|||
public static String byteArr2HexStr(byte[] arrB) throws Exception { |
|||
int iLen = arrB.length; |
|||
|
|||
StringBuffer sb = new StringBuffer(iLen * 2); |
|||
for (int i = 0; i < iLen; i++) { |
|||
int intTmp = arrB[i]; |
|||
|
|||
while (intTmp < 0) { |
|||
intTmp = intTmp + 256; |
|||
} |
|||
|
|||
if (intTmp < 16) { |
|||
sb.append("0"); |
|||
} |
|||
sb.append(Integer.toString(intTmp, 16)); |
|||
} |
|||
return sb.toString(); |
|||
} |
|||
|
|||
public static byte[] hexStr2ByteArr(String strIn) { |
|||
byte[] arrB = strIn.getBytes(); |
|||
int iLen = arrB.length; |
|||
|
|||
byte[] arrOut = new byte[iLen / 2]; |
|||
for (int i = 0; i < iLen; i = i + 2) { |
|||
String strTmp = new String(arrB, i, 2); |
|||
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); |
|||
} |
|||
return arrOut; |
|||
} |
|||
|
|||
public BaseDES() throws Exception { |
|||
this(strDefaultKey); |
|||
} |
|||
|
|||
public BaseDES(String strKey) throws Exception { |
|||
|
|||
Key key = getKey(strKey.getBytes()); |
|||
|
|||
encryptCipher = Cipher.getInstance("DES"); |
|||
encryptCipher.init(Cipher.ENCRYPT_MODE, key); |
|||
|
|||
decryptCipher = Cipher.getInstance("DES"); |
|||
decryptCipher.init(Cipher.DECRYPT_MODE, key); |
|||
} |
|||
|
|||
public byte[] encrypt(byte[] arrB) throws Exception { |
|||
return encryptCipher.doFinal(arrB); |
|||
} |
|||
|
|||
public String encrypt(String strIn) throws Exception { |
|||
return byteArr2HexStr(encrypt(strIn.getBytes())); |
|||
} |
|||
|
|||
public byte[] decrypt(byte[] arrB) throws Exception { |
|||
return decryptCipher.doFinal(arrB); |
|||
} |
|||
|
|||
public String decrypt(String strIn) throws Exception { |
|||
return new String(decrypt(hexStr2ByteArr(strIn))); |
|||
} |
|||
|
|||
private Key getKey(byte[] arrBTmp) { |
|||
|
|||
byte[] arrB = new byte[8]; |
|||
|
|||
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { |
|||
arrB[i] = arrBTmp[i]; |
|||
} |
|||
|
|||
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); |
|||
|
|||
return key; |
|||
} |
|||
|
|||
|
|||
public static void main(String args[]) { |
|||
try { |
|||
BaseDES d = new BaseDES(); |
|||
System.out.println("加密字符串:90005179》"+d.encrypt("90005179")); |
|||
System.out.println("解密字符串:6aaef5277c050f7ae383f816651098ff》"+d.decrypt("6aaef5277c050f7ae383f816651098ff")); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue