FrameworkDemoiselle.gov.brCommunity Documentation

Capítulo 7. Exemplos de Uso

7.1. Carregar um array de bytes de um arquivo
7.2. Gravar um array de bytes em um arquivo
7.3. Carregar uma chave privada em arquivo
7.4. Carregar uma chave privada de um token
7.5. Carregar uma chave pública em arquivo
7.6. Carregar uma chave pública de um token
7.7. Carregar um certificado digital de um arquivo
7.8. Carregar um certificado digital de um token

A seguir temos alguns exemplos de tarefas normalmente necessárias na utilização do componente.


byte[] result = null;
File file = new File("documento.odp");
FileInputStream is = new FileInputStream(file);
result = new byte[(int) file.length()];
is.read(result);
is.close();
return result;

byte[] conteudo = "este eh um conteudo de arquivo texto".getBytes();
FileOutputStream out = new FileOutputStream(new File("arquivo.txt");
out.write(sign);
out.close();

File file = new File("private_rsa_1024.pkcs8");
fileContent = new byte[(int) file.length()];
is = new FileInputStream(file);
is.read(fileContent);
is.close();
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(fileContent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey chavePrivada = keyFactory.generatePrivate(privateKeySpec);

KeyStoreLoader keyStoreLoader = KeyStoreLoaderFactory.factoryKeyStoreLoader();
KeyStore keyStore = keyStoreLoader.getKeyStore("pinnumber");
String certificateAlias = keyStore.aliases().nextElement();
PrivateKey chavePrivada = (PrivateKey)keyStore.getKey(certificateAlias, "pinnumber");

File file = new File("public_rsa_1024.pkcs8");
byte[] fileContent = new byte[(int) file.length()];
is = new FileInputStream(file);
is.read(fileContent);
is.close();
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(fileContent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey chavePublica = keyFactory.generatePublic(publicKeySpec);

KeyStoreLoader keyStoreLoader = KeyStoreLoaderFactory.factoryKeyStoreLoader();
KeyStore keyStore = keyStoreLoader.getKeyStore();
CertificateLoader certificateLoader = new CertificateLoader();
certificateLoader.setKeyStore(keyStore);
X509Certificate certificate = certificateLoader.loadFromToken();
PublicKey chavePublica = certificate.getPublicKey();

CertificateLoader certificateLoader = new CertificateLoaderImpl();
X509Certificate certificate = certificateLoader.load(new File("certificado.cer");

KeyStoreLoader keyStoreLoader = KeyStoreLoaderFactory.factoryKeyStoreLoader();
KeyStore keyStore = keyStoreLoader.getKeyStore();
CertificateLoader certificateLoader = new CertificateLoaderImpl();
certificateLoader.setKeyStore(keyStore);
X509Certificate certificate = certificateLoader.loadFromToken();