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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| /*
* RSAEncryptor.java
*
* Created on 17 juin 2006, 15:58
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Security;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
*
* @author Absil Romain
*/
public class RSAEncryptor
{
/**
* Generate a pair of key, write the public in publicOutputFileName and the
* private in privateOutputFileName.
* @param publicOutputFileName the file where the public key is written in.
* @param privateOutputFileName the file where the private key is written in.
**/
public static void generateKeys(String publicInputFileName,
String privateOutputFileName)
{
try
{
KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
pairgen.initialize(2048, random);
KeyPair keyPair = pairgen.generateKeyPair();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(publicInputFileName));
out.writeObject(keyPair.getPublic());
out.close();
out = new ObjectOutputStream(
new FileOutputStream(privateOutputFileName));
out.writeObject(keyPair.getPrivate());
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Encrytps the file inputFileName and saves the result in the file
* outputFileName, with the specified key keyFileName.
* @param inputFileNane the file to encrypt.
* @param outputFileName the file to save the encrypting result in.
* @param keyFileName the key to use to encrypt the file.
**/
public static void encryptFile(String inputFileName, String outputFileName,
String keyFileName)
{
try
{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
//emballe avec la clé publique RSA
ObjectInputStream keyIn = new ObjectInputStream(
new FileInputStream(keyFileName));
Key publicKey = (Key)keyIn.readObject();
keyIn.close();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE, publicKey);
byte[] wrappedKey = cipher.wrap(key);
DataOutputStream out = new DataOutputStream(
new FileOutputStream(outputFileName));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
InputStream in = new FileInputStream(inputFileName);
cipher = cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Decrytps the file inputFileName and saves the result in the file
* outputFileName, with the specified key keyFileName.
* @param inputFileName the file to decrypt.
* @param outputFileName the file to save the decrypting result in.
* @param keyFileName the key to use to decrypt the file.
**/
public static void decryptFile(String inputFileName, String outputFileName,
String keyFileName)
{
try
{
DataInputStream in = new DataInputStream(
new FileInputStream(inputFileName));
int length = in.readInt();
byte[] wrappedKey = new byte[length];
in.read(wrappedKey, 0, length);
//déballe avec la clé RSA
ObjectInputStream keyIn = new ObjectInputStream(
new FileInputStream(keyFileName));
Key privateKey = (Key)keyIn.readObject();
keyIn.close();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
OutputStream out = new FileOutputStream(outputFileName);
cipher = cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private static void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException, GeneralSecurityException
{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean done = false;
while(!done)
{
inLength = in.read(inBytes);
if(inLength == blockSize)
{
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
}
else
done = true;
}
if(inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.write(outBytes);
}
} |
Partager