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");