M
Marco A. Cruz Quevedo
Hi everybody,
Finally, I have the following code:
.. . . . . . . . . . . . . . . . . . . . . . . . . . .
InputStream my_inStream = new FileInputStream("myown_b64.cer");
CertificateFactory my_cf = CertificateFactory.getInstance("X.509");
X509Certificate my_cert =
(X509Certificate)my_cf.generateCertificate(my_inStream);
System.out.println("Serial number:"+my_cert.getSerialNumber());
RSAPublicKey my_pubkey = (RSAPublicKey)my_cert.getPublicKey();
my_inStream.close();
String orig_message ="The original message";
Cipher my_cipher = Cipher.getInstance("RSA");
my_cipher.init(Cipher.ENCRYPT_MODE, my_cert);
byte[] scrambled = my_cipher.doFinal(orig_message.getBytes());
String encoded = Base64.encodeBytes(scrambled);
System.out.println("Message: "+orig_message);
System.out.println("\nEncrypted: "+encoded);
.. . . . . . . . . . . . . . . . . . . . . . . . . . .
The original message is constant and everytime I run the program, the
encrypted message is different. I tried with different length messages
and the encrypted message has same length. What is wrong? Is it
normal?.
Any help will be appreciated.
Best regards,
Marco
Finally, I have the following code:
.. . . . . . . . . . . . . . . . . . . . . . . . . . .
InputStream my_inStream = new FileInputStream("myown_b64.cer");
CertificateFactory my_cf = CertificateFactory.getInstance("X.509");
X509Certificate my_cert =
(X509Certificate)my_cf.generateCertificate(my_inStream);
System.out.println("Serial number:"+my_cert.getSerialNumber());
RSAPublicKey my_pubkey = (RSAPublicKey)my_cert.getPublicKey();
my_inStream.close();
String orig_message ="The original message";
Cipher my_cipher = Cipher.getInstance("RSA");
my_cipher.init(Cipher.ENCRYPT_MODE, my_cert);
byte[] scrambled = my_cipher.doFinal(orig_message.getBytes());
String encoded = Base64.encodeBytes(scrambled);
System.out.println("Message: "+orig_message);
System.out.println("\nEncrypted: "+encoded);
.. . . . . . . . . . . . . . . . . . . . . . . . . . .
The original message is constant and everytime I run the program, the
encrypted message is different. I tried with different length messages
and the encrypted message has same length. What is wrong? Is it
normal?.
Any help will be appreciated.
Best regards,
Marco