欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

算法学习

程序员文章站 2022-07-14 21:06:47
...
//可传入key值的加密算法
public String encrypt(String src, String key)
        throws Exception
    {
        if (null == key || null == src)
        {
            return null;
        }
       
        byte[] raw = key.getBytes("ASCII");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(src.getBytes());
       
        return TypeConvertUtil.byteTS(encrypted).toLowerCase();
       
    }
//对应解密算法
public  String decrypt(String src, String key)
        throws Exception
    {
        try
        {
            if (null == key || null == src)
            {
                return null;
            }
            byte[] raw = key.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = TypeConvertUtil.sTByte(sSrc);
            try
            {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
   
相关标签: Java