2012-09-03 94 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); 
} 
相關問題