博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA_DES 加密 解密 生成随机密钥
阅读量:5138 次
发布时间:2019-06-13

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

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

  

转载于:https://www.cnblogs.com/gisblogs/p/5082344.html

你可能感兴趣的文章
ad logon hour
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
距离公式汇总以及Python实现
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>
Sql Server 中由数字转换为指定长度的字符串
查看>>
tmux的简单快捷键
查看>>
[Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
查看>>
php match_model的简单使用
查看>>
Vue_(组件通讯)子组件向父组件传值
查看>>
STM32单片机使用注意事项
查看>>
移动开发平台-应用之星app制作教程
查看>>
springboot No Identifier specified for entity的解决办法
查看>>
51nod 1428 活动安排问题 (贪心+优先队列)
查看>>
如何在maven工程中加载oracle驱动
查看>>
一句话说清分布式锁,进程锁,线程锁
查看>>