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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/**
* Encrypts the specified input file to the specified output file.
* @param inputFileName the name of the file to encrypt.
* @param outputFileName the name of the file where you want to write
* the encrypted file.
* @throws java.io.FileNotFoundException if the file to encrypt doesn't
* exist.
* @throws java.io.IOException if an error occurs during writing encrypted
* datas.
* @throws java.security.InvalidKeyException if the key is not valid for
* this type of operation.
* @throws javax.crypto.IllegalBlockSizeException if the cipher is a block
* cipher, no padding has been requested, and the length of the encoding
* of the key to be wrapped is not a multiple of the block size.
*/
public void encryptFile(String inputFileName, String outputFileName)
throws InvalidKeyException, IllegalBlockSizeException,
FileNotFoundException, IOException
{
if(!this.isEncryptInitialized)
throw new IllegalStateException("The underlying encryptor is not" +
" initialized in encrypt mode. You must specify a " +
"public key.");
KeyGenerator keygen = null;
Cipher cipherRsa = null;
Cipher cipherAes= null;
try
{
keygen = KeyGenerator.getInstance("AES");
cipherRsa = Cipher.getInstance("RSA");
cipherAes = Cipher.getInstance("AES");
}
catch(NoSuchAlgorithmException e)//never launched
{
e.printStackTrace();
}
catch(NoSuchPaddingException e)
{
e.printStackTrace();
}
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
cipherRsa.init(Cipher.WRAP_MODE, publicKey);
byte[] wrappedKey = cipherRsa.wrap(key);
DataOutputStream out = new DataOutputStream(
new FileOutputStream(outputFileName));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
InputStream in = new FileInputStream(inputFileName);
cipherAes.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipherAes);
in.close();
out.close();
}
/**
* Encrypts the specified input file to the specified output file.
* @param inputFile file to encrypt.
* @param outputFile the file where you want to write the encrypted file.
* @throws java.io.FileNotFoundException if the file to encrypt doesn't
* exist.
* @throws java.io.IOException if an error occurs during writing encrypted
* datas.
* @throws java.security.InvalidKeyException if the key is not valid for
* this type of operation.
* @throws javax.crypto.IllegalBlockSizeException if the cipher is a block
* cipher, no padding has been requested, and the length of the encoding
* of the key to be wrapped is not a multiple of the block size.
*/
public void encryptFile(File inputFile, File outputFile)
throws InvalidKeyException, IllegalBlockSizeException,
FileNotFoundException, IOException
{
if(!this.isEncryptInitialized)
throw new IllegalStateException("The underlying encryptor is not" +
" initialized in encrypt mode. You must specify a " +
"public key.");
KeyGenerator keygen = null;
Cipher cipherRsa = null;
Cipher cipherAes= null;
try
{
keygen = KeyGenerator.getInstance("AES");
cipherRsa = Cipher.getInstance("RSA");
cipherAes = Cipher.getInstance("AES");
}
catch(NoSuchAlgorithmException e)//never launched
{
e.printStackTrace();
}
catch(NoSuchPaddingException e)
{
e.printStackTrace();
}
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
cipherRsa.init(Cipher.WRAP_MODE, publicKey);
byte[] wrappedKey = cipherRsa.wrap(key);
DataOutputStream out = new DataOutputStream(
new FileOutputStream(outputFile));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
InputStream in = new FileInputStream(inputFile);
cipherAes.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipherAes);
in.close();
out.close();
}
/**
* Decrypts the specified input file to the specified output file.
* @param inputFileName the name of the file to decrypt.
* @param outputFileName the name of the file where you want to write
* the decrypted file.
* @throws java.io.FileNotFoundException if the file to decrypt doesn't
* exist.
* @throws java.io.IOException if an error occurs during writing decrypted
* datas.
* @throws java.security.InvalidKeyException if the key is not valid for
* this type of operation.
*/
public void decryptFile(String inputFileName, String outputFileName)
throws FileNotFoundException, IOException, InvalidKeyException
{
if(!this.isEncryptInitialized)
throw new IllegalStateException("The underlying encryptor is not" +
" initialized in decrypt mode. You must specify a " +
"private key.");
DataInputStream in = new DataInputStream(
new FileInputStream(inputFileName));
int length = in.readInt();
byte[] wrappedKey = new byte[length];
in.read(wrappedKey, 0, length);
Cipher cipherRsa = null;
Cipher cipherAes = null;
Key key = null;
try
{
cipherRsa = Cipher.getInstance("RSA");
cipherAes = Cipher.getInstance("AES");
cipherRsa.init(Cipher.UNWRAP_MODE, privateKey);
key = cipherRsa.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
}
catch(NoSuchAlgorithmException e)//never thrown
{
e.printStackTrace();
}
catch(NoSuchPaddingException e)//never thrown
{
e.printStackTrace();
}
OutputStream out = new FileOutputStream(outputFileName);
cipherAes.init(Cipher.DECRYPT_MODE, key);
crypt(in, out, cipherAes);
in.close();
out.close();
}
/**
* Decrypts the specified input file to the specified output file.
* @param inputFile the file to decrypt.
* @param outputFile the file where you want to write the decrypted file.
* @throws java.io.FileNotFoundException if the file to decrypt doesn't
* exist.
* @throws java.io.IOException if an error occurs during writing decrypted
* datas.
* @throws java.security.InvalidKeyException if the key is not valid for
* this type of operation.
*/
public void decryptFile(File inputFile, File outputFile)
throws FileNotFoundException, IOException, InvalidKeyException
{
if(!this.isEncryptInitialized)
throw new IllegalStateException("The underlying encryptor is not" +
" initialized in decrypt mode. You must specify a " +
"private key.");
DataInputStream in = new DataInputStream(
new FileInputStream(inputFile));
int length = in.readInt();
byte[] wrappedKey = new byte[length];
in.read(wrappedKey, 0, length);
Cipher cipherRsa = null;
Cipher cipherAes = null;
Key key = null;
try
{
cipherRsa = Cipher.getInstance("RSA");
cipherAes = Cipher.getInstance("AES");
cipherRsa.init(Cipher.UNWRAP_MODE, privateKey);
key = cipherRsa.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
}
catch(NoSuchAlgorithmException e)//never thrown
{
e.printStackTrace();
}
catch(NoSuchPaddingException e)//never thrown
{
e.printStackTrace();
}
OutputStream out = new FileOutputStream(outputFile);
cipherAes.init(Cipher.DECRYPT_MODE, key);
crypt(in, out, cipherAes);
in.close();
out.close();
}
/**
* Encrypts the specified input stream to the specified output stream.
* @param in the stream to encrypt.
* @param out the stream where you want to write the encrypted stream.
* @throws java.io.IOException if an error occurs during writing encrypted
* datas.
* @throws java.security.InvalidKeyException if the key is not valid for
* this type of operation.
* @throws javax.crypto.IllegalBlockSizeException if the cipher is a block
* cipher, no padding has been requested, and the length of the encoding
* of the key to be wrapped is not a multiple of the block size.
*/
public void encryptStream(InputStream in, OutputStream out)
throws InvalidKeyException, IllegalBlockSizeException, IOException
{
if(!this.isEncryptInitialized)
throw new IllegalStateException("The underlying encryptor is not" +
" initialized in encrypt mode. You must specify a " +
"public key.");
KeyGenerator keygen = null;
Cipher cipherRsa = null;
Cipher cipherAes= null;
try
{
keygen = KeyGenerator.getInstance("AES");
cipherRsa = Cipher.getInstance("RSA");
cipherAes = Cipher.getInstance("AES");
}
catch(NoSuchAlgorithmException e)//never launched
{
e.printStackTrace();
}
catch(NoSuchPaddingException e)
{
e.printStackTrace();
}
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
cipherRsa.init(Cipher.WRAP_MODE, publicKey);
byte[] wrappedKey = cipherRsa.wrap(key);
Integer length = wrappedKey.length;
out.write(length.byteValue());
out.write(wrappedKey);
cipherAes.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipherAes);
in.close();
out.close();
}
/**
* Decrypts the specified input stream to the specified output stream.
* @param in the stream to decrypt.
* @param out the stream where you want to write the decrypted stream.
* @throws java.io.IOException if an error occurs during writing encrypted
* datas.
* @throws java.security.InvalidKeyException if the key is not valid for
* this type of operation.
*/
public void decryptStream(InputStream in, OutputStream out)
throws IOException, InvalidKeyException
{
if(!this.isEncryptInitialized)
throw new IllegalStateException("The underlying encryptor is not" +
" initialized in decrypt mode. You must specify a " +
"private key.");
int length = in.read();
byte[] wrappedKey = new byte[length];
in.read(wrappedKey, 0, length);
Cipher cipherRsa = null;
Cipher cipherAes = null;
Key key = null;
try
{
cipherRsa = Cipher.getInstance("RSA");
cipherAes = Cipher.getInstance("AES");
cipherRsa.init(Cipher.UNWRAP_MODE, privateKey);
key = cipherRsa.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
}
catch(NoSuchAlgorithmException e)//never thrown
{
e.printStackTrace();
}
catch(NoSuchPaddingException e)
{
e.printStackTrace();
}
cipherAes.init(Cipher.DECRYPT_MODE, key);
crypt(in, out, cipherAes);
in.close();
out.close();
}
/**
* Crypts or decrypts the specified input stream to the specified output
* stream with a given cipher. The crypting or decrypting operation is
* determined by the cipher's state.
* @param cipher the cipher used to crypt datas.
* @param in the input srteal stream to be encypted or decrypted.
* @param out the output stream to be encypted or decrypted.
* @throws java.io.IOException if an I/O error occurs during reading
* the input stream or writting datas to the output stream.
*/
public void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException
{
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)
{
try
{
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
}
catch(ShortBufferException e)
{
e.printStackTrace();
}
}
else
done = true;
}
try
{
if(inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.write(outBytes);
}
catch(IllegalBlockSizeException e)
{
e.printStackTrace();
}
catch(BadPaddingException e)
{
e.printStackTrace();
}
}
} |
Partager