Encryption Problems

D

DougJrs

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 protected]</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 protected]</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
 
E

Esmond Pitt

DougJrs said:
String TestMessage = "<XML tag>[email protected]</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.
 
R

Red Orchid

Message-ID said:
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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top