2012-09-03 53 views
0

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

你说过你在做什么,给了一些代码......但没有真正的*问题*。 (请注意,'char'到'int'的转换会给你一个UTF-16代码单元的Unicode值......想想超越ASCII。) –

+2

你的问题是什么? –

+0

如何将用户给出的字符串更改为ASCII值(加密),然后将值(例如hello = 104101108108111)更改为其字符(解密)? – user1644257

回答

2

如果你想拥有的东西其实是在任何Java字符串(UTF-16型)的ASCII编码(?加密前),你可能会对其进行编码,在base64:这个编码方案的创建只是为了那个。

这是really easy在java中执行此编码/解码(与其他语言一样)。

+0

非常感谢很多人,我非常强调要完成这件事。我将尝试了解如何使用base64。再次感谢 – user1644257

+0

base64字符串只是一个只有一组缩减的字符的字符串(因此字符串长了大约30%)。您可以使用任何字符串,但无需为大多数用途而转义。 –

+0

如果我阅读你的问题和代码权利dystroy,这是*不*你想要什么。 –

1

你似乎想要得到的字节数组代表输入字符串的某种字符编码(不加密)。那么你似乎想要显示编码字符的八进制值。如果您只需要US ASCII,那么您将获得高达177八进制的所有(可打印)字符。如果您想要特殊字符,您需要选择更具体的字符集(IBM OEM或西方拉丁文是常用字符集)。也可以使用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); 
} 
相关问题