Tecnologia do Blogger.
RSS

[androidbrasil-dev] RSA para Android

Seguinte, eu tenho uma chave pública gerada de um servidor Rails. Meu objetivo é criptografar dados através do RSA. Por exemplo,

Recupero o arquivo public-key.pem

String filePath = getFilesDir().getAbsolutePath();
File file = new File(filePath, "public-key.pem");
FileInputStream fileInput = new FileInputStream(file);

De acordo com esse link para criptografar os dados seria dessa forma:


Obtendo a publicKey:

DataInputStream dis = new DataInputStream(fileInput);
byte[] keyBytes = new byte[fileInput.available()];
dis.readFully(keyBytes);
dis.close();
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
keyFactory.generatePublic(spec);------ Erro acontece nesta linha.

Criptografando o dado:

byte[] encryptedBytes;
Cipher cipher = Cipher.getInstance("RSA");
PublicKey publicKey = getPublicKey();
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(plain.getBytes());
String encrypted = new String(encryptedBytes);


Estou recebendo esta exception
java.security.spec.InvalidKeySpecException: java.io.IOException: cannot recognise object in stream

Já testei com várias outras chaves e dá o mesmo erro...
--







Atenciosamente,
Douglas Alípio Mesquita

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário