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[CDKEY.length() / 2];
for (int i = 0; i < CDKEY.length() / 2; i++) {
int num = Integer.parseInt(CDKEY.substring(i * 2, i * 2 + 2), 16);
encryptedBytes[i] = (byte) num;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(cipher.doFinal(encryptedBytes));
return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
} catch (Exception e) {
return CDKEY;
}
}