oracle中的encrypt_des/加密对应Java的加密方式

package cn.sh.ideal.encryption;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class DesUtil {
    
    

    private static String key = "12345678";

    /**
     * 加密
     *
     * @param inStr
     * @return
     */
    public static String enTel(String inStr) {
    
    
        DESKeySpec desKey;
        SecretKey securekey;
        Cipher cipher;
        try {
    
    

            desKey = new DESKeySpec(key.getBytes());
            securekey = SecretKeyFactory.getInstance("DES").generateSecret(desKey);
            cipher = Cipher.getInstance("DES/CBC/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, securekey, new IvParameterSpec(new byte[8]));
            byte[] inBytes = new byte[((int) (inStr.length() / 8) + 1) * 8];
            for (int i = 0; i < inStr.length(); i++) {
    
    
                inBytes[i] = inStr.getBytes()[i];
            }
            byte[] enBytes = cipher.doFinal(inBytes);
            //String hexStr = DatatypeConverter.printHexBinary(enBytes);
            String hexStr =DesUtil.bytesToHexString(enBytes);
            return hexStr;

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

        return null;

    }

    /**
     * 解密
     *
     * @param encryptStr
     * @return
     */
    public static String deTel(String encryptStr) {
    
    
        DESKeySpec desKey;
        SecretKey securekey;
        Cipher cipher;
        try {
    
    
            desKey = new DESKeySpec(key.getBytes());
            securekey = SecretKeyFactory.getInstance("DES").generateSecret(desKey);
            cipher = Cipher.getInstance("DES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, securekey, new IvParameterSpec(new byte[8]));
            byte[] decryptBytes = cipher.doFinal(Hex.decodeHex(encryptStr.toCharArray()));
            return new String(decryptBytes).trim();

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

        return null;

    }


    /**
     * 把字节数组转换成16进制字符串
     *
     * @param bArray
     * @return
     */
    public static final String bytesToHexString(byte[] bArray) {
    
    
        if(bArray == null )
        {
    
    
            return "";
        }
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
    
    
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) {
    
    
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
    
    
        System.out.println("加密:" + enTel("86217280"));
    }


}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/SuperstarSteven/article/details/109098995