将具有 ASCII 值的字符串存储到数组中并使用 RSA 密钥
Storing a String with ASCII values into an Array and use RSA key
我对编码还很陌生。我正在尝试接受用户输入,将他们的文本更改为 ASCII 值,然后使用我创建的 RSA 密钥对其进行加密。
目前我有这个代码:
public class RSA {
public static void main(String args[]){
int p = 11;
int q = 17;
int n = p*q; //187
int phi = (p-1)*(q-1); //160
int e = 7;
int d = 23;
Scanner input = new Scanner(System.in);
System.out.println("Please enter your message: ");
String clearMsg = input.nextLine();
String trimmedClearMsg = new String(clearMsg.trim());
StringBuilder sb = new StringBuilder();
for (char c : trimmedClearMsg.toCharArray())
sb.append(String.format("%03d",(int)c));
BigInteger asciiMsg = new BigInteger(sb.toString());
System.out.println(asciiMsg); /*i print this out to check i have transferred to ASCII */
byte[] asciiArray = asciiMsg.toByteArray();
for (int i = 0; i < asciiArray.length; i++) {
double keyCipher = Math.pow(i, e)%n;
System.out.println(keyCipher);
}
}
当前输出为:
请输入您的留言:
你好
104101108108111(这是 hello 的 ASCII 值)
0.0
1.0
128.0
130.0
115.0
146.0
我不确定为什么要打印这些数字,我期待 104101108108111 的加密版本。
任何指导将不胜感激!! :)
这有效(假设您的 RSA 详细信息正确):
public class RSA {
public static void main(String args[]) {
int p = 11, q = 17;
int n = p * q; //187
int phi = (p - 1) * (q - 1); //160
int e = 7, d = 23;
Scanner input = new Scanner(System.in);
System.out.print("Please enter your message: ");
String msg = input.nextLine().trim();
byte[] msgBytes = msg.getBytes();
byte[] encryptedBytes = new byte[msgBytes.length];
for (int i = 0; i < msgBytes.length; i++) {
//below works only because n==187
encryptedBytes[i] = (byte) (Math.pow(msgBytes[i], e) % n);
}
System.out.println("Encrypted string : " + new String(encryptedBytes));
System.out.println("Encrypted string base64 : " + new String(Base64.getEncoder().encode(encryptedBytes)));
}
}
请注意它使用 Base64
从 Java 8 开始可用。
我对编码还很陌生。我正在尝试接受用户输入,将他们的文本更改为 ASCII 值,然后使用我创建的 RSA 密钥对其进行加密。 目前我有这个代码:
public class RSA {
public static void main(String args[]){
int p = 11;
int q = 17;
int n = p*q; //187
int phi = (p-1)*(q-1); //160
int e = 7;
int d = 23;
Scanner input = new Scanner(System.in);
System.out.println("Please enter your message: ");
String clearMsg = input.nextLine();
String trimmedClearMsg = new String(clearMsg.trim());
StringBuilder sb = new StringBuilder();
for (char c : trimmedClearMsg.toCharArray())
sb.append(String.format("%03d",(int)c));
BigInteger asciiMsg = new BigInteger(sb.toString());
System.out.println(asciiMsg); /*i print this out to check i have transferred to ASCII */
byte[] asciiArray = asciiMsg.toByteArray();
for (int i = 0; i < asciiArray.length; i++) {
double keyCipher = Math.pow(i, e)%n;
System.out.println(keyCipher);
}
}
当前输出为: 请输入您的留言: 你好 104101108108111(这是 hello 的 ASCII 值) 0.0 1.0 128.0 130.0 115.0 146.0
我不确定为什么要打印这些数字,我期待 104101108108111 的加密版本。
任何指导将不胜感激!! :)
这有效(假设您的 RSA 详细信息正确):
public class RSA {
public static void main(String args[]) {
int p = 11, q = 17;
int n = p * q; //187
int phi = (p - 1) * (q - 1); //160
int e = 7, d = 23;
Scanner input = new Scanner(System.in);
System.out.print("Please enter your message: ");
String msg = input.nextLine().trim();
byte[] msgBytes = msg.getBytes();
byte[] encryptedBytes = new byte[msgBytes.length];
for (int i = 0; i < msgBytes.length; i++) {
//below works only because n==187
encryptedBytes[i] = (byte) (Math.pow(msgBytes[i], e) % n);
}
System.out.println("Encrypted string : " + new String(encryptedBytes));
System.out.println("Encrypted string base64 : " + new String(Base64.getEncoder().encode(encryptedBytes)));
}
}
请注意它使用 Base64
从 Java 8 开始可用。