博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java des算法_Java DES算法程序
阅读量:2532 次
发布时间:2019-05-11

本文共 4788 字,大约阅读时间需要 15 分钟。

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 KeyGenerator			SecretKey key = KeyGenerator.getInstance("DES").generateKey();			AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);			// get Cipher instance and initiate in encrypt mode			encryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");			encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);			// get Cipher instance and initiate in decrypt mode			decryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");			decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);			// method to encrypt clear text file to encrypted file			encrypt(new FileInputStream(clearTextFile), new FileOutputStream(cipherTextFile));			// method to decrypt encrypted file to clear text file			decrypt(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 encryptCipher		os = new CipherOutputStream(os, encryptCipher);		writeData(is, os);	}	private static void decrypt(InputStream is, OutputStream os) throws IOException {		// create CipherOutputStream to decrypt the data using decryptCipher		is = new CipherInputStream(is, decryptCipher);		writeData(is, os);	}	// utility method to read data from input stream and write to output stream	private static void writeData(InputStream is, OutputStream os) throws IOException {		byte[] buf = new byte[1024];		int numRead = 0;		// read and write operation		while ((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:

进一步阅读:

References: and

参考: 和

翻译自:

java des算法

转载地址:http://relzd.baihongyu.com/

你可能感兴趣的文章
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>