java des算法

Java Cryptography Extension (JCE) provides framework and implementation for generating key and encryption/decryption of data using various algorithms. In this tutorial, we will use Java DES implementation to encrypt and decrypt a file.

Java密码学扩展JCE )提供了用于使用各种算法生成密钥以及对数据进行加密/解密的框架和实现。 在本教程中,我们将使用Java DES实现来加密和解密文件。

DES is a block cipher algorithm in which we will have to use same key for encryption and decryption. It is one of the basic cypher technique. It’s not safe because we need to give client application secure key to decrypt data.

DES是一种分组密码算法,在该算法中,我们将必须使用相同的密钥进行加密和解密。 它是基本的密码技术之一。 这是不安全的,因为我们需要为客户端应用程序提供安全密钥以解密数据。

Java DES加密解密步骤 (Java DES Encryption Decryption Steps)

  • First of all we need to get the KeyGenerator instance using DES algorithm.首先,我们需要使用DES算法获取KeyGenerator实例。
  • Generate SecureKey (key) that will be used for encryption and decryption.生成将用于加密和解密的SecureKey (密钥)。
  • Get Cipher instance using DES algorithm, one for encrypt mode and another for decrypt mode. Initialize the cypher object using key and IvParameterSpec object.使用DES算法获取Cipher实例,一种用于加密模式,另一种用于解密模式。 使用key和IvParameterSpec对象初始化IvParameterSpec对象。
  • For encryption, create object of CipherOutputStream using encrypt cipher. For decryption, create object of CipherInputStream using decrypt cipher.为了加密, CipherOutputStream使用加密密码创建CipherOutputStream对象。 对于解密, CipherInputStream使用解密密码创建CipherInputStream对象。
  • Read the input stream and write to the output stream.读取输入流并写入输出流。

Below example first encrypt the file and save encrypted data to new file. Then it decrypts the same file to create the plain text file.

下面的示例首先加密文件,然后将加密的数据保存到新文件。 然后,它解密相同的文件以创建纯文本文件。

package com.journaldev.des;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;public class DESEncryptionExample {private static Cipher encryptCipher;private static Cipher decryptCipher;private static final byte[] iv = { 11, 22, 33, 44, 99, 88, 77, 66 };public static void main(String[] args) {String clearTextFile = "/Users/pankaj/source.txt";String cipherTextFile = "/Users/pankaj/cipher.txt";String clearTextNewFile = "/Users/pankaj/source-new.txt";try {// create SecretKey using KeyGeneratorSecretKey key = KeyGenerator.getInstance("DES").generateKey();AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);// get Cipher instance and initiate in encrypt modeencryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);// get Cipher instance and initiate in decrypt modedecryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);// method to encrypt clear text file to encrypted fileencrypt(new FileInputStream(clearTextFile), new FileOutputStream(cipherTextFile));// method to decrypt encrypted file to clear text filedecrypt(new FileInputStream(cipherTextFile), new FileOutputStream(clearTextNewFile));System.out.println("DONE");} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException| InvalidAlgorithmParameterException | IOException e) {e.printStackTrace();}}private static void encrypt(InputStream is, OutputStream os) throws IOException {// create CipherOutputStream to encrypt the data using encryptCipheros = new CipherOutputStream(os, encryptCipher);writeData(is, os);}private static void decrypt(InputStream is, OutputStream os) throws IOException {// create CipherOutputStream to decrypt the data using decryptCipheris = new CipherInputStream(is, decryptCipher);writeData(is, os);}// utility method to read data from input stream and write to output streamprivate static void writeData(InputStream is, OutputStream os) throws IOException {byte[] buf = new byte[1024];int numRead = 0;// read and write operationwhile ((numRead = is.read(buf)) >= 0) {os.write(buf, 0, numRead);}os.close();is.close();}}

Once program terminates, you can check that both the plain text file have same data and the encrypted file don’t have plain text data. Below is the files content from my sample files, with cipher text highlighted.

程序终止后,您可以检查纯文本文件是否具有相同的数据,以及加密文件是否没有纯文本数据。 以下是示例文件中的文件内容,突出显示了密文。

Further Reading: Generate CSR Java Program

进一步阅读: 生成CSR Java程序

References: Wikipedia and Java Cryptography Architecture

参考: Wikipedia和Java密码体系结构

翻译自: https://www.journaldev.com/1309/java-des-algorithm-program

java des算法

java des算法_Java DES算法程序相关推荐

  1. java 抽奖算法_Java抽奖算法第二例

    本文实例为大家分享了java抽奖算法,供大家参考,具体内容如下 1. 算法分析 根据概率将奖品划分区间,每个区间代表一个奖品,然后抽取随机数,反查落在那个区间上,即为所抽取的奖品. 2. 代码核心算法 ...

  2. java质因数算法_Java分解任意输入数的质因数算法的实现示例

    这篇文章主要介绍了Java实现分解任意输入数的质因数算法,涉及java数学运算相关操作技巧,需要的朋友可以参考下 本文实例讲述了Java实现分解任意输入数的质因数算法.分享给大家供大家参考,具体如下: ...

  3. java常用算法_JAVA编程常用算法——冒泡排序

    一.冒泡排序算法运作的过程 (1)比较相邻的元素.如果第一个比第二个大,就交换他们两个. (2)对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数. (3 ...

  4. java 随机数生成算法_java 语言实现的随机数生成算法

    ----------------------疯狂软件java培训分享--------------------- 广州疯狂软件学院拥有三大课程体系包括:java课程,android课程,ios课程,疯狂 ...

  5. java 数独算法_java版数独游戏核心算法(一)

    之前学习javascript时用javascript写过一个数独游戏,最近看了一点java的内容,于是就心血来潮想搞一个java版的数独游戏. 现在将全部代码分享出来和大家学习交流,当然代码中有着各种 ...

  6. java 日历算法_Java实现 蓝桥杯VIP 算法提高 任意年月日历输出

    算法提高 任意年月日历输出 时间限制:1.0s 内存限制:512.0MB 已知2007年1月1日为星期一. 设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印. 为 ...

  7. java 随机数生成算法_Java随机数的生成算法

    Java中随机数的生成算法主要有3种 1.Math.random()//产生一个0-1之间的随机数,类型为double类型 2.new Random() random.nextInextInt(100 ...

  8. java回溯算法_java实现回溯算法

    最近有在leetcode上面做算法题,已经遇到了两道回溯算法的题目,感觉一点思路都没有,现决定将java如何实现回溯算法做一次总结. 什么叫做回溯算法 (摘抄于百度百科) 回溯算法实际上一个类似枚举的 ...

  9. java笛卡尔积算法_Java 笛卡尔积算法的简单实现

    笛卡尔积算法的Java实现: (1)循环内,每次只有一列向下移一个单元格,就是CounterIndex指向的那列. (2)如果该列到尾部了,则这列index重置为0,而CounterIndex则指向前 ...

最新文章

  1. 实现iOS图片等资源文件的热更新化(二):自定义的动态 imageNamed
  2. Android 再谈handler
  3. jQuery的Password Validation插件
  4. UTF8有BOM和无BOM
  5. 完全采用CSS的CROSS BROWSER TABBED PAGES
  6. ROS机器人程序设计(原书第2版)2.4.1 ROS文件系统导览
  7. 48小时备考TOGAF经验分享
  8. Geserver SLD 线标注注意事项
  9. Android 显示文字超过指定长度部分使用省略号表示
  10. 阿里云cdn以及阿里云负载均衡配置方法
  11. 用米思齐mixly和APP INVENTOR 2通过MQTT控制灯亮和熄
  12. 昨日种种,昨日死.今日种种,今日生.--看了几篇影评有感
  13. 7080mt安装linux网卡驱动,Intel英特尔PRO100/1000/10GbE系列网卡驱动
  14. 有趣的兔子(斐波那契数列)
  15. 2.Excel vba开发-从选择中突出显示重复项
  16. 脱壳2 (15PB pack)
  17. 浅谈jdk-spi与dubbo-spi
  18. 89.77%准确率!谷歌大脑提出CoAtNet:结合卷积和注意力
  19. java字节字符_java按字节截取带有汉字的字符串的解法(推荐)
  20. 智能电动渗透率突破50%!BBA「反攻」与新势力「下沉」

热门文章

  1. 设置窗体的可见性无效
  2. nhibernate源码分析之六: Criteria数据加载
  3. [转载] python 调用自己的方法报错,numpy.ndarray object has no attribute brighten
  4. 超哥笔记 --nginx入门(6)
  5. BZOJ3244 [Noi2013]树的计数 【数学期望 + 树遍历】
  6. item 12: 把重写函数声明为“override”的
  7. 如何让你的JavaScript代码更加语义化
  8. .NET异步编程总结----四种实现模式
  9. No New-Net
  10. pyqt的listwidget 支持键盘搜索_键盘测评丨Ceke M87机械键盘:更好的双模MAC系统支持?...