0

我正在研究 RSA 算法并取得了一些进展,但它不起作用。我有以下代码。

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++;

        }

//开始字符串处理 //循环等

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

}

当我构建文件时,我在行中收到错误,for (int i = 0, i < plainText.length(); i = i+2)指出需要引号并且它是表达式的非法开头。我迷路了

4

1 回答 1

3

你有一个逗号而不是一个分号。

for (int i = 0, i < plainText.length(); i = i+2)

应该

for (int i = 0; i < plainText.length(); i = i+2)

通常最小的语法错误会让你困惑好几个小时!

于 2013-05-06T17:59:43.163 回答