java实现DES和CBC加密import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
pto.Cipher;
java加密方式有哪些pto.SecretKeyFactory;
pto.spec.DESedeKeySpec;
pto.spec.IvParameterSpec;
import java.security.Key;
public class ThreeDESCBC {
/**
* @param key  密钥
* @param data 明⽂
* @return Base64编码的密⽂
* @throws Exception
* @Description ECB加密,不要IV
*/
public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = Instance("desede");
deskey = ateSecret(spec);
Cipher cipher = Instance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* @param key  密钥
* @param data Base64编码的密⽂
* @return 明⽂
* @throws Exception
* @Description ECB解密,不要IV
*/
public static byte[] ees3DecodeECB(byte[] key, byte[] data) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = Instance("desede");
deskey = ateSecret(spec);
Cipher cipher = Instance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* @param key  密钥
* @param keyiv IV
* @param data  明⽂
* @return Base64编码的密⽂
* @throws Exception
* @Description CBC加密
*/
public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = Instance("desede");
deskey = ateSecret(spec);
Cipher cipher = Instance("desede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* @param key  密钥
* @param keyiv IV

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。