DES加密解密
public class DESCoder { public static final String KEY_ALGORITHM = "DES"; public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding"; private Key toKey(byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException { DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(dks); return secretKey; } public byte[] decrypt(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { Key k = toKey(key); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(2, k); return cipher.doFinal(data); } public byte[] encrypt(byte[] data, byte[] key) throws Exception, NoSuchAlgorithmException, InvalidKeySpecException { Key k = toKey(key); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(1, k); return cipher.doFinal(data); } public byte[] initKey() throws NoSuchAlgorithmException { KeyGenerator kg = KeyGenerator.getInstance("DES"); kg.init(56); SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); } public void test2() throws Exception { byte[] key = initKey(); byte[] key2 = Base64.encode(key); System.out.println("???????" + new String(key2)); String str1 = "??????????????"; byte[] b1 = str1.getBytes(); byte[] b2 = Base64.encode(b1); byte[] b3 = encrypt(b2, key2); System.out.println("?????????????????Base64?????????)???" + new String(b3)); byte[] bb1 = Base64.encode(b3); System.out.println("????????????" + new String(bb1)); byte[] bb2 = Base64.decode(bb1); byte[] b4 = decrypt(bb2, key2); System.out.println("?????????Base64???????????" + new String(b4)); byte[] b5 = Base64.decode(b4); System.out.println("?????" + new String(b5)); } public static void main(String[] args) throws Exception { DESCoder d = new DESCoder(); String key = d.a(); String b = d.b("sjdlf", key); String c = d.c(b, key); System.out.println(key + " " + b + " " + c); } public String a() throws Exception { byte[] key = initKey(); byte[] key2 = Base64.encode(key); return new String(key2); } public String b(String s, String keyh) throws Exception { String str1 = s; byte[] b1 = str1.getBytes(); byte[] b2 = Base64.encode(b1); byte[] b3 = encrypt(b2, keyh.getBytes()); byte[] bb1 = Base64.encode(b3); String str = new String(bb1); return str; } public String c(String miwen, String keyh) throws Exception { byte[] bb2 = Base64.decode(miwen.getBytes()); byte[] b4 = decrypt(bb2, keyh.getBytes()); byte[] b5 = Base64.decode(b4); String ss = new String(b5); return ss; } public void saveKey(String str, File file) throws Exception { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write(str); bw.flush(); if (bw != null) { bw.close(); } } public String getFileKey(File file) throws Exception { String str; FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); StringBuilder sb = new StringBuilder(); while ((str = br.readLine()) != null) { sb.append(str); } if (fr != null) { fr.close(); } if (br != null) { br.close(); } return sb.toString(); } }
以雷霆击碎黑暗

浙公网安备 33010602011771号