我試圖做一個簡單的加密程序,將一個字符串轉換成等價的ASCII值,然後再解密爲字符串或char。如何將字符串更改爲ascii值並返回字符串?
import java.io.*;
import javax.swing.*;
public class SimpleEncryption {
public static void main (String [] args) throws Exception
{
BufferedReader inKb = new BufferedReader (new InputStreamReader (System.in));
for(int i=0; i<10; i++)
{
String ans = JOptionPane.showInputDialog ("Hello User, would you like to encrypt or decrypt?");
ans = ans.toUpperCase();
int a = 0;
if (ans.contains("EN")||ans.contains("ENCRYPT"))
{
String pass = "";
pass = JOptionPane.showInputDialog ("Please type your phrase into input:");
for (int j=0; j<pass.length(); j++)
{
char c = pass.charAt(j);
a = (int) c;
System.out.print(a);
}
break;
}
if (ans.contains("DE")||ans.contains("DECRYPT"))
{
String pass = "";
pass = JOptionPane.showInputDialog ("Please type the encrypted code into input:");
for (int k=0; k<pass.length(); k++)
{
char c = pass.charAt(k);
a = (int)(c);
i = (char) a;
System.out.print(a);
}
break;
}
System.out.println("Sorry I don't understand, please retry.");
}
}
}
你說過你在做什麼,給了一些代碼......但沒有真正的*問題*。 (請注意,'char'到'int'的轉換會給你一個UTF-16代碼單元的Unicode值......想想超越ASCII。) –
你的問題是什麼? –
如何將用戶給出的字符串更改爲ASCII值(加密),然後將值(例如hello = 104101108108111)更改爲其字符(解密)? – user1644257