Java/Security/File Secure IO

Материал из Java эксперт
Перейти к: навигация, поиск

Basic IO example using SHA1

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.DigestInputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
 * Basic IO example using SHA1
 */
public class MainClass {
  public static void main(String[] args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());        
    byte[] input = "www.jexp.ru".getBytes();
    System.out.println("input     : " + new String(input));    
    MessageDigest hash = MessageDigest.getInstance("SHA1");
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input);
    DigestInputStream digestInputStream = new DigestInputStream(byteArrayInputStream, hash);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int ch;
    while ((ch = digestInputStream.read()) >= 0) {
      byteArrayOutputStream.write(ch);
    }
    byte[] newInput = byteArrayOutputStream.toByteArray();
    System.out.println("in digest : " + new String(digestInputStream.getMessageDigest().digest()));
    byteArrayOutputStream = new ByteArrayOutputStream();
    DigestOutputStream digestOutputStream = new DigestOutputStream(byteArrayOutputStream, hash);
    digestOutputStream.write(newInput);
    digestOutputStream.close();
    System.out.println("out digest: " + new String(digestOutputStream.getMessageDigest().digest()));
  }
}





Basic IO example with CTR using AES

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
 * Basic IO example with CTR using AES
 */
public class MainClass {
  public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());        
    byte[] input = "www.jexp.ru".getBytes();
    byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
        0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
    byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x01 };
    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
    System.out.println("input : " + new String(input));
    // encryption pass
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
    ByteArrayInputStream bIn = new ByteArrayInputStream(input);
    CipherInputStream cIn = new CipherInputStream(bIn, cipher);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    int ch;
    while ((ch = cIn.read()) >= 0) {
      bOut.write(ch);
    }
    byte[] cipherText = bOut.toByteArray();
    System.out.println("cipher: " + new String(cipherText));
    // decryption pass
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    bOut = new ByteArrayOutputStream();
    CipherOutputStream cOut = new CipherOutputStream(bOut, cipher);
    cOut.write(cipherText);
    cOut.close();
    System.out.println("plain : " + new String(bOut.toByteArray()));
  }
}





DES Crypter And Decryper File

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
public class MainClass {
  static Cipher m_encrypter;
  static Cipher m_decrypter;
  public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();
    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };
    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);
    FileInputStream fis = new FileInputStream("cipherTest.in");
    FileOutputStream fos = new FileOutputStream("cipherTest.out");
    int dataInputSize = fis.available();
    byte[] inputBytes = new byte[dataInputSize];
    fis.read(inputBytes);
    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 disk\n");
    fis = new FileInputStream("cipherTest.out");
    byte[] decrypted = new byte[dataInputSize];
    read(decrypted, fis);
    fis.close();
    String decryptedAsString = new String(decrypted);
    System.out.println("DECRYPTED FILE:\n" + decryptedAsString + "\n");
  }
  public static void write(byte[] bytes, OutputStream out) throws Exception {
    CipherOutputStream cos = new CipherOutputStream(out, m_encrypter);
    cos.write(bytes, 0, bytes.length);
    cos.close();
  }
  public static void read(byte[] bytes, InputStream in) throws Exception {
    CipherInputStream cis = new CipherInputStream(in, m_decrypter);
    int pos = 0, intValue;
    while ((intValue = cis.read()) != -1) {
      bytes[pos] = (byte) intValue;
      pos++;
    }
  }
}