常用加解密工具類(MD5、SHA、DES、AES、RSA)

phpxs發表於2016-01-22

  解密工具類,實現了常用的加解密類。包括單向加密:MD5、SHA;對稱加密:DES、AES;非對稱加密:RSA

  完整程式碼見:https://git.oschina.net/bayern.com/SecureUtils.git  同時提供ant打包指令碼。

  下面展示部分關鍵程式碼

MD5 單向加密:
    /**
     * 返回MD5單向加密後的十六進位制字串
     * @param data
     * @return
     * @throws Exception
     */
    public String getEncryptForHex(byte[] data) throws Exception
    {
        byte[] digestData = encrypt(data);
        StringBuffer hex = new StringBuffer();
        for(int i = 0; i < digestData.length; i++)
        {
            int h = ((int)digestData[i]) & 0XFF;
            if(h < 16)
            {
                hex.append("0");
            }
            hex.append(Integer.toHexString(h));
        }
        
        return hex.toString();
    }
 
 
DES 對稱加密類:
    @Override
    public byte[] encrypt(byte[] data) throws Exception 
    {
        if(secretKey == null || "".equals(secretKey))
        {
            throw new Exception("scretKey need to exists");
        }
        
        SecretKey md5Key = getKey(secretKey);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, md5Key);
        return cipher.doFinal(data);
    }
 
    @Override
    public byte[] decrypt(byte[] data) throws Exception 
    {
        if(secretKey == null || "".equals(secretKey))
        {
            throw new Exception("scretKey need to exists");
        }
        
        SecretKey md5Key = getKey(secretKey);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, md5Key);
        return cipher.doFinal(data);
    }
 
 
 
RSA 非對稱加密。私鑰加密 & 私鑰解密 & 私鑰簽名
    @Override
    public byte[] encrypt(byte[] data) throws Exception 
    {
        PrivateKey rsaPrivateKey = getRSAPrivateKey();
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, rsaPrivateKey);
        return cipher.doFinal(data);
    }
 
    @Override
    public byte[] decrypt(byte[] data) throws Exception 
    {
        PrivateKey rsaPrivateKey = getRSAPrivateKey();
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
        return cipher.update(data);
    }
 
        /**
     * 使用私鑰 對資料進行簽名
     * @param data
     * @return
     * @throws Exception
     */
    public String sign(byte[] data) throws Exception
    {
        PrivateKey rsaPrivateKey = getRSAPrivateKey();
        Signature signature = Signature.getInstance(SIGN_ALGORITHM);
        signature.initSign(rsaPrivateKey);
        signature.update(data);
        return encoder(signature.sign());
    }
 
RSA 非對稱加密。公鑰加密 & 公鑰解密 & 公鑰校驗簽名
    @Override
    public byte[] encrypt(byte[] data) throws Exception
    {
        if(publicKey == null || "".equals(publicKey))
        {
            throw new Exception("publicKey is need exists");
        }
        
        PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
        return cipher.doFinal(data);
    }
 
    @Override
    public byte[] decrypt(byte[] data) throws Exception
    {
        if(publicKey == null || "".equals(publicKey))
        {
            throw new Exception("publicKey is need exists");
        }
        
        PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, rsaPublicKey);
        return cipher.doFinal(data);
    }
 
    /**
     * 使用公鑰校驗簽名
     * @param data
     * @param sign
     * @return
     * @throws Exception
     */
    public boolean verifySign(byte[] data, String sign) throws Exception
    {
        if(publicKey == null || "".equals(publicKey))
        {
            throw new Exception("publicKey is need exists");
        }
        
        PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
        Signature signature = Signature.getInstance(SIGN_ALGORITHM);
        signature.initVerify(rsaPublicKey);
        signature.update(data);
        return signature.verify(decoder(sign));
    }

相關文章