java实现的des加密

DES算法为密码体制中的对称密码体制,又被成为美国数据加密标准 ,是1972年美国IBM 公司研制的对称密码体制加密算法,其密钥长度为56位,明文按64位进行分组,将分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法,它是早起最为广泛使用的对称加密算法之一。虽然56位密钥的DES算法已经风光不在,而且常有用Des加密的明文被破译的报道,但是了解一下昔日美国的标准加密算法总是有益的,而且目前DES算法得到了广泛的应用,在某些场合,仍然发挥着余热。
DES加密算法已发现的缺点:分组比较短、密钥太短、密码生命周期短、运算速度较慢。
DES工作的基本原理是,其入口参数有三个:key、data、mode。 
key为加密解密使用的密钥 ,data为加密解密的数据,mode为其工作模式(当模式为加密模式时,明文按照64)




按位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密。实际运用中,密钥只用到了64位中的56位,这样才具有高的安全性。


加密代码如下:


package com.demo.aes;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;


import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;


import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;


public class Main {


public Main(){

}

public Main(String str){
setKey(key);
}
private Key key;

public Key getKey() {
return key;
}


public void setKey(Key key) {
this.key = key;
}


/**
* 根据参数生成key
* @param key
*/
public void setKey(String key) {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(new SecureRandom(key.getBytes()));
this.key = keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

/**
* 加密
* @param strMing 秘文输入
* @return string 明文输出
*/
public String encrypStr(String strMing) {

byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";

BASE64Encoder base64Encoder = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF-8");
byteMi = encrypByte(byteMing);
strMi = base64Encoder.encoder(byteMi);
} catch (Exception e) {
e.printStackTrace();
}

return strMi;
  }

/**
* 解密
* @param strMing 秘文输入
* @return string 明文输入
*/
public String decrypStr(String strMi) {

byte[] byteMi = null;
byte[] byteMing = null;
String strMing = "";

BASE64Decoder base64Decoder = new BASE64Decoder();
try {
byteMi = base64Decoder.decodeBuffer(strMi);
byteMing = decrypByte(byteMi);
strMing = new String(byteMing,"UTF-8");
} catch (Exception e) {
e.printStackTrace();
}

return strMing;
  }

/**
* 加密
* @param byteMing 明文输入
* @return byte[]  密文输出
*/
private byte[] encrypByte(byte[] byteMing){
byte[] byteMi = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
byteMi = cipher.doFinal(byteMing);
} catch (Exception e) {
e.printStackTrace();
}

return byteMi;
}

/**
* 解密

* @param byteMi 密文输入
* @return byte[] 明文输出
*/
private byte[] decrypByte(byte[] byteMi){
byte[] byteMing = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("MD5");
cipher.init(Cipher.DECRYPT_MODE, key);
byteMing = cipher.doFinal(byteMi);
} catch (Exception e) {
e.printStackTrace();
}

return byteMing;
}

/***
* 加密文件

* @param originalFilePath 要加密的文件路径
* @param targetFilePath 保存加密的文件路径
*/
public void encryFile(String originalFilePath,String targetFilePath) {

try {
Cipher cipher = Cipher.getInstance("MD5");
cipher.init(Cipher.ENCRYPT_MODE, key);
FileInputStream is = new FileInputStream(originalFilePath);
FileOutputStream out = new FileOutputStream(targetFilePath);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buf = new byte[1024];
int len = 0;
while ((len = cis.read(buf)) != -1) {
out.write(buf);
}
cis.close();
out.close();
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();


}

/***
* 解密文件

* @param originalFilePath 要解密的文件路径
* @param targetFilePath 保存加密的文件路径
*/
public void decryFile(String originalFilePath,String targetFilePath) {

try {
Cipher cipher = Cipher.getInstance("MD5");
cipher.init(Cipher.DECRYPT_MODE, key);
FileInputStream is = new FileInputStream(originalFilePath);
FileOutputStream out = new FileOutputStream(targetFilePath);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buf = new byte[1024];
int len = 0;
while ((len = cis.read(buf)) != -1) {
out.write(buf);
}
cis.close();
out.close();
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();


}

public static void main(String[] args) {
// TODO Auto-generated method stub


Main des = new Main( "1234567" );  
      // DES 加密文件  
      // des.encryptFile("G:/test.doc", "G:/ 加密 test.doc");  
      // DES 解密文件  
      // des.decryptFile("G:/ 加密 test.doc", "G:/ 解密 test.doc");  
      String str1 = " 要加密的字符串 test" ;  
      // DES 加密字符串  
      String str2 = des.encryptStr(str1);  
      // DES 解密字符串  
      String deStr = des.decryptStr(str2);  
      System. out .println( " 加密前: " + str1);  
      System. out .println( " 加密后: " + str2);  
      System. out .println( " 解密后: " + deStr);  
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值