📄 desencrypt.java
字号:
package des;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.swing.JOptionPane;
/**
*
* 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
*
* 方法: void getKey(String strKey)从strKey的字条生成一个Key
*
* String getEncString(String strMing)对strMing进行加密,返回String密文 String
* getDesString(String strMi)对strMin进行解密,返回String明文
*
* byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
* byteD)byte[]型的解密
*/
public class DesEncrypt {
Key key;
/**
* 根据参数生成KEY
*
* @param strKey
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 加密String明文输入,String密文输出
*
* @param strMing
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
return byte2hex(getEncCode(strMing.getBytes()));
// byteMing = strMing.getBytes("UTF8");
// byteMi = this.getEncCode(byteMing);
// strMi = new String( byteMi,"UTF8");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
*
* @param strMi
* @return
*/
public String getDesString(String strMi) {
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
return new String(getDesCode(hex2byte(strMi.getBytes())));
// byteMing = this.getDesCode(byteMi);
// strMing = new String(byteMing,"UTF8");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD)
{
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
cipher = null;
}
return byteFina;
}
/**
* 二行制转字符串
*
* @param b
* @return
*/
public static String byte2hex(byte[] b)
{
// 一个字节的数,
// 转成16进制字符串
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
// 整数转成十六进制表示
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase(); // 转成大写
}
public static byte[] hex2byte(byte[] b)
{
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2)
{
String item = new String(b, n, 2);
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
public static void main(String[] args)
{
System.out.println("hello");
DesEncrypt des = new DesEncrypt(); //实例化一个对像
des.getKey("a"); //生成密匙
String strEnc,strDes ;
/*
String strEnc = des.getEncString("abcdabcd"); //加密字符串,返回String的密文
System.out.println(strEnc);
String strDes = des.getDesString(strEnc); //把String 类型的密文解密
System.out.println(strDes);
*/
//new DesEncrypt();
FormattedInput in = new FormattedInput();
int choice = 0;
try
{
File inFile ;//= new File("dataToDec.dat");//dataToDec.dat//dataToEnc.dat
FileInputStream fis ;//= new FileInputStream(inFile);
BufferedReader d ;//= new BufferedReader (new InputStreamReader(fis));
String temp = "\0";
File outFile ;//= new File("dataDecrypted.dat");//dataDecrypted.dat//dataEncrypted.dat
FileOutputStream fos ;//= new FileOutputStream(outFile);
DataOutputStream dos ;//= new DataOutputStream (fos);
for(;;){
JOptionPane.showMessageDialog(null,"Enter 1 to encrypt\n"+
"Enter 2 to decrypt\n"+"Enter 9 to quit","Guide",JOptionPane.INFORMATION_MESSAGE);
try {
choice = in.readInt("Enter a number");
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e.getMessage()+"\nTry again",
"Enter your choice",JOptionPane.ERROR_MESSAGE);
continue;
}
switch(choice){
case 1:
inFile = new File("dataToEnc.dat");
fis = new FileInputStream(inFile);
d = new BufferedReader (new InputStreamReader(fis));
outFile = new File("dataEncrypted.dat");
fos = new FileOutputStream(outFile);
dos = new DataOutputStream (fos);
while(!temp.equals(null))
{
temp = d.readLine();
if(!temp.equals(null))
{
strEnc = des.getEncString(temp);
dos.writeBytes(strEnc+"\r\n");
}
}
fis.close();
break;
case 2:
inFile = new File("dataToDec.dat");
fis = new FileInputStream(inFile);
d = new BufferedReader (new InputStreamReader(fis));
outFile = new File("dataDecrypted.dat");
fos = new FileOutputStream(outFile);
dos = new DataOutputStream (fos);
while(!temp.equals(null))
{
temp = d.readLine();
if(!temp.equals(null))
{
strDes = des.getDesString(temp); //把String 类型的密文解密
dos.writeBytes(strDes+"\r\n");
}
}
fis.close();
break;
case 9:
JOptionPane.showMessageDialog(null,"Thank you for your use" ,
"Ending program",JOptionPane.INFORMATION_MESSAGE);
return;
default:
JOptionPane.showMessageDialog(null,"Invalid selection" ,
"Error",JOptionPane.ERROR_MESSAGE);
break;
}
}
}
catch (NullPointerException e)
{
if(choice==1)
JOptionPane.showMessageDialog(null,"The data has been encrypted." ,
"Ending program",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null,"The data has been decrypted." ,
"Ending program",JOptionPane.INFORMATION_MESSAGE);
}
catch (EOFException e)
{
System.out.println("Empty File.");
}
catch (FileNotFoundException e)
{
System.out.println("FileStreamsTest: " + e);
}
catch (IOException e)
{
System.err.println("FileStreamsTest: " + e);
}
}
}
//a class for a standard input I define for users including checking the validity of data input
//by users
class FormattedInput{
private final int MaxStudentNum = 100000;
public String readString(String str){
String strInput;
strInput = JOptionPane.showInputDialog("Please enter "+str);
return strInput;
}
public double readDouble(String str){
String strInput;
double input = 0;
int maxTries = 5;
for(;;){
try{
strInput = JOptionPane.showInputDialog("Please enter "+str);
input = Double.parseDouble(strInput);
if(input>100000||input<0){
if(--maxTries == 0){
JOptionPane.showMessageDialog(null,"ERROR INPUT.Terminating...",
"Enter the number",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
JOptionPane.showMessageDialog(null,"Out of Range.Try again.",
"Enter the number(Range from 0 to MaxStudentNum)",
JOptionPane.ERROR_MESSAGE);
continue;
}
return input;
}
catch(Exception e){
if(--maxTries == 0){
JOptionPane.showMessageDialog(null,"ERROR INPUT.Terminating...",
"Enter the number",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
JOptionPane.showMessageDialog(null,e.getMessage()+"\nTry again.",
"Enter the number",JOptionPane.ERROR_MESSAGE);
continue;
}
}
}
public int readInt(String str){
String strInput;
int input=0;
int maxTries = 5;
for(;;){
try{
strInput = JOptionPane.showInputDialog("Please enter "+str);
input = Integer.parseInt(strInput);
if(input>100000||input<0){
if(--maxTries == 0){
JOptionPane.showMessageDialog(null,"ERROR INPUT.Terminating...",
"Enter the number",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
JOptionPane.showMessageDialog(null,"Out of Range.Try again.",
"Enter the number(Range from 0 to MaxStudentNum)",
JOptionPane.ERROR_MESSAGE);
continue;
}
return input;
}
catch(Exception e){
if(--maxTries == 0){
JOptionPane.showMessageDialog(null,"ERROR INPUT.Terminating...",
"Enter the number",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
JOptionPane.showMessageDialog(null,e.getMessage()+"\nTry again.",
"Enter the number",JOptionPane.ERROR_MESSAGE);
continue;
}
}
}
private int readToken(){
try{
ttype = tokenizer.nextToken();
return ttype;
}
catch(IOException e){
e.printStackTrace(System.err);
System.exit(1);
}
return 0;
}
private StreamTokenizer tokenizer = new StreamTokenizer (new BufferedReader(new
InputStreamReader(System.in)));
private int ttype;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -