/**
* 3des解密
*
* @param ss
* 要解密的数据
* @param deskey
* 生成密钥用的数组
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] DesDecode(byte[] ss, byte[] deskey)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
//负责完成加密或解密工
Cipher c = Cipher.getInstance(modeDes);
//格式化密
SecretKeySpec k = new SecretKeySpec(deskey, modeDesKey);
// 根据密钥,对Cipher对象进行初始ENCRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, k);
// 解密,结果保存进dec
byte[] dec = c.doFinal(ss);
return dec;
}
/**
* 3des加密
*
* @param ss
* 要加密的字符
* @param deskey
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws UnsupportedEncodingException
*/
public static byte[] DesEncode(byte[] ss, byte[] deskey)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
//负责完成加密或解密工
Cipher c = Cipher.getInstance(modeDes);
//格式化密
SecretKeySpec k = new SecretKeySpec(deskey, modeDesKey);
// 根据密钥,对Cipher对象进行初始ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, k);
byte[] src = ss;
// 加密,结果保存进enc
byte[] enc = c.doFinal(ss);
return enc;
}
本文出自 “当初” 博客,请务必保留此出处http://9497786.blog.51cto.com/9487786/1749999
原文地址:http://9497786.blog.51cto.com/9487786/1749999