DESCode
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.security.NoSuchAlgorithmException; public class DESCoder { public static final String ALGORITHM = "DES"; private static SecretKey toKey(byte[] key) throws Exception { DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey secretKey = keyFactory.generateSecret(dks); return secretKey; } public static byte[] decrypt(byte[] data, byte[] key) { byte[] bytes = new byte[0]; try { SecretKey k = toKey(key); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, k); bytes = cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); } return bytes; } public static byte[] encrypt(byte[] data, byte[] key) { Cipher cipher; byte[] ret_byte = null; try { SecretKey k = toKey(key); cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, k); ret_byte = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return ret_byte; } }