Opa! tô conseguindo criptografar já ! Olha como ficou o método de criptografar:
public void criptografarArquivo(File arquivo, PublicKey chavePublica)
throws CSFException, NoSuchProviderException, InvalidAlgorithmParameterException {
//
try {
Mac mac = Mac.getInstance("HMACSHA1","BC");
chavePublica.hashCode();
SecureRandom sec_rand = new SecureRandom();
byte[] mac_key_bytes = new byte[20]; // Set to correct length for HMACSHA1
sec_rand.nextBytes(mac_key_bytes); // Fill with random data,
Key mac_key = new SecretKeySpec(mac_key_bytes, "HMACSHA1"); // Convert to key object.
mac.init(mac_key); // Initialize.
KeyGenerator key_gen = KeyGenerator.getInstance("AES", "BC"); // Using appropriate key generator.
key_gen.init(128, sec_rand); // Set up with key size and a SecureRandom.
Key aes_key = key_gen.generateKey(); // Generate the Key Object.
byte[] ivBytes = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
//byte[] keyBytes = chavePublica.getEncoded();
//SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, aes_key, ivSpec);
byte [] arquivoEmBytes = UtilArquivos.abrirArquivoEmArrayDeBytes(arquivo);
byte [] arquivoCriptografado = null;
arquivoCriptografado = cipher.doFinal(arquivoEmBytes);
File arqCripto = new File(arquivo.getAbsolutePath()+".p7b");
FileOutputStream fos = new FileOutputStream(arqCripto);
fos.write(arquivoCriptografado);
fos.close();
} catch (NoSuchAlgorithmException e) {
throw new CSFExceptionCritica(e.getMessage(),e.getStackTrace());
} catch (NoSuchPaddingException e) {
throw new CSFExceptionCritica(e.getMessage(),e.getStackTrace());
} catch (InvalidKeyException e) {
throw new CSFExceptionCritica(e.getMessage(),e.getStackTrace());
} catch (IOException e) {
throw new CSFExceptionCritica(e.getMessage(),e.getStackTrace());
} catch (IllegalBlockSizeException e) {
throw new CSFExceptionCritica(e.getMessage(),e.getStackTrace());
} catch (BadPaddingException e) {
throw new CSFExceptionCritica(e.getMessage(),e.getStackTrace());
}
}
agora quero pegar informações sobre esse arquivo criptografado, isso é possível?? Desse jeito q eu criptografei tem como pegar informações do arquivo se eu tivesse extraído essa chave pública aí de um certificado??