⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ciphersample.java

📁 Encrypt your stream data/file by JAVA
💻 JAVA
字号:
package Crypto;

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

public class CipherSample {
	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) {
		CipherSample ce=new CipherSample();
		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:\\temp\\LogToFile2.xml");
			FileOutputStream fos=new FileOutputStream("c:\\temp\\Log.enc");
			
			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:\\temp\\Log.enc");
			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();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -