gpt4 book ai didi

java - RSA 加密在 Java 中不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:15 26 4
gpt4 key购买 nike

我正在研究 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) 行遇到错误说引号是预期的,并且它是表达式的非法开始。我迷路了

最佳答案

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

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

应该是

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

通常最小的语法错误会让您困惑数小时!

关于java - RSA 加密在 Java 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16404399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com