1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
/*
* SecureObjectInputStream.java
*
* Created on 5 mai 2007, 11:04
*
*/
package security;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
/**
* This class models a SecureObjectInputStream object that will be able to
* @author Absil Romain
*/
public class RSASecureObjectInputStream
{
private SecretKey secretKey;
private PrivateKey privateKey;
private ObjectInputStream in;
public RSASecureObjectInputStream(String inputFileName,
String privateRSAKeyInputFileName, String cryptedKey)
throws FileNotFoundException, IOException, InvalidKeyException,
ClassNotFoundException
{
DataInputStream in = new DataInputStream(new FileInputStream(cryptedKey));
File f = new File(cryptedKey);
int length = (int)f.length();
byte[] wrappedKey = new byte[length];
in.read(wrappedKey, 0, length);
ObjectInputStream keyIn = new ObjectInputStream(
new FileInputStream(privateRSAKeyInputFileName));
privateKey = (PrivateKey)keyIn.readObject();
keyIn.close();
try
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.UNWRAP_MODE, privateKey);
this.secretKey = (SecretKey)cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
this.in = new ObjectInputStream(new CipherInputStream(new FileInputStream(inputFileName), cipher));
}
catch(Exception e)
{}
}
public Object readObject() throws IOException, ClassNotFoundException
{
return in.readObject();
}
public void close() throws IOException
{
in.close();
}
} |
Partager