我试图做一个简单的加密程序,将一个字符串转换成等价的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