# Thread: Using Java To Implement RSA Algorithm

1. ## Using Java To Implement RSA Algorithm

Hi I'm looking for SOME advice on how to simulate the following:

http://i71.photobucket.com/albums/i1...pinate/RSA.jpg

http://i71.photobucket.com/albums/i1...inate/RSA2.jpg

Ive done this so far:

Java Code:
```package RSAalgorithm;

public class SecurityAlgorithm {

public static char[] StartSymbolic = {'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'};

public static int[] numeric = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26};

public static void main(String[] args) {

long P3 = 0;
long P3mod33=0;
long C7=0;
long C7mod33=0;

for(int counter = 0; counter < numeric.length; counter++){

for(int i = 0; i < StartSymbolic.length; i++)  {

int temp = numeric[counter];
P3 = (long) (temp*temp*temp);
P3mod33 = P3%33;
C7 = (long) (P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33);
int tp = numeric[counter];
C7mod33 = C7%33;
}

System.out.println("*******************************************");
//System.out.println(StartSymbolic[i]);
System.out.println("Numeric: " + numeric[counter]);
System.out.println("P3: " +P3);
System.out.println("P3mod33: " + P3mod33);
System.out.println("C7: " +C7);
System.out.println("C7mod33:" + C7mod33);
//  System.out.println(StartSymbolic[i]);
System.out.println("*******************************************");

}

}
}```

Java Code:
```Output:
*******************************************
Numeric: 1
P3: 1
P3mod33: 1
C7: 1
C7mod33:1
*******************************************
*******************************************
Numeric: 2
P3: 8
P3mod33: 8
C7: 2097152
C7mod33:2
*******************************************
*******************************************
Numeric: 3
P3: 27
P3mod33: 27
C7: 10460353203
C7mod33:3
*******************************************
*******************************************
Numeric: 4
P3: 64
P3mod33: 31
C7: 27512614111
C7mod33:4
*******************************************
*******************************************
Numeric: 5
P3: 125
P3mod33: 26
C7: 8031810176
C7mod33:5
*******************************************
*******************************************
Numeric: 6
P3: 216
P3mod33: 18
C7: 612220032
C7mod33:6
*******************************************
*******************************************
Numeric: 7
P3: 343
P3mod33: 13
C7: 62748517
C7mod33:7
*******************************************
*******************************************
Numeric: 8
P3: 512
P3mod33: 17
C7: 410338673
C7mod33:8
*******************************************
*******************************************
Numeric: 9
P3: 729
P3mod33: 3
C7: 2187
C7mod33:9
*******************************************
*******************************************
Numeric: 10
P3: 1000
P3mod33: 10
C7: 10000000
C7mod33:10
*******************************************
*******************************************
Numeric: 11
P3: 1331
P3mod33: 11
C7: 19487171
C7mod33:11
*******************************************
*******************************************
Numeric: 12
P3: 1728
P3mod33: 12
C7: 35831808
C7mod33:12
*******************************************
*******************************************
Numeric: 13
P3: 2197
P3mod33: 19
C7: 893871739
C7mod33:13
*******************************************
*******************************************
Numeric: 14
P3: 2744
P3mod33: 5
C7: 78125
C7mod33:14
*******************************************
*******************************************
Numeric: 15
P3: 3375
P3mod33: 9
C7: 4782969
C7mod33:15
*******************************************
*******************************************
Numeric: 16
P3: 4096
P3mod33: 4
C7: 16384
C7mod33:16
*******************************************
*******************************************
Numeric: 17
P3: 4913
P3mod33: 29
C7: 17249876309
C7mod33:17
*******************************************
*******************************************
Numeric: 18
P3: 5832
P3mod33: 24
C7: 4586471424
C7mod33:18
*******************************************
*******************************************
Numeric: 19
P3: 6859
P3mod33: 28
C7: 13492928512
C7mod33:19
*******************************************
*******************************************
Numeric: 20
P3: 8000
P3mod33: 14
C7: 105413504
C7mod33:20
*******************************************
*******************************************
Numeric: 21
P3: 9261
P3mod33: 21
C7: 1801088541
C7mod33:21
*******************************************
*******************************************
Numeric: 22
P3: 10648
P3mod33: 22
C7: 2494357888
C7mod33:22
*******************************************
*******************************************
Numeric: 23
P3: 12167
P3mod33: 23
C7: 3404825447
C7mod33:23
*******************************************
*******************************************
Numeric: 24
P3: 13824
P3mod33: 30
C7: 21870000000
C7mod33:24
*******************************************
*******************************************
Numeric: 25
P3: 15625
P3mod33: 16
C7: 268435456
C7mod33:25
*******************************************
*******************************************
Numeric: 26
P3: 17576
P3mod33: 20
C7: 1280000000
C7mod33:26
*******************************************```

I was looking to achieve what is shown in the 1st image.

I can get the numeric, P3, P3mod33, C7, C7mod33 to print out but not the symbolic information for some reason?

I want to be able to allow the user to enter in a string then the plaintext and ciphertext can be displayed.

Like this:

String input: SUZANNE

Output:

Plaintext: S U Z A N N E
Ciphertext: 28 21 20 1 5 5 26

Thanks.
Last edited by Floetic; 03-22-2008 at 01:44 AM.

2. Member
Join Date
Feb 2008
Location
Oregon, USA
Posts
49
Rep Power
0
Okay, first of all, I would probably advise you line up your tabs logically to make your code easier to read. As for your error, I noticed that your StartSymbolic lines were commented, but I'm guessing you did that so the program would compile for the temporary, even though they should work anyways.

I noticed you used the 'int' declaration inside of for statements. This is bad because you are recreating the variables over and over and can cause error, so you want to initialize everything outside of any for, while, if, try, catch, or any other block statement.

Java Code:
```package RSAalgorithm;

public class SecurityAlgorithm {

public static final char[] StartSymbolic = {'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'};

public static final int[] numeric = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26};

private static final String separator = "*******************************************";

public static void main(String[] args) {
long P3 = 0;
long P3mod33 = 0;
long C7 = 0;
long C7mod33 = 0;
int temp = 0;

for(int counter = 0; counter < numeric.length; counter++){
for(int i = 0; i < StartSymbolic.length; i++)  {
temp = numeric[counter];
P3 = (long) (temp * temp * temp);
P3mod33 = P3 % 33;
C7 = (long) (P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33);
tp = numeric[counter];
C7mod33 = C7 % 33;
}

System.out.println(separator);
// System.out.println(StartSymbolic[i]);
System.out.println("Numeric: " + numeric[counter]);
System.out.println("P3: " +P3);
System.out.println("P3mod33: " + P3mod33);
System.out.println("C7: " +C7);
System.out.println("C7mod33: " + C7mod33);
// System.out.println(StartSymbolic[i]); // THESE are the commented lines, remove the double slash before System.out.println and it should work
System.out.println(separator);
}
}
}```
I tabbed your lines more like what the Java conventions would suggest, I don't understand your random spacing, no offense. Personally, I don't tab my lines, they are all aligned to the left. And again, I added your temp and tp declarations to the end of your long declarations, and before your for statements. See if the above code works, also, if what you wanted was the letter to show up, uncomment one of the commented lines in the section where you print output.

3. Cheers it looks much neater and im using Netbeans IDE 6.0.1 it didnt like the StartSymbolic[i] so I changed to StartSymbolic[index] but by doing that the output the beginning and ending symbol should match each other but they're not!?!

Java Code:
```package newpackage2;

public class RSA {

public static final char[] StartSymbolic = {'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'};

public static final int[] numeric = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26};

private static final String separator = "*******************************************";

public static void main(String[] args) {
long P3 = 0;
long P3mod33 = 0;
long C7 = 0;
long C7mod33 = 0;
int temp = 0;
int tp=0;
int index=0;

for(int counter = 0; counter < numeric.length; counter++){
for(int i = 0; i < StartSymbolic.length; i++)  {
temp = numeric[counter];
P3 = (long) (temp * temp * temp);
P3mod33 = P3 % 33;
C7 = (long) (P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33);
tp = numeric[counter];
C7mod33 = C7 % 33;
}

System.out.println(separator);
System.out.println("Beginning Symbol: " +StartSymbolic[index]++);
System.out.println("Numeric: " + numeric[counter]);
System.out.println("P3: " +P3);
System.out.println("P3mod33: " + P3mod33);
System.out.println("C7: " +C7);
System.out.println("C7mod33: " + C7mod33);
System.out.println("Ending Symbol: " +StartSymbolic[index++]);
// THESE are the commented lines, remove the double slash before System.out.println and it should work
System.out.println(separator);
}
}
}```

Java Code:
```*******************************************
Beginning Symbol: A
Numeric: 1
P3: 1
P3mod33: 1
C7: 1
C7mod33: 1
Ending Symbol: B
*******************************************
*******************************************
Beginning Symbol: B
Numeric: 2
P3: 8
P3mod33: 8
C7: 2097152
C7mod33: 2
Ending Symbol: C
*******************************************
*******************************************
Beginning Symbol: C
Numeric: 3
P3: 27
P3mod33: 27
C7: 10460353203
C7mod33: 3
Ending Symbol: D
*******************************************
*******************************************
Beginning Symbol: D
Numeric: 4
P3: 64
P3mod33: 31
C7: 27512614111
C7mod33: 4
Ending Symbol: E
*******************************************
*******************************************
Beginning Symbol: E
Numeric: 5
P3: 125
P3mod33: 26
C7: 8031810176
C7mod33: 5
Ending Symbol: F
*******************************************
*******************************************
Beginning Symbol: F
Numeric: 6
P3: 216
P3mod33: 18
C7: 612220032
C7mod33: 6
Ending Symbol: G
*******************************************
*******************************************
Beginning Symbol: G
Numeric: 7
P3: 343
P3mod33: 13
C7: 62748517
C7mod33: 7
Ending Symbol: H
*******************************************
*******************************************
Beginning Symbol: H
Numeric: 8
P3: 512
P3mod33: 17
C7: 410338673
C7mod33: 8
Ending Symbol: I
*******************************************
*******************************************
Beginning Symbol: I
Numeric: 9
P3: 729
P3mod33: 3
C7: 2187
C7mod33: 9
Ending Symbol: J
*******************************************
*******************************************
Beginning Symbol: J
Numeric: 10
P3: 1000
P3mod33: 10
C7: 10000000
C7mod33: 10
Ending Symbol: K
*******************************************
*******************************************
Beginning Symbol: K
Numeric: 11
P3: 1331
P3mod33: 11
C7: 19487171
C7mod33: 11
Ending Symbol: L
*******************************************
*******************************************
Beginning Symbol: L
Numeric: 12
P3: 1728
P3mod33: 12
C7: 35831808
C7mod33: 12
Ending Symbol: M
*******************************************
*******************************************
Beginning Symbol: M
Numeric: 13
P3: 2197
P3mod33: 19
C7: 893871739
C7mod33: 13
Ending Symbol: N
*******************************************
*******************************************
Beginning Symbol: N
Numeric: 14
P3: 2744
P3mod33: 5
C7: 78125
C7mod33: 14
Ending Symbol: O
*******************************************
*******************************************
Beginning Symbol: O
Numeric: 15
P3: 3375
P3mod33: 9
C7: 4782969
C7mod33: 15
Ending Symbol: P
*******************************************
*******************************************
Beginning Symbol: P
Numeric: 16
P3: 4096
P3mod33: 4
C7: 16384
C7mod33: 16
Ending Symbol: Q
*******************************************
*******************************************
Beginning Symbol: Q
Numeric: 17
P3: 4913
P3mod33: 29
C7: 17249876309
C7mod33: 17
Ending Symbol: R
*******************************************
*******************************************
Beginning Symbol: R
Numeric: 18
P3: 5832
P3mod33: 24
C7: 4586471424
C7mod33: 18
Ending Symbol: S
*******************************************
*******************************************
Beginning Symbol: S
Numeric: 19
P3: 6859
P3mod33: 28
C7: 13492928512
C7mod33: 19
Ending Symbol: T
*******************************************
*******************************************
Beginning Symbol: T
Numeric: 20
P3: 8000
P3mod33: 14
C7: 105413504
C7mod33: 20
Ending Symbol: U
*******************************************
*******************************************
Beginning Symbol: U
Numeric: 21
P3: 9261
P3mod33: 21
C7: 1801088541
C7mod33: 21
Ending Symbol: V
*******************************************
*******************************************
Beginning Symbol: V
Numeric: 22
P3: 10648
P3mod33: 22
C7: 2494357888
C7mod33: 22
Ending Symbol: W
*******************************************
*******************************************
Beginning Symbol: W
Numeric: 23
P3: 12167
P3mod33: 23
C7: 3404825447
C7mod33: 23
Ending Symbol: X
*******************************************
*******************************************
Beginning Symbol: X
Numeric: 24
P3: 13824
P3mod33: 30
C7: 21870000000
C7mod33: 24
Ending Symbol: Y
*******************************************
*******************************************
Beginning Symbol: Y
Numeric: 25
P3: 15625
P3mod33: 16
C7: 268435456
C7mod33: 25
Ending Symbol: Z
*******************************************
*******************************************
Beginning Symbol: Z
Numeric: 26
P3: 17576
P3mod33: 20
C7: 1280000000
C7mod33: 26
Ending Symbol: [
*******************************************```

4. Member
Join Date
Feb 2008
Location
Oregon, USA
Posts
49
Rep Power
0
There are two ways you can fix this:

You could use counter for both (see comments for explanation)
Java Code:
```package newpackage2;

public class RSA {

public static final char[] StartSymbolic = {'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'};

public static final int[] numeric = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26};

private static final String separator = "*******************************************";

public static void main(String[] args) {
long P3 = 0;
long P3mod33 = 0;
long C7 = 0;
long C7mod33 = 0;
int temp = 0;
int tp=0;
int index=0;

for(int counter = 0; counter < numeric.length; counter++){
for(int i = 0; i < StartSymbolic.length; i++)  {
temp = numeric[counter];
P3 = (long) (temp * temp * temp);
P3mod33 = P3 % 33;
C7 = (long) (P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33);
tp = numeric[counter];
C7mod33 = C7 % 33;
}

System.out.println(separator);
System.out.println("Beginning Symbol: " +StartSymbolic[counter]); // The ++ is not necessary here, so I removed it. (putting a + here concatenates strings)
System.out.println("Numeric: " + numeric[counter]); /* Here I saw that
numeric and StartSymbolic matched up (1-A, 2-B, etc.) so counter could be
used for both of them */
System.out.println("P3: " +P3);
System.out.println("P3mod33: " + P3mod33);
System.out.println("C7: " +C7);
System.out.println("C7mod33: " + C7mod33);
System.out.println("Ending Symbol: " +StartSymbolic[counter]);
System.out.println(separator);
}
}
}```
or (if that doesn't work, like StartSymbolic[i])
Java Code:
```package newpackage2;

public class RSA {

public static final char[] StartSymbolic = {'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'};

public static final int[] numeric = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26};

private static final String separator = "*******************************************";

public static void main(String[] args) {
long P3 = 0;
long P3mod33 = 0;
long C7 = 0;
long C7mod33 = 0;
int temp = 0;
int tp=0;
int index=0;

for(int counter = 0; counter < numeric.length; counter++){
for(int i = 0; i < StartSymbolic.length; i++)  {
temp = numeric[counter];
P3 = (long) (temp * temp * temp);
P3mod33 = P3 % 33;
C7 = (long) (P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33*P3mod33);
tp = numeric[counter];
C7mod33 = C7 % 33;
}

System.out.println(separator);
System.out.println("Beginning Symbol: " +StartSymbolic[index]); // remove ++
System.out.println("Numeric: " + numeric[counter]);
System.out.println("P3: " +P3);
System.out.println("P3mod33: " + P3mod33);
System.out.println("C7: " +C7);
System.out.println("C7mod33: " + C7mod33);
System.out.println("Ending Symbol: " +StartSymbolic[index]); // don't increment here
System.out.println(separator);
index++; // increment separately, I never like incrementing on the spot, this way gives you more control
}
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•