1. 程式人生 > >java各種加密方式

java各種加密方式

Base64加解密

private static final BASE64Encoder encoder = new BASE64Encoder();
    private static final BASE64Decoder decoder = new BASE64Decoder();

    /**
     * Base64加密
     *
     * @param toEncodeContent
     * @return
     */
    public static String encryptByBase64(String toEncodeContent) {
        if
("".equals(toEncodeContent)) { return null; } return encoder.encode(toEncodeContent.getBytes()); } /** * Base64解密 * * @param toDecodeContent * @return */ public static String decryptByBase64(String toDecodeContent) { if ("".equals(toDecodeContent)) { return
null; } byte[] buf = null; try { buf = decoder.decodeBuffer(toDecodeContent); } catch (IOException e) { log.error("EncryptUtil-->decryptByBase64 Base64解密失敗!",e); } return new String(buf); }

AES

/**
     * AES加密
     * @param
sSrc * @param sKey 使用16位key * @return * @throws Exception */
public static String encryptByAes(String sSrc, String sKey,String vi) throws Exception { if (sKey == null) { throw new NullPointerException("$$$key為空"); } // 判斷Key是否為16位 if (sKey.length() != 16) { throw new Exception("$$$key長度不等於16位"); } byte[] raw = sKey.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"演算法/模式/補碼方式" IvParameterSpec iv = new IvParameterSpec(vi.getBytes());//使用CBC模式,需要一個向量iv,可增加加密演算法的強度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(sSrc.getBytes()); return new BASE64Encoder().encode(encrypted);//此處使用BASE64做轉碼功能,同時能起到2次加密的作用。 } /** * AES解密 * @param sSrc * @param sKey * @return * @throws Exception */ public static String decryptByAes(String sSrc, String sKey,String vi) throws Exception { try { // 判斷Key是否正確 if ("".equals(sKey)) { throw new NullPointerException("$$$明文為空"); } // 判斷Key是否為16位 if (sKey.length() != 16) { throw new Exception("$$$Key長度不是16位"); } byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(vi.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密 try { byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original); return originalString; } catch (Exception e) { log.error("EncryptUtil-->decryptByAes AES解密失敗!",e); return null; } } catch (Exception ex) { log.error("EncryptUtil-->decryptByAes AES解密失敗!",ex); return null; } }

MD5

/**
     * 對字串md5加密
     * 返回32位大寫字串
     * @param str
     * @return
     */
    public static String getMD5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes("utf-8"));
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            //32位加密
            return buf.toString().toUpperCase();
            // 16位的加密
            //return buf.toString().substring(8, 24);
        } catch (Exception e) {
            log.info("EncryptUtil-->getMD5 對字串md5加密失敗!",e);
            return null;
        }
    }

RSA

   /**
     * 生成公鑰和私鑰
     * 
     * @throws NoSuchAlgorithmException
     */
    public static HashMap<String, Object> getRSAKeys() throws NoSuchAlgorithmException {
        HashMap<String, Object> map = new HashMap<String, Object>();
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        keyPairGen.initialize(512);
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        map.put("public", publicKey);
        map.put("private", privateKey);
        return map;
    }

    /**
     * 使用模和指數生成RSA公鑰
     * 注意:【此程式碼用了預設補位方式,為RSA/None/PKCS1Padding,不同JDK預設的補位方式可能不同,如 
     * Android預設是RSA
     * /None/NoPadding】
     *
     * @param modulus  模
     * @param exponent 指數
     * @return
     */
    public static RSAPublicKey getRSAPublicKey(String modulus, String exponent) {
        try {
            BigInteger b1 = new BigInteger(modulus);
            BigInteger b2 = new BigInteger(exponent);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
            return (RSAPublicKey) keyFactory.generatePublic(keySpec);
        } catch (Exception e) {
            log.info("EncryptUtil-->RSAPublicKey 生成RSA公鑰失敗!",e);
            return null;
        }
    }

    /**
     * 使用模和指數生成RSA私鑰
     * 注意:【此程式碼用了預設補位方式,為RSA/None/PKCS1Padding,不同JDK預設的補位方式可能不同,如
     * Android預設是RSA
     * /None/NoPadding】
     *
     * @param modulus  模
     * @param exponent 指數
     * @return
     */
    public static RSAPrivateKey getRSAPrivateKey(String modulus, String exponent) {
        try {
            BigInteger b1 = new BigInteger(modulus);
            BigInteger b2 = new BigInteger(exponent);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
            return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
        } catch (Exception e) {
            log.info("EncryptUtil-->getRSAPrivateKey 生成RSA私鑰失敗!",e);
            return null;
        }
    }

    /**
     * 公鑰加密
     *
     * @param data
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static String encryptByRSAPublicKey(String data, RSAPublicKey publicKey)
            throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        // 模長
        int key_len = publicKey.getModulus().bitLength() / 8;
        // 加密資料長度 <= 模長-11
        String[] datas = splitString(data, key_len - 11);
        String mi = "";
        //如果明文長度大於模長-11則要分組加密
        for (String s : datas) {
            mi += bcd2Str(cipher.doFinal(s.getBytes()));
        }
        return mi;
    }

    /**
     * 私鑰解密
     *
     * @param data
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String decryptByRSAPrivateKey(String data, RSAPrivateKey privateKey)
            throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        //模長
        int key_len = privateKey.getModulus().bitLength() / 8;
        byte[] bytes = data.getBytes();
        byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
        //如果密文長度大於模長則要分組解密
        String ming = "";
        byte[][] arrays = splitArray(bcd, key_len);
        for (byte[] arr : arrays) {
            ming += new String(cipher.doFinal(arr));
        }
        return ming;
    }

    /**
     * ASCII碼轉BCD碼
     */
    public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {
        byte[] bcd = new byte[asc_len / 2];
        int j = 0;
        for (int i = 0; i < (asc_len + 1) / 2; i++) {
            bcd[i] = asc_to_bcd(ascii[j++]);
            bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++]) & 0xff) + (bcd[i] << 4));
        }
        return bcd;
    }

    public static byte asc_to_bcd(byte asc) {
        byte bcd;

        if ((asc >= '0') && (asc <= '9'))
            bcd = (byte) (asc - '0');
        else if ((asc >= 'A') && (asc <= 'F'))
            bcd = (byte) (asc - 'A' + 10);
        else if ((asc >= 'a') && (asc <= 'f'))
            bcd = (byte) (asc - 'a' + 10);
        else
            bcd = (byte) (asc - 48);
        return bcd;
    }

    /**
     * BCD轉字串
     */
    public static String bcd2Str(byte[] bytes) {
        char temp[] = new char[bytes.length * 2], val;

        for (int i = 0; i < bytes.length; i++) {
            val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
            temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');

            val = (char) (bytes[i] & 0x0f);
            temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
        }
        return new String(temp);
    }

    /**
     * 拆分字串
     */
    public static String[] splitString(String string, int len) {
        int x = string.length() / len;
        int y = string.length() % len;
        int z = 0;
        if (y != 0) {
            z = 1;
        }
        String[] strings = new String[x + z];
        String str = "";
        for (int i = 0; i < x + z; i++) {
            if (i == x + z - 1 && y != 0) {
                str = string.substring(i * len, i * len + y);
            } else {
                str = string.substring(i * len, i * len + len);
            }
            strings[i] = str;
        }
        return strings;
    }

    /**
     * 拆分陣列
     */
    public static byte[][] splitArray(byte[] data, int len) {
        int x = data.length / len;
        int y = data.length % len;
        int z = 0;
        if (y != 0) {
            z = 1;
        }
        byte[][] arrays = new byte[x + z][];
        byte[] arr;
        for (int i = 0; i < x + z; i++) {
            arr = new byte[len];
            if (i == x + z - 1 && y != 0) {
                System.arraycopy(data, i * len, arr, 0, y);
            } else {
                System.arraycopy(data, i * len, arr, 0, len);
            }
            arrays[i] = arr;
        }
        return arrays;
    }

DES

/**
     * 加密工具
     */
    private static Cipher desEncryptCipher = null;
    /**
     * 解密工具
     */
    private static Cipher desDecryptCipher = null;

    /**
     * 從指定字串生成金鑰,金鑰所需的位元組陣列長度為8位 不足8位時後面補0,超出8位只取前8位
     *
     * @param arrBTmp
     * @return
     * @throws Exception
     */
    public static Key getKey(byte[] arrBTmp) throws Exception {
        byte[] arrB = new byte[8];
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        Key key = new SecretKeySpec(arrB, "DES");
        return key;
    }

    /**
     * 將byte陣列轉換為表示16進位制值的字串, 如:byte[]{8,18}轉換為:0813, 和public static byte[]
     * hexStr2ByteArr(String strIn) 互為可逆的轉換過程
     *
     * @param arrB 需要轉換的byte陣列
     * @return 轉換後的字串
     * @throws Exception 本方法不處理任何異常,所有異常全部丟擲
     */
    public static String byteArr2HexStr(byte[] arrB) throws Exception {
        int iLen = arrB.length;
        // 每個byte用兩個字元才能表示,所以字串的長度是陣列長度的兩倍
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = arrB[i];
            // 把負數轉換為正數
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            // 小於0F的數需要在前面補0
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    /**
     * 將表示16進位制值的字串轉換為byte陣列, 和public static String byteArr2HexStr(byte[] arrB)
     * 互為可逆的轉換過程
     *
     * @param strIn 需要轉換的字串
     * @return 轉換後的byte陣列
     */
    public static byte[] hexStr2ByteArr(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
        // 兩個字元表示一個位元組,所以位元組陣列長度是字串長度除以2
        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }

    /**
     * DES加密
     *
     * @param strIn   需要加密字串
     * @param keyByte 金鑰
     * @return
     * @throws Exception
     */
    public static String encryptByDes(String strIn, byte[] keyByte) throws Exception {
        if (desEncryptCipher == null) {
            Key key = getKey(keyByte);
            desEncryptCipher = Cipher.getInstance("DES");
            desEncryptCipher.init(Cipher.ENCRYPT_MODE, key);
        }
        return byteArr2HexStr(desEncryptCipher.doFinal(strIn.getBytes()));
    }

    /**
     * DES解密
     *
     * @param strIn   需解密的字串
     * @param keyByte 金鑰
     * @return
     * @throws Exception
     */
    public static String decryptByDes(String strIn, byte[] keyByte) {
        try {

            if (desDecryptCipher == null) {
                Key key = null;
                key = getKey(keyByte);
                desDecryptCipher = Cipher.getInstance("DES");
                desDecryptCipher.init(Cipher.DECRYPT_MODE, key);
            }
            return new String(desDecryptCipher.doFinal(hexStr2ByteArr(strIn)));
        } catch (Exception e) {
            log.info("EncryptUtil-->decryptByDes" + "解密失敗!",e);
            return "";
        }
    }