首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

java中加密和好密之二

2012-07-26 
java中加密和解密之二public static String decryptDES(String sInfo){// 定义解密算法String Algorithm

java中加密和解密之二
public static String decryptDES(String sInfo)
    {
        // 定义解密算法
        String Algorithm = "DES";
        // 生成解密key
        Key key = getKey();
        String rs = "";
        byte[] cipherByte = null;
        try
        {
            // 得到加密、解密器
            Cipher cl = Cipher.getInstance(Algorithm);
            // 用指定的密钥和模式初始化Cipher对象
            cl.init(Cipher.DECRYPT_MODE, key);
            // 对要解密的内容进行解密
            cipherByte = cl.doFinal(hex2byte(sInfo));
            rs = new String(cipherByte);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        return rs;
    }


/**
     * 十六进制字符串转化为2进制
     *
     * @param hex
     * @return
     */
    public static byte[] hex2byte(String strIn)
    {
        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;
    }

热点排行