DES-Data Encryption Standard,即数据加密算法。是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
  DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位。
import java.security.Key;  
import java.security.SecureRandom;  
 
import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.SecretKey;  
import javax.crypto.SecretKeyFactory;  
import javax.crypto.spec.DESKeySpec;  
/** 
 * DES安全编码组件 
 *  
 * <pre> 
 * 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR) 
 * DES                  key size must be equal to 56 
 * DESede(TripleDES)    key size must be equal to 112 or 168 
 * AES                  key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available 
 * Blowfish             key size must be multiple of 8, and can only range from 32 to 448 (inclusive) 
 * RC2                  key size must be between 40 and 1024 bits 
 * RC4(ARCFOUR)         key size must be between 40 and 1024 bits 
 * 具体内容 需要关注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html 
 * </pre> 
 *  
 * @author 梁栋 
 * @version 1.0 
 * @since 1.0 
 */

public class DESTest  extends Coder {  
    /** 
     * ALGORITHM 算法 <br> 
     * 可替换为以下任意一种算法,同时key值的size相应改变。 
     *  
     * <pre> 
     * DES                  key size must be equal to 56 
     * DESede(TripleDES)    key size must be equal to 112 or 168 
     * AES                  key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available 
     * Blowfish             key size must be multiple of 8, and can only range from 32 to 448 (inclusive) 
     * RC2                  key size must be between 40 and 1024 bits 
     * RC4(ARCFOUR)         key size must be between 40 and 1024 bits 
     * </pre> 
     *  
     * 在Key toKey(byte[] key)方法中使用下述代码 
     * <code>SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);</code> 替换 
     * <code> 
     * DESKeySpec dks = new DESKeySpec(key); 
     * SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); 
     * SecretKey secretKey = keyFactory.generateSecret(dks); 
     * </code> 
     */ 
    public static final String ALGORITHM = "DES";  
 
    /** 
     * 转换密钥<br> 
     *  
     * @param key 
     * @return 
     * @throws Exception 
     */ 
    private static Key toKey(byte[] key) throws Exception {  
        DESKeySpec dks = new DESKeySpec(key);  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);  
        SecretKey secretKey = keyFactory.generateSecret(dks);  
 
        // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码  
        // SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);  
        return secretKey;  
    }  
 
    /** 
     * 解密 
     *  
     * @param data 
     * @param key 
     * @return 
     * @throws Exception 
     */ 
    public static byte[] decrypt(byte[] data, String key) throws Exception {  
        Key k = toKey(decryptBASE64(key));  
 
        Cipher cipher = Cipher.getInstance(ALGORITHM);  
        cipher.init(Cipher.DECRYPT_MODE, k);  
 
        return cipher.doFinal(data);  
    }  
 
    /** 
     * 加密 
     *  
     * @param data 
     * @param key 
     * @return 
     * @throws Exception 
     */ 
    public static byte[] encrypt(byte[] data, String key) throws Exception {  
        Key k = toKey(decryptBASE64(key));  
        Cipher cipher = Cipher.getInstance(ALGORITHM);  
        cipher.init(Cipher.ENCRYPT_MODE, k);  
 
        return cipher.doFinal(data);  
    }  
 
    /** 
     * 生成密钥 
     *  
     * @return 
     * @throws Exception 
     */ 
    public static String initKey() throws Exception {  
        return initKey(null);  
    }  
 
    /** 
     * 生成密钥 
     *  
     * @param seed 
     * @return 
     * @throws Exception 
     */ 
    public static String initKey(String seed) throws Exception {  
        SecureRandom secureRandom = null;  
 
        if (seed != null) {  
            secureRandom = new SecureRandom(decryptBASE64(seed));  
        } else {  
            secureRandom = new SecureRandom();  
        }  
 
        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);  
        kg.init(secureRandom);  
 
        SecretKey secretKey = kg.generateKey();  
 
        return encryptBASE64(secretKey.getEncoded());  
    }

/**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

}

}

测试:

import static org.junit.Assert.*;  
import org.junit.Test;  
public class DESCoderTest2 {
 @Test 
    public void test() throws Exception {  
        String inputStr = "DES";  
        String key = DESTest.initKey();  
        System.err.println("原文:\t" + inputStr);  
 
        System.err.println("密钥:\t" + key);  
 
        byte[] inputData = inputStr.getBytes();  
        inputData = DESTest.encrypt(inputData, key);  
 
        System.err.println("加密后:\t" + DESTest.encryptBASE64(inputData));  
 
        byte[] outputData = DESTest.decrypt(inputData, key);  
        String outputStr = new String(outputData);  
 
        System.err.println("解密后:\t" + outputStr);  
        assertEquals(inputStr, outputStr);  
    }

}

按着我理解的意思来进行再次分析:

import static org.junit.Assert.*;  
import org.junit.Test;  
public class DESCoderTest {  
 
    @Test 
    public void test() throws Exception {  
        String inputStr = "DES";  
        String key = DESTest.initKey();  
        System.err.println("原文:\t" + inputStr);  
 
        System.err.println("密钥:\t" + key);  
 
        byte[] inputData = inputStr.getBytes();  
        inputData = DESTest.encrypt(inputData, key);  
 
        System.err.println("des加密后:\t" + new String(inputData));
        //进行加密
        String encryptstr = DESTest.encryptBASE64(inputData);
        System.err.println("base加密后:\t" + encryptstr);  
      
        //解密:
        byte []b = DESTest.decryptBASE64(encryptstr);
        System.err.println("base解密后:\t" + new String(b));
        byte[] outputData = DESTest.decrypt(inputData, key);  
        String outputStr = new String(outputData);  
 
        System.err.println("des解密后:\t" + outputStr);  
 
        assertEquals(inputStr, outputStr);  
    }  
}

转载于:https://www.cnblogs.com/maxblog/archive/2010/12/17/1909533.html

java加密 des相关推荐

  1. java php des加密 byte数组16进制 DESTools

    大家好,我是烤鸭: 今天分享的是java 和 php des 加密. 因为接口对接,难免不同语言,加密又是必不可少的. 作为接口的提供方,必须把加密规则写好,最好有不同语言的加密demo. 1.    ...

  2. java使用DES加密方式,实现对数据的加密解密

    全栈工程师开发手册 (作者:栾鹏) java教程全解 java使用DES加密方式,实现对数据的加密解密.加密和解密中需要使用同一个秘钥 第一种方式,使用类型Key作为加密解密的共同秘钥. 测试代码 p ...

  3. php与java的des加密解密

    与第三方接口对接des加密.解密,第三方提供java的des加密解密demo,特记录PHP与java加密解密. import javax.crypto.*; import javax.crypto.s ...

  4. vue使用DES模式加密解密,包括Java加密解密

    前端加密解密 1.安装:npm install crypto-js 2.在utils下创建一个cryptoAES.js import CryptoJS from 'crypto-js';//与后端约定 ...

  5. Java实现DES加密解密(对称式)

    原文:https://blog.csdn.net/gs12software/article/details/83899389 对称式加密:就是加密和解密都是使用同一个密钥,如DES加密 非对称式加密: ...

  6. 一个java的DES加解密类转换成C#

    原文:一个java的DES加解密类转换成C# 一个java的des加密解密代码如下: //package com.visionsky.util;import java.security.*; //im ...

  7. Java 加密解密 对称加密算法 非对称加密算法 MD5 BASE64 AES RSA

    [最简单的加密] 1.简单的概念 明文:加密前的信息 密文:机密后的信息 算法:加密或解密的算法 密钥:算法使用的钥匙(读作miyao,正确应该是miyue,但是大家都读miyao) 2.简单的例子 ...

  8. Java加密与解密笔记(三) 非对称加密

    非对称的特点是加密和解密时使用的是不同的钥匙.密钥分为公钥和私钥,用公钥加密的数据只能用私钥进行解密,反之亦然. 另外,密钥还可以用于数字签名.数字签名跟上文说的消息摘要是一个道理,通过一定方法对数据 ...

  9. Java 加密 AES 对称加密算法

    版权声明:本文为博主原创文章,未经博主允许不得转载. [AES] 一种对称加密算法,DES的取代者. 加密相关文章见:Java 加密解密 对称加密算法 非对称加密算法 MD5 BASE64 AES R ...

最新文章

  1. python 内置函数
  2. 一文弄懂SSD目标检测算法
  3. PAT甲级题目翻译+答案 AcWing(模拟)
  4. SA 分析专家认证名单出炉!这一期学员太牛了
  5. TypeError at / 'AnonymousUser' object is not iterable
  6. Happy Birthday to You
  7. Ubuntu五笔输入法的安装过程
  8. 贪吃蛇c语言代码 链表,链表贪吃蛇 附代码
  9. JDK1.8优雅的集合排序(集合的排序)
  10. SharePoint 模拟审批Nintex工作流
  11. 国外10个优秀音乐网站推荐
  12. 你理解的精益可能是错的!——从源头重新解读精益
  13. 服务器系统才有卷影副本吗,windows server 2019没有适合具有卷影副本的卷
  14. git branch 相关命令
  15. Yahoo!创始人:杨致远的故事
  16. 编译原理 实验2 语法分析器的构造
  17. 2019.01.12 Presto中国区用户线下Meetup
  18. 解决vue.esm.js?efeb:628 [Vue warn]: Failed to mount component: template or render function not defined
  19. 隐私保护与隐私计算研讨会 | 余维仁:大数据时代下需要各界更新对个人隐私保护的固有认识
  20. python爬取b站搜索结果_Python爬虫实例:爬取猫眼电影——破解字体反爬,Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取,Python爬虫实例:爬取豆瓣Top250...

热门文章

  1. mysql timestamp 转型_MySQL的timestamp类型自动更新问题【转】
  2. lua 访问oracle,lua链接oracle解决方法与步骤
  3. php css下划线,如何自定义下划线的样式
  4. json数据解析_VBA 实践指南 -- Split函数解析Json数据
  5. android studio离线配置gradle插件_Gradle 扫盲与 Task 基础
  6. python使用循环嵌套显示数字金字塔_如何使用Python生成数字金字塔?
  7. js中如何得到循环中的点击的这个id_Js篇面试题9请说一下Js中的事件循环机制
  8. php多维数组和对象,在PHP中将多维多对象数组转换为标准多维数组
  9. 数据合并设计_八秒搞定合并相同内容的单元格,你却加班了1小时
  10. python字符串出栈方法_python字符串常用方法