java des3加密_java-Des3加密解密

packagecom.example.androi_test;importjava.security.Key;importjavax.crypto.Cipher;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESedeKeySpec;importjavax.crypto.spec.IvParameterSpec;importandroid.util.Base64;public classDes3 {public static void main(String[] args) throwsException {newDes3();

}public Des3() throwsException {//密钥

byte[] key = Base64.decode("12345678901234567890123456789000", Base64.DEFAULT);byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8};//原始数据

String mainStr = "1234567890";//原始数据转byte

byte[] data = mainStr.getBytes("UTF-8");

System.out.println("原始数据:" +mainStr);

System.out.println("ECB加密解密");//加密后的byte[]

byte[] encodeByte_ECB =des3EncodeECB(key, data);//加密后的String 需把加密的byte[]转base64

String encodeString_ECB =Base64.encodeToString(encodeByte_ECB,

Base64.DEFAULT);

System.out.println("加密的数据是:" +encodeString_ECB);//解密后的原始byte[] 需把加密后的byte[]转bass64

byte[] decodeByteMain_ECB =Base64.decode(encodeString_ECB,

Base64.DEFAULT);//解密后的byte[]

byte[] decodeByte_ECB =ees3DecodeECB(key, decodeByteMain_ECB);//解密后的String

String decodeString_ECB = new String(decodeByte_ECB, "UTF-8");

System.out.println("解密后的数据是:" +decodeString_ECB);

System.out.println("CBC加密解密");//加密后的byte[]

byte[] encodeByte_CBC =des3EncodeCBC(key, keyiv, data);//加密后的String 需把加密的byte[]转base64

String encodeString_CBC =Base64.encodeToString(encodeByte_CBC,

Base64.DEFAULT);

System.out.println("加密的数据是:" +encodeString_CBC);//解密后的原始byte[] 需把加密后的byte[]转bass64

byte[] decodeByteMain_CBC =Base64.decode(encodeString_CBC,

Base64.DEFAULT);//解密后的byte[]

byte[] decodeByte_CBC =des3DecodeCBC(key, keyiv, decodeByteMain_CBC);//解密后的String

String decodeString_CBC = new String(decodeByte_CBC, "UTF-8");

System.out.println("解密后的数据是:" +decodeString_CBC);

}public static byte[] des3EncodeECB(byte[] key, byte[] data)throwsException {

Key deskey= null;

DESedeKeySpec spec= newDESedeKeySpec(key);

SecretKeyFactory keyfactory= SecretKeyFactory.getInstance("desede");

deskey=keyfactory.generateSecret(spec);

Cipher cipher= Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, deskey);byte[] bOut =cipher.doFinal(data);returnbOut;

}public static byte[] ees3DecodeECB(byte[] key, byte[] data)throwsException {

Key deskey= null;

DESedeKeySpec spec= newDESedeKeySpec(key);

SecretKeyFactory keyfactory= SecretKeyFactory.getInstance("desede");

deskey=keyfactory.generateSecret(spec);

Cipher cipher= Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

cipher.init(Cipher.DECRYPT_MODE, deskey);byte[] bOut =cipher.doFinal(data);returnbOut;

}public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)throwsException {

Key deskey= null;

DESedeKeySpec spec= newDESedeKeySpec(key);

SecretKeyFactory keyfactory= SecretKeyFactory.getInstance("desede");

deskey=keyfactory.generateSecret(spec);

Cipher cipher= Cipher.getInstance("desede" + "/CBC/PKCS5Padding");

IvParameterSpec ips= newIvParameterSpec(keyiv);

cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);byte[] bOut =cipher.doFinal(data);returnbOut;

}public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)throwsException {

Key deskey= null;

DESedeKeySpec spec= newDESedeKeySpec(key);

SecretKeyFactory keyfactory= SecretKeyFactory.getInstance("desede");

deskey=keyfactory.generateSecret(spec);

Cipher cipher= Cipher.getInstance("desede" + "/CBC/PKCS5Padding");

IvParameterSpec ips= newIvParameterSpec(keyiv);

cipher.init(Cipher.DECRYPT_MODE, deskey, ips);byte[] bOut =cipher.doFinal(data);returnbOut;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值