java des例子_java DES 例子

/*** @Title Encryption.java ,By yangtao at 2011-3-4上午10:37:55

*@version1.0

*

* @Description this is you mark

* @Company Copyright (c) 2010 AOSA TECH, Ltd. All right reserved

* @Project SafeMedia

**/packageaosa.safemedia.util;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.io.OutputStreamWriter;importjava.io.UnsupportedEncodingException;importjava.security.InvalidKeyException;importjava.security.Key;importjava.security.NoSuchAlgorithmException;importjavax.crypto.BadPaddingException;importjavax.crypto.Cipher;importjavax.crypto.IllegalBlockSizeException;importjavax.crypto.KeyGenerator;importjavax.crypto.NoSuchPaddingException;importjavax.crypto.SecretKey;importcom.sun.org.apache.xerces.internal.impl.dv.util.Base64;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;/*** @Title Encryption

*@authoryangtao at 2011-3-4上午10:38:00

* @Description 用于对字符串加密使用。*/publicclassEncryption {/*** @Description 加密的时候要使用的密钥,这个方法就是生成一个密钥并保存在文件中

*              这个每次生成的密钥都不同,所以这个生成一次就行了,在写入文件的时候应该判断这个文件是否存在,这样是否更合理。*/privatevoidcreateKey() {try{//得到密钥的实例 以什么方式加密。加密的方式比较多。KeyGenerator kg=KeyGenerator.getInstance("DES");

kg.init(56);

SecretKey key=kg.generateKey();//将生成的密钥对象写入文件。ObjectOutputStream objectOutputStream=newObjectOutputStream(newFileOutputStream(newFile("e:\\key.obj")));

objectOutputStream.writeObject(key);

}catch(NoSuchAlgorithmException e) {

e.printStackTrace();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}/***@paramKeyFilePath

*            密钥Key对象的路径。注意使用该方法的时候,确保你已经生成了密钥。

*@return* @Description 从文件中读出Key,用于加密使用。*/privatestaticKey getKey(String KeyFilePath) {

Key key=null;try{//将生成的密钥对象从文件中读取出来,然后再强制转换成一个密钥对象。ObjectInputStream objectInputStream=newObjectInputStream(newFileInputStream(newFile(KeyFilePath)));

key=(Key) objectInputStream.readObject();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}catch(ClassNotFoundException e) {

e.printStackTrace();

}returnkey;

}/***@paramsource

*@return放回一个byte数组,为什么不放回字符串,是因为解密的时候要传入这个byte数组才能进行解密,如果解密的时候传入的是字符串

*         那么就会出错,愿意是编码的问题。

* @Description 将传入的字符串进行加密 下面写了将这种byte数组转换成字符串的方法。直接在调用就行了。*/publicbyte[] encrypt(String source) {byte[] target=null;try{byte[] center=source.getBytes("UTF-8");

Key key=getKey("e:\\key.obj");

Cipher cipher=Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, key);

target=cipher.doFinal(center);

}catch(NoSuchAlgorithmException e) {

e.printStackTrace();

}catch(NoSuchPaddingException e) {

e.printStackTrace();

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}catch(InvalidKeyException e) {

e.printStackTrace();

}catch(IllegalBlockSizeException e) {

e.printStackTrace();

}catch(BadPaddingException e) {

e.printStackTrace();

}returntarget;

}/***@paramsource

*            加密后的byte数组。可用加密方法encrypt(“String”)生成即可

*@return解密后的字符串。

* @Description 解密算法。*/publicbyte[] decrypt(byte[] source) {byte[] dissect=null;try{

Key key=getKey("e:\\key.obj");

Cipher cipher=Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE, key);//使用私钥解密dissect=cipher.doFinal(source);

}catch(NoSuchAlgorithmException e) {

e.printStackTrace();

}catch(NoSuchPaddingException e) {

e.printStackTrace();

}catch(InvalidKeyException e) {

e.printStackTrace();

}catch(IllegalBlockSizeException e) {

e.printStackTrace();

}catch(BadPaddingException e) {

e.printStackTrace();

}returndissect;

}/***@parambytes

* @Description 用于把加密后的byte[]数组采用特定的方式写入到文件。*/publicvoidencodeByteToFile(byte[] bytes){

BASE64Encoder base64encoder=newBASE64Encoder();try{

base64encoder.encode(bytes,newFileOutputStream(newFile("D:\\t.txt")));

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}/***@return* @Description 由于加密之前采用了编码的格式 所以现在采用特点的方式读出来 ,然后得到用一个byte[]用于解码。*/publicbyte[] getByteFromFile(){

BASE64Decoder base64decoder=newBASE64Decoder();byte[] encodeByte=null;try{

encodeByte=base64decoder.decodeBuffer(newFileInputStream(newFile("D:\\t.txt")));

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}returnencodeByte;

}/***@paramb

*@paramfilePath

* @Description 将指定的字节写入到文件中。*/publicvoidwriteByteToFile(byte[] b, String filePath) {

File file=newFile(filePath);if(!file.exists()) {try{

file.createNewFile();

}catch(IOException e) {

e.printStackTrace();

}

}

FileOutputStream fileOutputStream;try{

fileOutputStream=newFileOutputStream(file);

fileOutputStream.write(b);

fileOutputStream.close();

}catch(IOException e) {

e.printStackTrace();

}

}//测试使用。//public static void main(String[] args) {//Encryption encryption = new Encryption();encryption.dateEncrypt(encryption.encrypt("yangtao"));//System.out.println(new String(encryption.decrypt(encryption.getByteFromFile())));//}}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值