java DES 例子


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())));
//     }
}




根据引用的内容,这段引用中包含了一些Vue.js代码和HTML代码,并讨论了换行符和空格在网页中的使用。其中,引用是关于添加和还原换行符的方法,引用是关于在编辑数据时获取详情和使用textarea的示例代码,引用是关于HTML中空格的处理和字符实体的使用。 问题中提到的&nbsp;是HTML中表示空格的字符实体。HTML通常会自动截取多余的空格,不管你加多少空格,都被看做一个空格。为了在网页中增加空格,可以使用&nbsp;表示空格。此外,还有其他常用的字符实体,如<表示小于号,>表示大于号,&表示&符号,"表示双引号,等等。 需要注意的是,引用的内容中有一些HTML实体被转义了,所以在描述字符实体时需要参考编码转义后的字符。 根据给定的引用内容,我无法直接回答问题,因为问题中的信息不完整。请提供更具体的问题或补充说明,我将尽力为您提供帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [js+v-html+正则表达式去除所有& nbsp和html标签(& nbsp 换行 )](https://blog.csdn.net/xm_w_xm/article/details/91992754)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [html&nbsp;&nbsp;特殊字符(转)](https://blog.csdn.net/feiniu5566/article/details/13624619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值