0

我目前正在实现一个聊天程序,让用户可以在 RC4 和 TEA 加密之间进行选择。这是一个合作伙伴的任务,我参加了RC4。我主要使用维基百科页面,以及我们的书(我认为代码与维基百科页面上的代码相同)。

https://en.wikipedia.org/wiki/RC4

离开那里的代码,我把它改成了Java。请注意,我的 convertKey 方法将从一个单独的类中获取一个密钥,该类利用 Diffie-Hellman 获取密钥(我现在对密钥进行硬编码以确保 RC4 按预期工作),但只有我的 RC4 类不起作用,所以除非特别要求(因为其他类工作正常,而这个 RC4 类目前不使用它们)我只会粘贴 RC4 类以避免浪费空间。

堆栈跟踪:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:cryptochat2.RC4.PRGA(RC4.java:80) at cryptochat2.RC4.main(RC4.java:132) Java 结果中的-24:1 -</ p>

/*
 * To change this license header, choose License Headers in Project      Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cryptochat2;

/**
 *
 * @author Braydon
 */
//Initialize array of 256 bytes
// Run KSA on them (key stream algorithm)
// Run PRGA on KSA to generate keystream
// XOR the data with the keystream
import java.util.Arrays;
import java.util.*;
import java.util.stream.IntStream;

// The interesting feature of RC4 is that the key can be of any length from 1 to 256 
//bytes. And again, the key is only used to initialize the permutation S.
//Note that the 256-byte array K is filled by simply repeating the key until 
//the array is full.
public class RC4 {

byte[] lookUpTable = new byte[256]; // S
byte[] key = new byte[256];
byte[] initializedKey = new byte[256]; // key
byte[] keyStream;
byte keyStreamByte;
boolean generatingOutput;
//int key;
int keylength = 256;


// Call KSA then PRGA
public void KSA() {
    // uses identity permutation while also converting to bytes
    // then must process S
    for (int k = 0; k < 256; k++) {
        lookUpTable[k] = (byte) k;
    }
    // now processing permutation of array
    int j = 0;
    int tempOne = 0;
    int tempTwo = 0;
    for (int k = 0; k < 256; k++) {
        j = (j + lookUpTable[k] + initializedKey[k % keylength]) % 256;
        // Switching S[i] and S[j]
        byte tmp;
        for (int i = 0; i < 256; i++) {
            j = (j + lookUpTable[i] + initializedKey[i]) & 0xFF;
            tmp = lookUpTable[j];
            lookUpTable[j] = lookUpTable[i];
            lookUpTable[i] = tmp;

        }
    }
}
 //Error in PRGA-- arrayIndexOutOfBounds, value differs based on key but
// error remains the same. It's an issue with the following method.
public void PRGA() {
    int i = 0;
    int j = 0;
    byte tmp;
    boolean generatingOutput = true;
    while (generatingOutput) {
        i = (i + 1) % 256;
        j = (j + lookUpTable[i]) % 256;
        for (int k = 0; k < 256; k++) {
            j = (j + lookUpTable[i] + initializedKey[i]) & 0xFF;
            tmp = lookUpTable[j];
            lookUpTable[j] = lookUpTable[i];
            lookUpTable[i] = tmp;
        }

        int keyStreamByteTemp = ((lookUpTable[i] + lookUpTable[j]) % 256);

        try {// error's in this part vvvvvv
        keyStreamByte = lookUpTable[keyStreamByteTemp];
        System.out.println("keystream byte: " + keyStreamByte);
    } catch (IndexOutOfBoundsException exception){
            System.out.println(keyStreamByte + " has given an exception");
    }
}
}

public void convertKey(int key) {
    // call this first. it gives us our key.
    int nonByte = key;
    byte bytedKey = (byte) key;
    // We create an int array and from it we initialize our key byte array
    int[] data = IntStream.generate(() -> bytedKey).limit(256).toArray();
    for (int i = 0; i < 256; i++) {
        initializedKey[i] = (byte) data[i];

    }
    keylength = numberOfLeadingZeros(key);
}

public static int numberOfLeadingZeros(int i) {
    // http://stackoverflow.com/questions/2935793/count-bits-used-in-int
    if (i == 0) {
        return 32;
    }
    int n = 1;
    if (i >>> 16 == 0) {
        n += 16;
        i <<= 16;
    }
    if (i >>> 24 == 0) {
        n += 8;
        i <<= 8;
    }
    if (i >>> 28 == 0) {
        n += 4;
        i <<= 4;
    }
    if (i >>> 30 == 0) {
        n += 2;
        i <<= 2;
    }
    n -= i >>> 31;
    return n;
}

public static void main(String[] args) {
    RC4 RC4 = new RC4();
    // temp hard coded key
    RC4.convertKey(16);
    RC4.KSA();
    RC4.PRGA();
}



}

虽然我对 RC4 有一个大致的了解,但很可能我有一两个元素混淆、不正确或类似的东西。try/catch 块给了我一个无限循环(由于我想象的 generateOutput 布尔值,因为我们还没有传递实际的消息),正数和负数,抛出 IndexOutOfBounds 异常,直到我停止程序.

keystream byte: -53
keystream byte: 105
105 has given an exception
keystream byte: 6
6 has given an exception
keystream byte: -111
keystream byte: 73
73 has given an exception
keystream byte: 66
keystream byte: -86
keystream byte: -104
keystream byte: -114
keystream byte: -117
keystream byte: 56
keystream byte: 67
67 has given an exception
keystream byte: 121
keystream byte: 10
keystream byte: -7
keystream byte: 16
keystream byte: 103
103 has given an exception
keystream byte: -65
-65 has given an exception
keystream byte: 31
31 has given an exception
keystream byte: 21

我真的很感激任何帮助,我想学好这一点,不要让我的教授或我自己失望。

4

0 回答 0