您的位置:首页 > 编程语言 > Java开发

java 的3DES解密和加密

2016-03-11 16:51 351 查看
/**
* 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 3des加密