2017-03-07 15 views
1

私はウェブを見ていて、特にここで役立つものがたくさんあると知っています。私は近くにいますが、解読バイトを理解できません。バイト配列に関数の負の値を送るので、Decryptは機能しませんか?RC4復号化Java

import javax.xml.bind.DatatypeConverter; 
public class RC4_Main { 

public static int[] myKSA(String key) { 
    int j = 0, temp = 0; 
    int[] S = new int[256]; 
    int[] T = new int[256]; 
    int[] K = new int[key.length()]; 
    for (int a = 0; a < key.length(); a++) { 
     K[a] = key.charAt(a); 
    } 
    int keyLength = key.length(); 
    // Generation of the S-Box 
    for (int a = 0; a < 256; a++) { 
     S[a] = a; 
     T[a] = Integer.parseInt(Integer.toHexString((char) K[a % (keyLength)]), 16); 
    } 
    for (int a = 0; a < 256; a++) { 
     j = (j + S[a] + T[a]) % 256; 
     temp = S[a]; 
     S[a] = S[j]; 
     S[j] = temp; 
    } 
    return S; 

} 

/*ENCRYPT*/ 
public static byte[] encrypt(byte[] pt, int[] S) { 

    byte[] cipher = new byte[pt.length];// cipher text array 
    int i = 0, k = 0, j = 0, t = 0; 
    byte tmp; // temp placeholder 
    for (int count = 0; i < pt.length; count++) { 
     i = (i + 1) & 0xFF; 
     j = (j + S[i]) & 0xFF; 
     // perform swap 
     tmp = (byte) S[j]; 
     S[j] = S[i]; 
     S[i] = tmp; 
     t = (S[i] + S[j]) & 0xFF ; 
     k = S[t]; 
     cipher[count] = (byte)(pt[count]^k);// XOR 

    } 
    return cipher; 

} 

/*HEX TO BYTE ARRAY*/ 
public static byte[] hexToByteArray(String hex){ 
    return DatatypeConverter.parseHexBinary(hex); 

} 

/*BYTE ARRAY TO HEX STRING*/ 
public static String bytesToHex(byte[] bytes){ 
    String result = ""; 
    for(int i=0; i < bytes.length;i++){ 
     result += Integer.toString((bytes[i] & 0xFF) + 0x100,16).substring(1); 
    } 
    return result; 
} 

public static void main(String[] args) { 
    String key = "12345678"; 
    String pt = "hello"; 
    //String ct = "013d0175c986a8bd9f"; 
    byte M[] = new byte[pt.length()];//message bytes 
    M = pt.getBytes(); 
    System.out.println("PlainText: " + pt); 
    System.out.print("PlaintText bytes: "); 
    for(int i = 0;i<M.length;i++){ 
     System.out.print(M[i] + " "); 
    } 

    /* S-Box from KSA algorithm function*/ 
    int S[] = myKSA(key); 

    /**************************** 
    * Step 1: 
    * based the initial key iamkey, 
    * show the S-box after applying Key Schedule Algorithm 
    ***************************/ 
//  System.out.println("The final S-box after using KSA  algorithmis..."); 
//  for (int i = 0; i < S.length; i++) {  
//  if ((i % 16 == 0) && i > 0) { 
//  System.out.println(); 
//  }//if 
//  System.out.print(S[i] + " "); 
//  } // for 
    /************** 
    * END PRINT S-BOX 
    * ************/ 

    byte ctbytes[] = encrypt(M, S); 

    /*CipherText Bytes*/ 
    System.out.print("\nCipherText Bytes: "); 
    for(int i = 0; i < ctbytes.length; i++){ 
     System.out.print(ctbytes[i] + " "); 
    } 

    /*CipherText Hex Value*/ 
     String CipherHex = bytesToHex(ctbytes); 
    System.out.println("\nCipherText Hex: " +CipherHex); 

    /*Decrypted Bytes*/ 
    System.out.print("Decrypted PT Bytes: "); 
    byte dcbytes[] = encrypt(ctbytes,S); 
    for(int i = 0; i < dcbytes.length; i++){ 
     System.out.print(dcbytes[i]+ " "); 

    } 
    String s = new String(dcbytes); 
    System.out.println(s); 

}// main 
} 
+0

これはどういう意味ですか? "私は解読バイトを理解できません"。何が正しく動作していないのかを具体的に記述してください。 – Jeremy

+0

私の平文バイトは正しいです、暗号文のバイトとcipherHexは正しいです。しかし、私が解読して "String s = new String(dcbytes)"を実行すると、私は元の平文を取得しません – user3412695

+0

私はどこで解読しているのか分かりません。あなたは二度だけ暗号化していますか? (私はこのアルゴリズムに精通していないので間違えるかもしれません...) – Jeremy

答えて

1

解決は簡単でした。元の状態を保持する元のS-Boxの別のインスタンスを作成するだけでした。暗号化がインデックスを交換していた

int[] S = myKSA(key); 
int[] S2 = myKSA(key); //to hold original state after encrypt 
+0

ニース。私は最後の数時間このプログラムで遊んで終わった。面白かったです:-) – Jeremy

+1

あなたの入力が変更されていないことを確認したいと思います。 * myKSAの中で入力パラメータ 'S' *を複製することによって、 –

関連する問題