Reputation: 437
I am working on the RSA algorithm and have made some progress but it is not working. I have the following code.
public class RSA
{
public static void main(String args[])
{
String plainText = "ITS ALL GREEK TO ME";//temp local plaintext ignoring parameter
plainText = plainText.toUpperCase();
plainText = plainText.replaceAll("\\s","");
System.out.println(plainText);
String cipherText = "";
int p = 47; //has been provided
int q = 59; //has been provided
int d = 157; //has been provided
int n = p * q;//calculate n
int w = (p - 1)*(q - 1);// calculate W
int c = 0;//we will compute this = cipher variable
int m = 920;//hardcoded for now
int e = 17;//hardcoded for now
//start of euclids
ArrayList<Integer> remainderlist=new ArrayList<Integer>();//array list to hold the remainder values in euclids algorithm from the quotient
remainderlist.add(w);// step 1 of euclids algorithm starting with value of w as above
remainderlist.add(d);// step 2 of euclids algorithm to add value of d
int remainderposition1 = 0;
int remainderposition2 = 1;
int quotient = 0;
int remainder = 0;
while (remainderlist.get(remainderposition1)%(remainderlist.get(remainderposition2))>0){
quotient = remainderlist.get(remainderposition1)/remainderlist.get(remainderposition2);
remainder = remainderlist.get(remainderposition1)%remainderlist.get(remainderposition2);
remainderlist.add(remainder);
System.out.println("Q: " +quotient);
System.out.println("R: " +remainder);
System.out.println("");
remainderposition1++;
remainderposition2++;
}
//start string processing //loop etc
if (plainText.length()%2!=0)
{
plainText = plainText + " ";
}
for (int i = 0, i < plainText.length(); i = i+2)
{
char plTChar1 = plainText.CharAt(i);
char plTChar2 = plainText.CharAt(i + 1);
// Convert the character into an ASCII table value.
int asciiValue1 = (int) plTChar1;
int asciiValue2 = (int) plTChar2;
String numStr1 = asciiValue1.parseInt;
String numStr2 = asciiValue2.parseInt;
if (numStr.length() < 2)
{
numStr = "0" + numStr;
}
String fullNum = numStr1 + numStr2;
int m = Integer.parse(fullNum);
//start of encryption algorithm
String binarystring = Integer.toBinaryString(e);// step 1
System.out.println(binarystring);
c = 1; // step 2 of the encryption algorithm - notes
for (int i = 0; i<binarystring.length();i++)
{
c = (c*c)%n;// setp 3a
System.out.println(binarystring.charAt(i));
// step 3b notes
if (binarystring.charAt(i)=='1') {
c = (c*m)%n;
}
}
System.out.println("Cipher"+c);
}
When I build the file I get errors on the line for (int i = 0, i < plainText.length(); i = i+2)
saying that quotation marks are expected and that it is an illegal start to an expression. I'm lost
Upvotes: 0
Views: 322
Reputation: 27336
You have a comma instead of a semi-colon.
for (int i = 0, i < plainText.length(); i = i+2)
Should be
for (int i = 0; i < plainText.length(); i = i+2)
Often the smallest syntax error will confuse you for hours!
Upvotes: 3