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

    日志:

  • 相关阅读:
    PAIRING WORKFLOW MANAGER 1.0 WITH SHAREPOINT 2013
    Education resources from Microsoft
    upgrade to sql server 2012
    ULSViewer sharepoint 2013 log viewer
    Top 10 Most Valuable Microsoft SharePoint 2010 Books
    讨论 Setsockopt选项
    使用 Alchemy 技术编译 C 语言程序为 Flex 可调用的 SWC
    Nagle's algorithm
    Nagle算法 TCP_NODELAY和TCP_CORK
    Design issues Sending small data segments over TCP with Winsock
  • 原文地址:https://www.cnblogs.com/zhangqie/p/10980089.html
Copyright © 2011-2022 走看看