Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Encryption Problems

Thread view: 
DougJrs - 03 Apr 2007 02:34 GMT
I am working on a java RSA encryption/decryption program.  Currently
any non-alpha or numeric character is converted into a /. For example
if I take the string
"<XML tag>email.address@domain.com</xml tag><xm2>data12345</xm2>"
and encrypt and decrypt I get
"/XML/tag/email/address/domain/com//xml/tag//xm2/data12345//xm2/x "
when I decrypt it.

Can anyone help?

Thanks in advance!
Doug

Here is my code:

Security.addProvider(new
org.bouncycastle.jce.provider.BouncyCastleProvider());
    RSAEncryptUtil cipher = new RSAEncryptUtil();

    KeyPair key;
    RSAPublicKey PublicKey;
    RSAPrivateKey PrivateKey;

    key = cipher.generateKey();

    PublicKey = (RSAPublicKey)key.getPublic();

    PrivateKey = (RSAPrivateKey)key.getPrivate();

    String TestMessage = "<XML tag>email.address@domain.com</xml
tag><xm2>data12345</xm2>";

    BASE64Decoder b642 = new BASE64Decoder();
       byte[] cipherText = b642.decodeBuffer(TestMessage);

    out.println("<p>test before cipher: " + cipherText);

       Cipher cipher2 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
       cipher2.init(Cipher.ENCRYPT_MODE, key.getPublic());
       cipherText = cipher2.doFinal(cipherText);

    out.println("<p> Encrypted text is: " + cipherText);

    byte[] dectyptedText = null;
    Cipher cipher3 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher3.init(Cipher.DECRYPT_MODE, key.getPrivate());
       dectyptedText = cipher3.doFinal(cipherText);
       BASE64Encoder b64 = new BASE64Encoder();
       out.println("<p> Unencrypted text is: " +
b64.encode(dectyptedText));

And here is the output that it creates:
test before cipher: [B@104474

Encrypted text is: [B@1fe5c54

Unencrypted text is: /XML/tag/email/address/domain/com//xml/tag//xm2/
data12345//xm2/x
Esmond Pitt - 03 Apr 2007 07:27 GMT
>     String TestMessage = "<XML tag>email.address@domain.com</xml
> tag><xm2>data12345</xm2>";
>
>     BASE64Decoder b642 = new BASE64Decoder();
>         byte[] cipherText = b642.decodeBuffer(TestMessage);

Here you are base-64-decoding something that isn't base-64-encoded, so
it presents a larger character set than the decoder can handle. Remove
this step, and the corresponding encode step at the end.
Base-64-encoding would normally be applied to the *ciphertext*, and it
would be base-64-decoded *prior* to deciphering.
Red Orchid - 03 Apr 2007 20:34 GMT
"DougJrs" <dougjrs@gmail.com> wrote or quoted in
Message-ID <1175564068.594472.288380@q75g2000hsh.googlegroups.com>:

>     BASE64Decoder b642 = new BASE64Decoder();
>         byte[] cipherText = b642.decodeBuffer(TestMessage);

Maybe, BASE64Encoder, not BASE64Decoder

>         BASE64Encoder b64 = new BASE64Encoder();
>         out.println("<p> Unencrypted text is: " +
> b64.encode(dectyptedText));

Here, BASE64Decoder.

On the other hand, as another way, Base64Decoder/Encoder can be
removed in your code.  For example,

byte[] cipherText = TestMessage.getBytes("UTF-8");
String result = new String(dectyptedText, "UTF-8");


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.