3DES_ECB_加密解密

2023-01-31 04:01:46 des
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[] src,byte[] key) {
            try {
                System.out.println("没到8bytes:"+Util.byteArrayToHexString(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(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[] 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(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 src) {
            if(src.length()%16==0){
                return src;
            }else if(src.length()%16==14){
                return src+"80";
            }else{
                src+"80";
                 while(src.length()%8!=0){
                    src+="0";
                }
                return src;
            }
        }
        public static String DES_ECB_EN(String data){
            String msg = length_process(data);         
            byte[]  Util.hexStringToByteArray(msg);
             byte[] secretStr = SecretUtils.encryptMode(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);
        }          
        }


相关文章