java DES 例子

本文介绍了一种基于Java的DES加密解密方法实现过程,包括密钥的生成与保存、字符串的加密与解密等功能,并提供了文件读写的具体操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


package aosa.safemedia.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

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

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

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


public class Encryption {
    

    
private void createKey() {
        
try {
            
// 得到密钥的实例 以什么方式加密。加密的方式比较多。
            KeyGenerator kg = KeyGenerator.getInstance("DES");
            kg.init(
56);
            SecretKey key 
= kg.generateKey();
            
// 将生成的密钥对象写入文件。
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    
new FileOutputStream(new File("e:\\key.obj")));
            objectOutputStream.writeObject(key);
        
catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        
catch (FileNotFoundException e) {
            e.printStackTrace();
        
catch (IOException e) {
            e.printStackTrace();
        }
    }

    

    
private static Key getKey(String KeyFilePath) {
        Key key 
= null;
        
try {
            
// 将生成的密钥对象从文件中读取出来,然后再强制转换成一个密钥对象。
            ObjectInputStream objectInputStream = new ObjectInputStream(
                    
new FileInputStream(new File(KeyFilePath)));
            key 
= (Key) objectInputStream.readObject();
        
catch (FileNotFoundException e) {
            e.printStackTrace();
        
catch (IOException e) {
            e.printStackTrace();
        
catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
return key;
    }

    

    
public  byte[] 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 (IllegalBlockSizeExceptioe) {
            e.printStackTrace();
        
catch (BadPaddingException e) {
            e.printStackTrace();
        }
        
        
return target;
    }

    

    
public  byte[] 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 (IllegalBlockSizeExceptioe) {
            e.printStackTrace();
        
catch (BadPaddingException e) {
            e.printStackTrace();
        }
        
return dissect;
    }
    
    
    

    
public void encodeByteToFile(byte[] bytes){
        BASE64Encoder base64encoder 
= new BASE64Encoder();
        
try {
            base64encoder.encode(bytes,
new FileOutputStream(new File("D:\\t.txt")));
        
catch (FileNotFoundException e) {
            e.printStackTrace();
        
catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    
    
    

    
public byte[] getByteFromFile(){
        BASE64Decoder base64decoder 
= new BASE64Decoder();
        
byte[] encodeByte = null;
        
try {
            encodeByte 
= base64decoder.decodeBuffer(new FileInputStream(new File("D:\\t.txt")));
        
catch (FileNotFoundException e) {
            e.printStackTrace();
        
catch (IOException e) {
            e.printStackTrace();
        }
        
return encodeByte;
    }
    
    

    
public  void writeByteToFile(byte[] b, String filePath) {
        File file 
= new File(filePath);
        
if (!file.exists()) {
            
try {
                file.createNewFile();
            
catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileOutputStream fileOutputStream;
        
try {
            fileOutputStream 
= new FileOutputStream(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())));
//    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值