2011-10-30 48 views
5

我真的需要知道你將如何在Java代碼中這樣或類似的東西: http://www.cs.carleton.edu/faculty/adalal/teaching/f05/107/applets/ascii.html編程密碼程序(純文本 - >凱撒密碼 - > ASCII)?

這是我嘗試我一直在這整整一天(​​直譯),不得不對如何做搜索在互聯網上,因爲但是因爲我的Java知識不是很好,我不明白任何(在今天的開始,我不知道任何與數組有關),我需要的只是一點幫助或在右邊推方向。

抱歉忘了指出問題。我遇到的麻煩不在於轉換和加密PlainText,而是試圖將編碼的消息(當然是用我的程序加密)轉換爲PlainText(即,我不能僅僅通過我的程序中的變量來反轉它,實際上我必須能夠讀取並解碼它)

private void encryptBUTActionPerformed(java.awt.event.ActionEvent evt)  
{           
    int encryptLength=encryptTXT.getText().length(); 
    int[] anArray=new int[encryptLength]; 
    String key=encryptKey.getText(); 
    if(key.isEmpty()) 
    { 
     decryptTXT.setText(""+"INVALID KEY"); 
    } 
    else 
    { 
     int key2=Integer.parseInt(key); 
     for(int i=0;i<encryptLength;i++) 
     { 
      int letter = encryptTXT.getText().toLowerCase().charAt(i); 
      System.out.println(letter); 
      System.out.println((char)letter); 
      int letterCiphered= (letter-key2); 
      anArray[i]=letterCiphered; 
     } 
     String output=(Arrays.toString(anArray)); 
     decryptTXT.setText(output); 
    } 
}           

private void clearBUTActionPerformed(java.awt.event.ActionEvent evt)    
{           
mainPassword.setText(""); 
encryptTXT.setText(""); 
decryptTXT.setText(""); 
encryptKey.setText(""); 
decryptKey.setText(""); 
}           

private void decryptBUTActionPerformed(java.awt.event.ActionEvent evt)  
{           
    int textLength=decryptTXT.getText().length(); 
    ArrayList list=new ArrayList(); 
    String text=decryptTXT.getText(); 
    int count=1; 
    String key=decryptKey.getText(); 
    if(key.isEmpty()) 
    { 
     encryptTXT.setText(""+"INVALID KEY"); 
    } 
    else 
    { 
     int key2=Integer.parseInt(key); 
     for(int i=0;i<textLength;i++) 
     { 
      if(text.charAt(i)=='['||text.charAt(i)==','||text.charAt(i)==']') 
      { 
       count=count+1; 
      } 
      else if(count%2==0) 
      { 
       char number=text.charAt(i); 
       char number2=text.charAt(i+1); 
       int num=(int)number; 
       int num2=(int)number2; 
       int num3=num; 
       int num4=num3+num2-15; 
       int num5=num4+key2; 
       char letter2=(char)num5; 
       list.add(letter2); 
       count=count+1; 
      } 
      else 
      { 

      } 
     } 
     Object[] obj=(list.toArray()); 
     String out=Arrays.toString(obj); 
     encryptTXT.setText(out); 
    } 
} 
+4

Java是**不是** JavaScript。 –

+0

編輯:刪除了截止日期,因爲這與編碼問題沒有密切關係。 –

+0

滑鼠的對不起,夥計並不意味着得罪。 – ChaseVsGodzilla

回答

0

看來,您的加密只是將消息的每個字符移動一個給定的值。

int letterCiphered= (letter-key2); 

要解密這樣的消息應該由相同的值,但在其它方向上的密碼的每個字符移位。

int letter= (letterCiphered+key2); 

你在代碼中的解密函數完全是另一回事。

更新後的評論:

如果我理解正確的話,你想的ASCII值的字符串轉換爲它們所代表的文本。

解析輸入字符串,如果它是一個簡單的格式,你可以只使用String.split()閱讀它。
要將數字的文本表示轉換爲實際的數字,您可以使用Integer.parseInt()
最後一個整數ASCII值轉交給你只需要投char c = (char)97;

解析輸入識別每個ASCII值,每個值轉換爲整數,並將其轉換爲一個字符一個字符,然後正好連接字符到一個字符串。

+0

是的,它是一個愷撒密碼,但隨後的字母轉換爲它們對應的ASCII值,並存儲在該數組中,然後在我的JTextField顯示和對不起,當我發佈這個我知道如何解碼它的時候,我正在忙着解決一些想法。我的問題不是實際的解密,它更多地關於如何從輸入的文本中獲取ASCII值對,並將其轉換爲文本(字符),如果您按照URL查看我的意思,它不會對文本進行加密,但是它將char轉換爲ASCII並返回。通過簡單地將它們轉換爲字符,可以將 – ChaseVsGodzilla

+0

字節轉換爲字符:(char)。或者一個字符串可以通過'new String(myByteArray)'從一個字節數組中形成。也許你想使用一個字節數組而不是一個int數組。 –

+0

我剛纔讀了一些關於String方法的模式匹配,看起來很有希望'match()'和'replace()'這些可能是一種可能性,因爲我說我並不真正理解它,儘管如此我無法實現它。 – ChaseVsGodzilla

1

解決

這是我如何加密我的文字:

int encryptLength=encryptTXT.getText().length(); 
String key=encryptKey.getText(); 
String text=""; 
if(key.isEmpty()) 
{ 
    decryptTXT.setText(""+"INVALID KEY"); 
} 
else 
{ 
    int key2=Integer.parseInt(key); 
    for(int i=0;i<encryptLength;i++) 
    { 
     int letter = encryptTXT.getText().toLowerCase().charAt(i); 
     int letterCiphered = (letter-key2); 
     text=text+letterCiphered+" "; 
    } 
    decryptTXT.setText(""+text); 
} 

這是我如何解密我的文字

try 
{ 
String text=decryptTXT.getText(); 
String key=decryptKey.getText(); 
String[] decrypt=text.split(" "); 
String sentance=""; 
if(key.isEmpty()) 
{ 
    encryptTXT.setText(""+"INVALID KEY"); 
} 
else 
{ 
    int key2=Integer.parseInt(key); 
    for(int i=0;i<decrypt.length;i++) 
    { 
     int number=Integer.parseInt(decrypt[i]); 
     char letter=(char)(number+key2); 
     sentance=sentance+letter; 
    } 
    encryptTXT.setText(""+sentance); 
} 
} 
catch(NumberFormatException e) 
{ 
    encryptTXT.setText(""+"Please enter a valid encoded message"); 
} 

感謝所有幫幫我 結果比我想象的要簡單得多。

-3
package cipher; 

import java.io.*; 
import java.util.Scanner; 

public class Cipher { 

    public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; 
    public static final String ADDRESS = "C:\\Users\\abhati\\Documents\\NetBeansProjects\\Cipher\\encode.txt"; 
    public static final String LOCATE = "C:\\Users\\abhati\\Documents\\NetBeansProjects\\Cipher\\decode.enc"; 


    public static int MenuOption() throws Exception 
    { 
     int option; 
do{ 

     System.out.println(" +++++++++++++++++++\n + 1. Encrypt +"); 
     System.out.println(" +++++++++++++++++++\n + 2. Decrypt +"); 
     System.out.println(" +++++++++++++++++++\n + 3. Quit  + \n +++++++++++++++++++"); 
     System.out.println("\nWhat would you like to do?"); 
     Scanner input = new Scanner(System.in); 
     option = input.nextInt(); 

     switch(option) 
     { 
      case 1:  encrypt(); 
         break; 

      case 2:  decrypt(); 
         break;  

      case 3:  System.exit(0); 
         break; 
      default: System.out.println("WRONG INPUT ??? PLEASE SELECT THE OPTION \n"); 
     } 

    } while(option != 3); 
       return option; 
} 

    public static void main(String[] args) throws Exception 
    { 
     System.out.println("Hello to Change Your text into cipher text!!!!\n"); 
     System.out.println("Put your text file in root folder.\n"); 
     MenuOption(); 
     System.out.println(); 
    } 


    public static void encrypt() throws Exception { 

    String ex; 
    FileReader in=null; 
    FileWriter out = null; 
     try { 

      File file = new File(ADDRESS); 
      in = new FileReader(file); 
      file.getName(); 

      ex=getFileExtensionE(file); 

      if(ex.equals("txt")) 
     { 
     out = new FileWriter(LOCATE); 

     Scanner input2 = new Scanner(System.in); 
     System.out.println("What is the value for the key?"); 
     int key = input2.nextInt(); 


     Scanner input3 = new Scanner(in); 
     while (input3.hasNext()) { 
     String line = input3.nextLine(); 
     line = line.toLowerCase(); 
     String crypt = ""; 

     for (int i = 0; i < line.length(); i++) { 
     int position = ALPHABET.indexOf(line.charAt(i)); 
     int keyValue = (key + position) % 26; 
     char replace = ALPHABET.charAt(keyValue); 
     crypt += replace; 
     } 
     out.write(""+crypt); 
     System.out.println(""+crypt); 
     } 
     input3.close(); 
     System.out.println("\nDATA ENCRYPTED\n"); 
     } 
     else 
     { 
     System.out.println("DOESN'T ENCRYPTED!"); 
       } 
    } catch(Exception e) 
    { 
     System.out.println("\n NO FILE FOUND WITH THIS NAME!!!"); 
    } 
     finally { 
     if (in != null) { 
      in.close(); 
     } 
     if (out != null) { 
      out.close(); 
     } 
     } 
} 

    public static void decrypt() throws Exception { 

     String ex; 
     FileReader in = null; 
     FileWriter out = null; 
     try { 

     File file = new File(LOCATE); 
     in = new FileReader(file); 
     file.getName(); 

     ex=getFileExtensionD(file); 

      if(ex.equals("enc")) 
     { 
     out = new FileWriter("encrpted_DATA.txt"); 

     Scanner input4 = new Scanner(System.in); 
     System.out.println("What is the value for the key?"); 
     int key = input4.nextInt();      

     Scanner input5 = new Scanner(in); 
     while (input5.hasNext()) { 
     String line = input5.nextLine(); 
     line = line.toLowerCase(); 
     String decrypt = ""; 

     for (int i = 0; i < line.length(); i++) { 
     int position = ALPHABET.indexOf(line.charAt(i)); 
     int keyValue = (position - key) % 26; 
     if (keyValue < 0) 
      { 
       keyValue = ALPHABET.length() + keyValue; 
      } 
     char replace = ALPHABET.charAt(keyValue); 
     decrypt += replace; 
     } 
     out.write(""+decrypt); 
     System.out.println(""+decrypt); 
     } 
     input5.close(); 
     System.out.println("\nDATA DECRYPTED\n"); 

     } 
     else 
     { 
      System.out.println("DOESN'T DECRYPTED!"); 
     } 
     } catch(Exception e) 
    { 
     System.out.println("\n NO FILE FOUND WITH THIS NAME!!!"); 
    } 
     finally { 
     if (in != null) { 
      in.close(); 
     } 
     if (out != null) { 
      out.close(); 
     } 
    } 
} 

    public static String getFileExtensionE(File file) { 
    String name = file.getName(); 
    try { 
     return name.substring(name.lastIndexOf(".") + 1); 
    } catch (Exception e) { 
     return ""; 
    } 
} 

    public static String getFileExtensionD(File file) { 
    String name = file.getName(); 
    try { 
     return name.substring(name.lastIndexOf(".") + 1); 
    } catch (Exception e) { 
     return ""; 
    } 
} 
}