Pessoal estou a ter problemas a cifrar/decifrar um objecto com encriptação RSA 1024 bits:
tenho:
public SealedObject cifrarObjecto(Serializable ob, PublicKey pubKey) {
SealedObject objectoCifrado = null;
try {
Cipher ecifrador = Cipher.getInstance("RSA");
ecifrador.init(Cipher.ENCRYPT_MODE, pubKey);
// cifrar o objecto
objectoCifrado = new SealedObject(ob, ecifrador);
} catch (IOException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
}
return objectoCifrado;
}
tenho:
public Object decifrarObjecto(SealedObject obCifrado, PrivateKey priKey) {
Object objectoDecifrado = null;
try {
// inicia decifrador
Cipher dcipher = Cipher.getInstance("RSA");
dcipher.init(Cipher.DECRYPT_MODE, priKey);
// Unseal (decrypt) the class
objectoDecifrado = (Object) obCifrado.getObject(dcipher);
} catch (IOException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(ElectionSecurity.class.getName()).log(Level.SEVERE, null, ex);
}
return objectoDecifrado;
}
para testar fiz num main(…)
PublicKey pub = es.extrairChavePublica(new File("chaveiroPublico.jks"), "key", "password");
SealedObject tuploCifrado = es.cifrarObjecto(ot, pub);
System.out.println("O tuplo foi cifrado!");
PrivateKey pri = es.extrairChavePrivada(new File("chaveiroPrivado.jks"), "key", "password");
Tuplo<Object> tuploDecifrado = ( Tuplo<Object> ) es.decifrarObjecto(tuploCifrado, pri);
System.out.println("O tuplo foi decifrado!");
System.out.printf("Object tuple: %s\n", tuploDecifrado.toString());
Então dá-me este erro que não estou a ver muito bem como contornar…
11/Mai/2009 19:45:27 ElectionSecurity cifrarObjecto
SEVERE: null
javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA13*…)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*…)
at javax.crypto.Cipher.doFinal(DashoA13*…)
at javax.crypto.SealedObject.(DashoA13*…)
at ElectionSecurity.cifrarObjecto(ElectionSecurity.java:536)
O tuplo foi decifrado!
at testSecurity.main(testSecurity.java:56)
11/Mai/2009 19:45:27 ElectionSecurity decifrarObjecto
SEVERE: null
java.security.InvalidKeyException: No installed provider supports this key: (null)
at javax.crypto.Cipher.a(DashoA13*…)
at javax.crypto.Cipher.init(DashoA13*…)
at javax.crypto.Cipher.init(DashoA13*…)
at ElectionSecurity.decifrarObjecto(ElectionSecurity.java:566)
at testSecurity.main(testSecurity.java:59)
11/Mai/2009 19:45:27 testSecurity main
SEVERE: null
java.lang.NullPointerException
at testSecurity.main(testSecurity.java:61)
Qualquer ajuda é bem vinda, obrigado desde já…