java des 加密 解密 密钥随机取得方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java des 加密 解密 密钥随机取得方法相关的知识,希望对你有一定的参考价值。

请大侠们详细说明,希望带上详细代码,包括先从密钥随机开始
lzyaisr00163 能否讲讲DesKey.xml怎么搞

java DES 加密 解密 生成随机密钥
举例说明:
//保存生成的key
public static void saveDesKey()
try
SecureRandom sr = new SecureRandom();
// 选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(sr);
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// 绝对路径
String fileName = "d:/DesKey.xml";
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密钥
Key key = kg.generateKey();
oos.writeObject(key);
oos.close();
catch (Exception e)
e.printStackTrace();


//获取生成的key
public static Key getKey()
Key kp = null;
try
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);
// 绝对路径
String fileName = "d:/DesKey.xml";
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (Key) oos.readObject();
oos.close();
catch (Exception e)
e.printStackTrace();

return kp;

//加密开始
public static void encrypt(String file, String dest) throws Exception
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0)
out.write(buffer, 0, r);

cis.close();
is.close();
out.close();

//解密开始
public static void decrypt(String file, String dest) throws Exception
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0)
cos.write(buffer, 0, r);

cos.close();
out.close();
is.close();


//des加密主方法
public class DES
public static void main(String[] args) throws Exception
Encrypt.saveDesKey();
System.out.println("生成key");
Encrypt.getKey();
System.out.println("获取key");
Encrypt.encrypt("d:\\\\hello.txt", "d:\\\\encrypt.txt");
System.out.println("加密");
Encrypt.decrypt("d:\\\\encrypt.txt", "d:\\\\decrypt.txt");
System.out.println("解密");

以上方法亲测可用。
参考技术A java DES 加密 解密 生成随机密钥
举例说明:
//保存生成的key
public static void saveDesKey()
try
SecureRandom sr = new SecureRandom();
// 选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(sr);
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// 绝对路径
String fileName = "d:/DesKey.xml";
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密钥
Key key = kg.generateKey();
oos.writeObject(key);
oos.close();
catch (Exception e)
e.printStackTrace();


//获取生成的key
public static Key getKey()
Key kp = null;
try
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);
// 绝对路径
String fileName = "d:/DesKey.xml";
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (Key) oos.readObject();
oos.close();
catch (Exception e)
e.printStackTrace();

return kp;

//加密开始
public static void encrypt(String file, String dest) throws Exception
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0)
out.write(buffer, 0, r);

cis.close();
is.close();
out.close();

//解密开始
public static void decrypt(String file, String dest) throws Exception
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0)
cos.write(buffer, 0, r);

cos.close();
out.close();
is.close();


//des加密主方法
public class DES
public static void main(String[] args) throws Exception
Encrypt.saveDesKey();
System.out.println("生成key");
Encrypt.getKey();
System.out.println("获取key");
Encrypt.encrypt("d:\\hello.txt", "d:\\encrypt.txt");
System.out.println("加密");
Encrypt.decrypt("d:\\encrypt.txt", "d:\\decrypt.txt");
System.out.println("解密");

以上方法亲测可用。
参考技术B package com.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
class Encrypt

public static void saveDesKey()
try
SecureRandom sr = new SecureRandom();
// 为我们选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(sr);
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// 绝对路径
String fileName = "d:/DesKey.xml";
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密钥
Key key = kg.generateKey();
oos.writeObject(key);
oos.close();
catch (Exception e)
e.printStackTrace();



public static Key getKey()
Key kp = null;
try
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);
// 绝对路径
String fileName = "d:/DesKey.xml";
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (Key) oos.readObject();
oos.close();
catch (Exception e)
e.printStackTrace();

return kp;


public static void encrypt(String file, String dest) throws Exception
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0)
out.write(buffer, 0, r);

cis.close();
is.close();
out.close();


public static void decrypt(String file, String dest) throws Exception
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0)
cos.write(buffer, 0, r);

cos.close();
out.close();
is.close();


public class DES
public static void main(String[] args) throws Exception
Encrypt.saveDesKey();
System.out.println("生成key");
Encrypt.getKey();
System.out.println("获取key");
Encrypt.encrypt("d:\\hello.txt", "d:\\encrypt.txt");
System.out.println("加密");
Encrypt.decrypt("d:\\encrypt.txt", "d:\\decrypt.txt");
System.out.println("解密");

以上是关于java des 加密 解密 密钥随机取得方法的主要内容,如果未能解决你的问题,请参考以下文章

Java加密与解密之对称加密算法

tpm

怎么用C#解密 Java写的3des加密 ~~~~密钥是48位的。

java des加密解密

JAVA加密解密DES对称加密算法

java之--加密解密算法