java DES加密 ECB hex

package org.webrtc.live.one2many.Eencryption;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import java.security.MessageDigest;
import java.security.Security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 

public class DesEencryption {
    private static final Logger log = LoggerFactory.getLogger(DesEencryption.class);


    //数据填充方式  
    private static String des_ecb_pkcs7 = "DES/ECB/PKCS7Padding"; 

    static {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }
 
    public void test(){
        String sSrc = "255";
        String sKey = "+*^$@)%!";

        String strEcbEncrypt = ecbEncryptToHexString(sKey, sSrc);
        log.info("[test]  aes ecb Hex加密: {}", strEcbEncrypt);

        String strEcbDecrypt = ecbDecryptHexString(sKey, strEcbEncrypt);
        log.info("[test]  aes ecb Hex解密: {}", strEcbDecrypt);

    }

    // Aes128ecb - 加密
    /**************************************************************************************************************
    * @param  sKey              秘钥 必须16字节 
    * @param  value             需要加密的字符串
    * @return   String 加密后的数据
    ***************************************************************************************************************/
    public String ecbEncryptToHexString(String sKey, String sSrc)  {

        try {
            if (sKey == null || sSrc==null) {
                log.error("[ecbEncryptToHexString] Key|sSrc为空  Key: {}  sSrc: {} ", sKey, sSrc);
                return null;
            }
            // 判断Key是否为8位
            if (sKey.length() != 8) {
                log.error("[ecbEncryptToHexString] Key长度不是8位: {}", sKey);
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
            Cipher cipher = Cipher.getInstance(des_ecb_pkcs7);//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

            return  bytesToHexString(encrypted);
            
        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
        
    }

    // Aes128ecb - 解密
    /**************************************************************************************************************
    * @param  sKey              秘钥  必须16字节  
    * @param  value             需要加密的字符串
    * @return   String 加密后的数据
    ***************************************************************************************************************/
    public String ecbDecryptHexString(String sKey, String sSrc) {
        try {
            if (sKey == null || sSrc==null) {
                log.error("[ecbDecryptHexString] Key|sSrc为空  Key: {}  sSrc: {} ", sKey, sSrc);
                return null;
            }
            // 判断Key是否为8位
            if (sKey.length() != 8) {
                log.error("[ecbDecryptHexString] Key长度不是8位: {}", sKey);
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
            Cipher cipher = Cipher.getInstance(des_ecb_pkcs7);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = hexStringToBytes(sSrc);
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }



    public  String bytesToHexString(byte[] src){   
        StringBuilder stringBuilder = new StringBuilder("");   
        if (src == null || src.length <= 0) {   
            return null;   
        }   
        for (int i = 0; i < src.length; i++) {   
            int v = src[i] & 0xFF;   
            String hv = Integer.toHexString(v);   
            if (hv.length() < 2) {   
                stringBuilder.append(0);   
            }   
            stringBuilder.append(hv);   
        }   
        return stringBuilder.toString();   
    }   

    public  byte[] hexStringToBytes(String hexString) {   
        if (hexString == null || hexString.equals("")) {   
            return null;   
        }   
        hexString = hexString.toUpperCase();   
        int length = hexString.length() / 2;   
        char[] hexChars = hexString.toCharArray();   
        byte[] d = new byte[length];   
        for (int i = 0; i < length; i++) {   
            int pos = i * 2;   
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));   
        }   
        return d;   
    }   
 
    private byte charToByte(char c) {   
        return (byte) "0123456789ABCDEF".indexOf(c);   
    }

 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值