【问题标题】:RC4 Decryption JavaRC4 解密 Java
【发布时间】:2017-03-07 04:03:47
【问题描述】:

我一直在网上寻找很多有用的东西,尤其是在这里,我很接近,但我不太清楚解密字节。解密不工作是因为我在字节数组中发送函数负值吗?

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
}

【问题讨论】:

  • 这是什么意思? “我无法弄清楚解密字节”。请更具体地说明哪些工作不正常。
  • 我的明文字节是正确的,密文字节和cipherHex是正确的。但是当我解密并执行“S​​tring s = new String(dcbytes)”时,我没有得到原始明文
  • 我看不出你在哪里解密。你只是加密两次? (我可能弄错了,因为我不熟悉这个算法......)
  • 解密是同一个过程

标签: java encryption cryptography rc4-cipher


【解决方案1】:

解决方案很简单。只需要创建原始 S-Box 的另一个实例来保持原始状态。加密正在交换索引

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

【讨论】:

  • 不错。在过去的几个小时里,我最终玩了这个程序。很有趣:-)
  • 我宁愿确保您的输入没有被更改,例如通过克隆输入参数S within myKSA.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
相关资源
最近更新 更多