java加密觖密算法

import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;

public class CipherExample {
    private Cipher m_encrypter;
    private Cipher m_decrypter;
    public void init(SecretKey key) {
        byte[] initVector = new byte[] {0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x1,
                            0x02};
        AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
        try {
            m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
            m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
            m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
            m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);
        } catch (InvalidAlgorithmParameterException ex) {
        } catch (InvalidKeyException ex) {
        } catch (NoSuchPaddingException ex) {
        } catch (NoSuchAlgorithmException ex) {
        }

    }

    public void write(byte[] bytes, OutputStream out) {
        CipherOutputStream cos = new CipherOutputStream(out, m_encrypter);
        try {
            cos.write(bytes, 0, bytes.length);
            cos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void read(byte[] bytes, InputStream in) {
        CipherInputStream cis = new CipherInputStream(in, m_decrypter);
        int pos = 0, intValue;
        try {
            while ((intValue = cis.read()) != -1) {
                bytes[pos] = (byte) intValue;
                pos++;
            }
        } catch (IOException ex) {
        }
    }

    public byte[] encrypt(byte[] input) {
        try {
            return (m_encrypter.doFinal(input));
        } catch (BadPaddingException ex) {
            return null;
        } catch (IllegalBlockSizeException ex) {
            return null;
        }
    }

    public byte[] decrypt(byte[] input) {
        try {
            return (m_decrypter.doFinal(input));
        } catch (BadPaddingException ex) {
            return null;
        } catch (IllegalBlockSizeException ex) {
            return null;
        }
    }
    public static void main(String[] args) {
       CipherExample ce=new CipherExample();
       try
       {
           SecretKey key = KeyGenerator.getInstance("DES").generateKey();
           ce.init(key);
          
           System.out.println("Testing encrypt/decypt of bytes");
           byte[] clearText=new byte[]{65,73,82,68,65,78,67,69};
           byte[] encryptedText=ce.encrypt(clearText);
           byte[] decryptedText=ce.decrypt(encryptedText);
          
           String clearTextAsString =new String(clearText);
           String encTextAsString=new String(encryptedText);
           String decTextAsString=new String(decryptedText);
          
           System.out.println("   cleartext: "+clearTextAsString);
           System.out.println("   encrypted: "+encTextAsString);
           System.out.println("   decrypted: "+decTextAsString);
          
           System.out.println("/nTesting encrypting of a file/n");
           FileInputStream fis=new FileInputStream("c:/cipherTest.in");
           FileOutputStream fos=new FileOutputStream("c:/cipherTest.out");
          
           int dataInputSize=fis.available();
           byte[] inputBytes=new byte[dataInputSize];
           fis.read(inputBytes);
           ce.write(inputBytes,fos);
           fos.flush();
           fis.close();
           fos.close();
          
          
           String inputFileAsString=new String(inputBytes);
           System.out.println("INPUT FILE CONTENTS/n"+inputFileAsString+"/n");
          
           System.out.println("File encrypted and saved to dist/n");
           fis=new FileInputStream("c:/cipherTest.out");
           byte[] decrypted=new byte[dataInputSize];
           ce.read(decrypted,fis);
           fis.close();
           String decryptedAsString=new String(decrypted);
           System.out.println("DESCRYPTED FILE:/n"+decryptedAsString+"/n");
       }catch(Exception e)
       {
           e.printStackTrace();
       }
    }

}
 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值