1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| SecureRandom sr = new SecureRandom();
DESKeySpec dks; try { dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(DES); cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(src); } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) { throw new CodecException("加密失败", e); }
|