zoukankan      html  css  js  c++  java
  • android -------- DES加密解密算法

    DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),并授权在非密级政府通信中使用,随后该算法在国际上广泛流传开来。需要注意的是,在某些文献中,作为算法的DES称为数据加密算法(Data Encryption Algorithm,DEA),已与作为标准的DES区分开来。

    DES加密介绍
    DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。DES加密算法出自IBM的研究,
    后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少,因为DES使用56位密钥,以现代计算能力,
    24小时内即可被破解。虽然如此,在某些简单应用中,我们还是可以使用DES加密算法,本文简单讲解DES的JAVA实现


    注意:DES加密和解密过程中,密钥长度都必须是8的倍数

    网上写法也有很多种,我只是随便弄一种

    /**
         * 使用DES对字符串加密
         *
         * @param str
         *            utf8编码的字符串
         * @param key
         *            密钥(56位,7字节)
         *
         */
        public static byte[] desEncrypt(String str, String key) throws Exception {
            if (str == null || key == null)
                return null;
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "DES"));
            byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
            return bytes;
        }
    
        /**
         * 使用DES对数据解密
         *
         * @param bytes
         *            utf8编码的二进制数据
         * @param key
         *            密钥(16字节)
         * @return 解密结果
         * @throws Exception
         */
        public static String desDecrypt(byte[] bytes, String key) throws Exception {
            if (bytes == null || key == null)
                return null;
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "DES"));
            bytes = cipher.doFinal(bytes);
            return new String(bytes, "utf-8");
        }
    
        /**
         * 使用base64解决乱码
         *
         * @param secretKey
         *            加密后的字节码
         */
        public static String jdkBase64String(byte[] secretKey) {
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(secretKey);
        }
    
        /**
         * 使用jdk的base64 解密字符串 返回为null表示解密失败
         *
         * @throws IOException
         */
        public static byte[] jdkBase64Decoder(String str) throws IOException {
            BASE64Decoder decoder = new BASE64Decoder();
            return decoder.decodeBuffer(str);
        }

    使用Base64 避免加密乱码

     //测试
        public static void main(String args[]) {
    try {
                String openId = jdkBase64String(desEncrypt("Hello 小笨蛋", "12345678"));
                System.out.println(openId);
                String desDecrypt = desDecrypt(jdkBase64Decoder(openId), "12345678");
                System.out.println(desDecrypt);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }

    日志:

  • 相关阅读:
    Sharepoint 2013默认dll部署位置
    Sharepoint 2010 Form认证自定义登录页面,总是自动登录问题
    cocos打包后页面在ios浏览器、安卓钉钉等部分app中横竖屏问题
    cocos构建出来的 Web Mobile 项目在微信开发者工具里面无法点击
    git 合并某个提交commit到指定的分支上
    cc.sys.localStorage存储和读取用户数据
    cocos creater关于blend,关于预乘premultiply alpha,关于图片白边灰边的问题
    cocos遮罩层点击穿透问题解决
    postman中 form-data、x-www-form-urlencoded、raw、binary操作
    ‘webpack-dev-server' 不是内部或外部命令,也不是可运行的程序
  • 原文地址:https://www.cnblogs.com/zhangqie/p/10980089.html
Copyright © 2011-2022 走看看