java仿MD5格式的DES加解密源码
desen("52pojie","88888888");
//output: 7A619ACF6A730058
desout("7A619ACF6A730058","88888888");
//output: 52pojie
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
public static String desen(String CDKEY, String sKey) {
try {
DESKeySpec desKeySpec = new DESKeySpec(sKey.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyBytes = keyFactory.generateSecret(desKeySpec).getEncoded();
IvParameterSpec iv = new IvParameterSpec(keyBytes);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generateSecret(desKeySpec), iv);
byte[] encryptedBytes = cipher.doFinal(CDKEY.getBytes(StandardCharsets.UTF_8));
StringBuilder stringBuilder = new StringBuilder();
for (byte b : encryptedBytes) {
stringBuilder.append(String.format("%02X", b));
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String desout(String CDKEY, String sKey) {
try {
byte[] keyBytes = sKey.getBytes(StandardCharsets.US_ASCII);
byte[] ivBytes = sKey.getBytes(StandardCharsets.US_ASCII);
DESKeySpec desKeySpec = new DESKeySpec(keyBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keyFactory.generateSecret(desKeySpec), ivSpec);
byte[] encryptedBytes = new byte;
for (int i = 0; i < CDKEY.length() / 2; i++) {
int num = Integer.parseInt(CDKEY.substring(i * 2, i * 2 + 2), 16);
encryptedBytes = (byte) num;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(cipher.doFinal(encryptedBytes));
return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
} catch (Exception e) {
return CDKEY;
}
} 不错~不错~ 不错~不错~ 可以 ~支持 Mark一下,支持 好东西,收藏了。 学习了,感谢楼主
页:
[1]