Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java API
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Other JSE/JEE APIs
RSA Encryption / Decryption does not work
Markus Schmider
Ranch Hand
Posts: 153
3
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello,
to try out
Java
security features I have written the following class (I know I would not be really usefull, just for trying the API):
public class RSAEncrypter { private KeyPair keyPair; public RSAEncrypter() { super(); try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); this.keyPair = keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } public byte[] encrypt(String message) { byte[] encrypted = null; try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, this.keyPair.getPublic()); encrypted = cipher.doFinal(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return encrypted; } public String decrypte(byte[] encrypted) { byte[] decrypted = null; try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, this.keyPair.getPrivate()); decrypted = cipher.doFinal(encrypted); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return new String(decrypted); } }
I call it with the following code
RSAEncrypter rsaEncrypter = new RSAEncrypter(); String message = "Vivamus mea Lesbia"; String encryptedMessage = rsaEncrypter.encrypt(message); System.out.println("Encypted"); System.out.println(new String(encryptedMessage)); System.out.println("Decrypted"); String decrypted = rsaEncrypter.decrypte(encryptedMessage); System.out.println(decrypted);
However the original messages is not restored.
decrypted contains a
String
of zero-length.
Where is the mistake??
Thanks for any input,
Hans
Rob Spoor
Sheriff
Posts: 22815
132
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Check out line 22.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Markus Schmider
Ranch Hand
Posts: 153
3
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I must be blind...
Thanks a lot
encrypted = cipher.doFinal(message.getBytes());
Rob Spoor
Sheriff
Posts: 22815
132
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You're welcome
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Need Help in Encryption
Length of Encrypted String Longer than Original String... Help
RSA decryption, BadPaddingException: Data must start with zero
BadPaddingException using AES
Digital Signature in PKCS 7 format with base 64 encoding
More...