51,411
社区成员
发帖
与我相关
我的任务
分享
/**
* 消息摘要
*
* @param input
* @return
* @throws NoSuchAlgorithmException
*/
public byte[] theOne(byte[] input) throws NoSuchAlgorithmException {
MessageDigest instance = MessageDigest.getInstance("md5");
return instance.digest(input);
}
/**
* 对称算法
*
* @param input
*
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public byte[] theOther(byte[] input) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
Cipher c = Cipher.getInstance("DES");
KeyGenerator keygen = null;
keygen = KeyGenerator.getInstance("DES");
keygen.init(56);
Key key = keygen.generateKey();// 这个key要用ObjectOutputStream 留下来
c.init(Cipher.ENCRYPT_MODE, key);
return c.doFinal(input);
}
/**
* 解密对称算法
*
* @param key 之前生成的key
* @param input
* @return
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public byte[] deTheOther(Key key, byte[] input)
throws IllegalBlockSizeException, BadPaddingException,
InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
Cipher c = Cipher.getInstance("DES");
c.init(Cipher.DECRYPT_MODE, key);
return c.doFinal(input);
}