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

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));

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值