DESCode
Posted blakflash000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } }
以上是关于DESCode的主要内容,如果未能解决你的问题,请参考以下文章