2012-09-03 3 views
0

문자열을 ASCII 값으로 변환 한 다음 다시 문자열이나 문자열로 해독하는 간단한 암호화 프로그램을 만들려고합니다. 무엇을 당신이하려는 것은 사실 (? 암호화 전) 인코딩 자바 문자열 (UTF-16 기준)의 ASCII에서 인 경우어떻게 문자열을 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 코드 단위의 유니 코드 값을 줄 것입니다 ... ASCII를 넘어서 생각하십시오.) –

+2

무엇이 당신의 질문입니까? –

+0

어떻게 사용자가 지정한 문자열을 ASCII 값 (암호화)으로 변경 한 다음 값 (예 : hello = 104101108108111)을 가져 와서 문자 (암호 해독)로 변경할 수 있습니까? – user1644257

답변

2

, 당신은 base64에 인코딩 수 있습니다이 인코딩 체계를 만들었습니다 단지 그것을 위해서.

이 인코딩/디코딩을 java (다른 언어와 마찬가지로)로 수행하려면 really easy입니다.

+0

고마움 많은 남자, 나는 이것을 끝내기를 정말로 강조했다. 이제 base64 사용 방법을 찾으려고 노력할 것입니다. 다시 한 번 감사드립니다 – user1644257

+0

base64 문자열은 단지 문자 집합 만 줄인 문자열입니다 (따라서 문자열은 약 30 % 더 길어집니다). 대부분의 용도로 이스케이프 할 필요없이 임의의 문자열로 사용할 수 있습니다. –

+0

질문과 코드를 올바르게 읽으십시오. 원하시는 내용이 아닙니다. –

1

입력 문자열의 일종의 문자 인코딩 (암호화가 아님)을 나타내는 바이트 배열을 얻는 것이 좋습니다. 그런 다음 인코딩 된 문자의 8 진수 값을 표시하려고합니다. US ASCII가 필요한 경우 모든 (인쇄 ​​가능) 문자를 최대 177 8 진수로 가져옵니다. 특수 문자를 원하면보다 구체적인 문자 집합을 선택해야합니다 (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); 
} 
관련 문제