# Encrypting text typed in a JTextArea

Waqas Shafiq (wshafiq@cyberjunkie.com)
Tue, 06 Jul 1999 12:16:17 +0500

- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: rammohan mrh: "query"
- Previous message: Hemant Dhavala: "problem with writing the generated key to a file"

    Date: Tue, 06 Jul 1999 12:16:17 +0500

    From: Waqas Shafiq <wshafiq@cyberjunkie.com>

    To: java-security@java.Sun.COM

    Subject: Encrypting text typed in a JTextArea

--------------5B50AB862989BFF9E5CDFB2F
Content-Type: text/plain; charset="us-ascii"
X-Sun-Content-Length: 1506

I am having trouble encrypting input data obtained from a JTextArea.
Consider the following:

I have made two functions:

1. public byte[] encrypt(byte[] clearText)

2. public byte[] decrypt(byte[] cipherText)

Case I [This works]

...

String myText = "This is an example of how dat can be encrypted";

byte[] cipherText = encrypt(myText.getBytes());

System.out.println(new String(decrypt(cipherText)));

...

Case II [This does not work]

...

JTextArea myTextArea = new JTextArea();

...

I type the same sentence as in Case I.

...

String myText = myTextArea.getText();

byte[] cipherText = encrypt(myText.getBytes());

System.out.println(new String(decrypt(cipherText)));

...

It appears that there is a problem how text area encodes characters.
The problem disappers when I use StringTokenizer to obtain words
(leaving
behind tab spaces, new line etc. and other such characters.) from
"myText"
string in Case II.

I would like to store the cipherText to a file as a string
(i.e. String myString = new String(cipherText))
on Win95. Copy the file to a Unix machine, read it and change the
string to byte[] (myString.getBytes()) in order the decode it.
If there were different file encodings on the machines would my program
work?

Any help will be appreciated.

---------------------------------------------------------
Name : Waqas Shafiq
Email : wshafiq@cyberjunkie.com
Home Page: http://www.cyberjunkie.com/wshafiq
---------------------------------------------------------

--------------5B50AB862989BFF9E5CDFB2F
Content-Type: text/html; charset="us-ascii"
X-Sun-Content-Length: 2015

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
I am having trouble encrypting input data obtained from a JTextArea.
Consider the following:

I have made two functions:

    1. public byte[] encrypt(byte[] clearText)

    2. public byte[] decrypt(byte[] cipherText)

Case I [This works]

    ...

    String myText = "This is an example of how dat can be encrypted";

    byte[] cipherText = encrypt(myText.getBytes());

    System.out.println(new String(decrypt(cipherText)));

    ...

Case II [This does not work]

    ...

    JTextArea myTextArea = new JTextArea();

    ...

    I type the same sentence as in Case I.

    ...

    String myText = myTextArea.getText();

    byte[] cipherText = encrypt(myText.getBytes());

    System.out.println(new String(decrypt(cipherText)));

    ...

It appears that there is a problem how text area encodes characters.
The problem disappers when I use StringTokenizer to obtain words (leaving
behind tab spaces, new line etc. and other such characters.) from "myText"
string in Case II.

I would like to store the cipherText to a file as a string
(i.e. String myString = new String(cipherText))
on Win95. Copy the file to a Unix machine, read it and change the
string to byte[] (myString.getBytes()) in order the decode it.
If there were different file encodings on the machines would my program
work?

Any help will be appreciated.

---------------------------------------------------------
Name : Waqas Shafiq
Email : wshafiq@cyberjunkie.com
Home Page: http://www.cyberjunkie.com/wshafiq
---------------------------------------------------------

--------------5B50AB862989BFF9E5CDFB2F--

- Next message: rammohan mrh: "query"
- Previous message: Hemant Dhavala: "problem with writing the generated key to a file"