⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rc4cipher.java

📁 RC4Cipher 经典的RC4Cipher加密解密算法源代码
💻 JAVA
字号:
package cipher;
import javax.swing.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author srt
 * @version 1.0
 */

public class RC4Cipher
    extends BaseCipher {

  private short[] sBox;
  private short[] keyBox;
  private short[] sKeyMatrix;
  private int[] i_sboxIndex;
  private int[] j_sboxIndex;

  RC4Cipher() {
    super();
    sBox = new short[256];
    keyBox = new short[256];
    sKeyMatrix = new short[4];
    i_sboxIndex = new int[1];
    j_sboxIndex = new int[1];
  }

  public int setKeyMatrix(int i, String sKey) { //读密钥
    short key;
    try {
      Integer.valueOf(sKey);
    }
    catch (NumberFormatException e) {
      String label = "万分歉意!阁下输入的加密参数不是整数,请重新输入!";
      JOptionPane.showMessageDialog(null, label, "小郭襄的提醒",
                                    JOptionPane.ERROR_MESSAGE);
      return 0;
    }
    key = (short) Integer.parseInt(sKey);
    if (key > 255 || key < 0) {
      String label = "万分歉意!阁下输入的加密参数超过范围,请重新输入!";
      JOptionPane.showMessageDialog(null, label, "小郭襄的提醒",
                                    JOptionPane.ERROR_MESSAGE);
      return 0;
    }
    else {
      sKeyMatrix[i] = key;
    }
    return 1;
  }

  private void initialSBox() { //初始化SBox
    int i = 0;
    for (i = 0; i <= 255; i++) {
      sBox[i] = (short) i;
    }
  }

  private void initialKeyBox() { //初始化KeyBox
    int j = 0;
    for (int i = 0; i <= 255; i++) {
      keyBox[i] = sKeyMatrix[j % 4];
      j++;
    }
  }

  public String getSBox() { //读取SBox内容
    String s = "";
    for (int i = 0; i < sBox.length; i++) {
      s += String.valueOf(i) + "     " + String.valueOf(sBox[i]) + '\n';
    }
    return s;
  }

  public String getKBox() { //读取KeyBox内容
    String s = "";
    for (int i = 0; i < keyBox.length; i++) {
      s += String.valueOf(i) + "     " + String.valueOf(keyBox[i]) + '\n';
    }
    return s;
  }

  private void KSA() { //设置SBox的初始态S0
    int i = 0, j = 0;
    short temp;
    for (i = 0; i <= 255; i++) {
      j = (j + sBox[i] + keyBox[i]) % 256;
      temp = sBox[i];
      sBox[i] = sBox[j];
      sBox[j] = temp;
    }
  }

  public short psga(int[] i, int[] j) { //产生随机2字节密钥
    short temp, key;
    int t;
    i[0] = (i[0] + 1) % 256;
    j[0] = (j[0] + sBox[i[0]]) % 256;
    temp = sBox[i[0]];
    sBox[i[0]] = sBox[j[0]];
    sBox[j[0]] = temp;
    t = (sBox[i[0]] + sBox[j[0]]) % 256;
    key = sBox[t];
    //System.out.println(Integer.toBinaryString(key)+"   "+shortToBinary(key));
    return key;
  }

  public String getKeyStream(String keyStreamSize) { //获得指定密钥流大小的密钥流
    int keyStreamSizeTemp;
    int index = 0;
    StringBuffer keyStreamTemp = new StringBuffer();
    try {
      Integer.valueOf(keyStreamSize);
    }
    catch (NumberFormatException e) {
      String label = "万分歉意!阁下输入的密钥流大小不是整数,请重新输入!";
      JOptionPane.showMessageDialog(null, label, "小郭襄的提醒",
                                    JOptionPane.ERROR_MESSAGE);
      return "";
    }
    keyStreamSizeTemp = Integer.parseInt(keyStreamSize);
    for (int k = 0; k < (keyStreamSizeTemp + 7) / 8; k++) {
      index++;
      keyStreamTemp.append(shortToBinary(psga(i_sboxIndex, j_sboxIndex)));
      if (index == 2) {
        keyStreamTemp.append('\n');
        index = 0;
      }
    }
    return keyStreamTemp.toString();
  }

  private String shortToBinary(short a) { //把shor型a转化为其值的对应的二进制字符串
    StringBuffer sbtemp = new StringBuffer("00000000");
    String stemp = Integer.toBinaryString(a);
    sbtemp.setLength(8 - stemp.length());
    sbtemp.append(stemp);
    return sbtemp.toString();
  }

  public void initialKboxAndSbox() { //初始化KeyBox和SBox
    initialKeyBox();
    initialSBox();
    KSA();
  }

  private short BinaryStringToShort(String s) { //把二进制字符串转化为对应的Short型的值
    double j = 7;
    short sum = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s.charAt(i) == '1') {
        sum += (short) Math.pow(2.0, j);
      }
      j--;
    }
    return sum;
  }

  int decipher() {
    short CiphertextTemp;
    initialKboxAndSbox();
    StringBuffer sbPlaintext = new StringBuffer();
    i_sboxIndex[0] = 0;
    j_sboxIndex[0] = 0;
    for (int k = 0; k < sCiphertext.length(); k = k + 8) {
      CiphertextTemp = BinaryStringToShort(sCiphertext.substring(k, k + 8));
      CiphertextTemp = (short) (CiphertextTemp ^ psga(i_sboxIndex, j_sboxIndex));
      sbPlaintext.append( (char) CiphertextTemp);
    }
    sPlaintext = sbPlaintext.toString();
    return 1;
  }

  int encipher() {
    short temp;
    initialKboxAndSbox();
    StringBuffer sbCiphertext = new StringBuffer();
    i_sboxIndex[0] = 0;
    j_sboxIndex[0] = 0;
    for (int k = 0; k < sPlaintext.length(); k++) {
      temp = (short) (sPlaintext.charAt(k) ^ psga(i_sboxIndex, j_sboxIndex));
      sbCiphertext.append(shortToBinary(temp));
    }
    sCiphertext = sbCiphertext.toString();
    return 1;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -