3DES_ECB_加密解密
短信预约 -IT技能 免费直播动态提醒
import java.util.ArrayList;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class SecretUtils {
//根秘钥
final static byte[] keyBytes = Util.hexStringToByteArray("404142434445464748494A4B4C4D4E4F48494A4B4C4D4E4F");
private static final String Algorithm = "DESede"; //3DES算法
private static byte[] ivs = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
private static IvParameterSpec iv = new IvParameterSpec(ivs);
public static byte[] encryptMode(byte[] class="lazy" data-src,byte[] key) {
try {
System.out.println("没到8bytes:"+Util.byteArrayToHexString(class="lazy" data-src));
SecretKey deskey = new SecretKeySpec(key, Algorithm); //生成密钥21
Cipher c1 = Cipher.getInstance("DESede/ECB/NoPadding"); //实例化负责加密/解密的Cipher工具类22
c1.init(Cipher.ENCRYPT_MODE, deskey); //初始化为加密模式23
return c1.doFinal(class="lazy" data-src); } catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace(); } catch (java.lang.Exception e3) {
e3.printStackTrace();}
return null; }
public static byte[] decryptMode(byte[] class="lazy" data-src ,byte[] key) {
try {SecretKey deskey = new SecretKeySpec(key, Algorithm);
Cipher c1 = Cipher.getInstance("DESede/ECB/NoPadding");
c1.init(Cipher.DECRYPT_MODE, deskey); //初始化为解密模式44
return c1.doFinal(class="lazy" data-src); } catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace(); } catch (java.lang.Exception e3) {
e3.printStackTrace(); } return null; }
private static String length_process(String class="lazy" data-src) {
if(class="lazy" data-src.length()%16==0){
return class="lazy" data-src;
}else if(class="lazy" data-src.length()%16==14){
return class="lazy" data-src+"80";
}else{
class="lazy" data-src+"80";
while(class="lazy" data-src.length()%8!=0){
class="lazy" data-src+="0";
}
return class="lazy" data-src;
}
}
public static String DES_ECB_EN(String data){
String msg = length_process(data);
byte[] Util.hexStringToByteArray(msg);
byte[] secretStr = SecretUtils.encryptMode(class="lazy" data-src,keyBytes); //调用加密方法16
System.out.println("【Card Cryptogram DATA明文是】:" + msg);
System.out.println("【Card Cryptogram 全部加密文是】:" + Util.byteArrayToHexString(secretStr));
return Util.byteArrayToHexString(secretStr);
}
public static String DES_ECB_DE(String secretStr){
byte[] myMsg = SecretUtils.decryptMode(Util.hexStringToByteArray(secretStr),keyBytes); //调用解密方法17
System.out.println("【Card Cryptogram DATA解密是】:" + Util.byteArrayToHexString(myMsg));
return Util.byteArrayToHexString(myMsg);
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341