0

我正在尝试制作一个简单的加密程序,将字符串转换为等效的 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.");
     }
  }
}
4

2 回答 2

2

如果你想要的实际上是任何 java 字符串(基于 UTF-16)的 ASCII 编码(在加密之前?),你可以用base64对其进行编码:这个编码方案就是为此而创建的。

在 java 中进行这种编码/解码真的很容易(就像在其他语言中一样)。

于 2012-09-03T17:01:50.593 回答
1

您似乎想要的是获取表示输入字符串的某种字符编码(不是加密)的字节数组。然后你似乎想显示编码字符的八进制值。如果您只需要 US ASCII,那么您将获得最多 177 个八进制的所有(可打印)字符。如果您需要特殊字符,则需要选择更具体的字符集(IBM OEM 或 Western-Latin 是常见的字符集)。也可以使用 UTF-8,但它可以将单个字符编码为多个字节。

public static String toOctalString(final byte[] encoding) {
    final StringBuilder sb = new StringBuilder(encoding.length * 4);
    for (int i = 0; i < encoding.length; i++) {
        if (i != 0) {
            sb.append("|");
        }
        sb.append(Integer.toOctalString(encoding[i] & 0xFF));
    }
    return sb.toString();
}

public static byte[] fromOctalString(final String octalString) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(octalString.length() / 4 + 1);
    final Matcher m = Pattern.compile("[0-7]{1,3}").matcher(octalString);
    while (m.find()) {
        baos.write(Integer.parseInt(m.group(), 8));
    }
    return baos.toByteArray();
}

public static void main(String[] args) {
    final String userInput = "owlstæd";
    // use the common Latin-1 encoding, standardized in ISO 8859 as character set 1
    // you can replace with ASCII, but the ASCII characters will encode fine for both
    final byte[] userInputEncoded = userInput.getBytes(Charset.forName("ISO8859-1"));
    final String octalString = toOctalString(userInputEncoded); 
    System.out.println(octalString);

    final byte[] userInputEncoded2 = fromOctalString(octalString);
    final String userInput2 = new String(userInputEncoded2, Charset.forName("ISO8859-1"));
    System.out.println(userInput2);
}
于 2012-09-03T23:17:06.237 回答