系统中加密算法EncryptUtil02

public static String encrypt(String inputText, String algorithmName)
    {
        if (inputText == null)
        {
            throw new IllegalArgumentException(
                    "The input parameter cannot be null.");
        }
        if (algorithmName == null || "".equals(algorithmName.trim()))
        {
            algorithmName = "md5";
        }
        String encryptText = null;
        try
        {
            MessageDigest m = MessageDigest.getInstance(algorithmName);
            m.update(inputText.getBytes("UTF8"));
            byte s[] = m.digest();
            // m.digest(inputText.getBytes("UTF8"));
            return byte2hex(s);
        }
        catch (NoSuchAlgorithmException e)
        {
            logger.error(e.getMessage(), e);
        }
        catch (UnsupportedEncodingException e)
        {
            logger.error(e.getMessage(), e);
        }
        return encryptText;
    }

  
    public static String encryptDES(String info)
    {
        // 定义加密算法,可用 DES,DESede,Blowfish
        String Algorithm = "DES";

        // 生成加密key
        Key key = getKey();

        // 定义要生成的密文
        String rs = "";
        byte[] cipherByte = null;
        try
        {
            // 得到加密/解密器
            Cipher c1 = Cipher.getInstance(Algorithm);

            // 用指定的密钥和模式初始化Cipher对象
            // 参数:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
            c1.init(Cipher.ENCRYPT_MODE, key);

            // 对要加密的内容进行编码处理,
            cipherByte = c1.doFinal(info.getBytes());
            rs = byte2hex(cipherByte);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }

        // 返回密文的十六进制形式
        return rs;
    }

 
    public static String decryptDES(String sInfo)
    {
        // 定义加密算法,
        String Algorithm = "DES";

        // 生成解密key
        Key key = getKey();
        String rs = "";
        byte[] cipherByte = null;
        try
        {
            // 得到加密/解密器
            Cipher c1 = Cipher.getInstance(Algorithm);

            // 用指定的密钥和模式初始化Cipher对象
            c1.init(Cipher.DECRYPT_MODE, key);

            // 对要解密的内容进行编码处理
            cipherByte = c1.doFinal(hex2byte(sInfo));
            rs = new String(cipherByte);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        // return byte2hex(cipherByte);
        return rs;
    }

猜你喜欢

转载自xieguowei123485.iteye.com/blog/1431976