生成密钥
String key = "key123";
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
加密
SecureRandom random = new SecureRandom();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
byte[] result = cipher.doFinal(password.getBytes());
return Base64.getEncoder().encodeToString(result);
解密
SecureRandom random = new SecureRandom();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
byte[] result = cipher.doFinal(Base64.getDecoder().decode(password));
new String(result);