【问题标题】:How to fix RC4 description does not match the original text?如何修复 RC4 描述与原文不符?
【发布时间】:2020-02-22 22:40:03
【问题描述】:

我使用 RC4 构建了一个加密数据库应用程序,我从数据库列中检索文本,然后使用 RC4 对文本进行加密并保存回数据库列。以及描述过程,加密成功。

https://imgur.com/BwpDXv6

问题出在描述过程的时候。当我从数据库列中检索文本时(加密后),结果与原始文本不同。

我的 RC4 课程代码:

     public final class RC4 {

      static short[] S;
      static short[] T;

public RC4(String keyString) {

    if (keyString.length() < 1 && keyString.length() > 256) {
        throw new IllegalArgumentException("Key lenght should be in between 1 and 256");
    }

    byte[] tempKey;
    tempKey = keyString.getBytes();
    short[] key = new short[tempKey.length];
    int keyLength = tempKey.length;

    for (int i = 0; i < keyLength; i++) {
        key[i] = (short) ((short) tempKey[i] & 0xff);
    }
    ksa(key);

}

public void ksa(short[] key) {
    short temp;
    S = new short[256];
    T = new short[256];

    for (int i = 0; i < 256; i++) {
        S[i] = (short) i;
    }

    int j = 0;
    for (int i = 0; i < 256; i++) {
        j = (j + S[i] + key[i % key.length]) % 256;

        temp = S[i];
        S[i] = S[j];
        S[j] = temp;
    }
    System.arraycopy(S, 0, T, 0, S.length);
}

public byte[] genPad(int length) {
    System.arraycopy(S, 0, T, 0, S.length);
    int i = 0, j = 0;
    short temp;
    byte[] tempPpad = new byte[length];
    for (int k = 0; k < length; k++) {
        i = (i + 1) % 256;
        j = (j + T[i]) % 256;

        temp = T[i];
        T[i] = T[j];
        T[j] = temp;

        tempPpad[k] = (byte) (T[(T[i] + T[j]) % 256]);
    }
    return tempPpad;
}

public byte[] encrypt(byte[] plain) {
    byte[] pad = genPad(plain.length);
    byte[] encrypt = new byte[plain.length];
    for (int i = 0; i < plain.length; i++) {
        encrypt[i] = (byte) (plain[i] ^ pad[i]);
    }
    return encrypt;
}

public byte[] decrypt(byte[] cipher) {
    byte[] plain = encrypt(cipher);
    return plain;
  }
}

代码我如何使用加密和描述:

     start = System.currentTimeMillis(); //waktu memulai proses

     try{
          Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+jComboBox1.getSelectedItem()+"","root","");
          Statement stmt=con.createStatement();


         for(int i =0; i < box.size();i++){
             if(box.get(i).isSelected()){
                 //System.out.print(box.get(i).getText().trim());
                 ResultSet rs=stmt.executeQuery("select "+box.get(i).getText().trim()+" from "+jComboBox3.getSelectedItem()+";");
                 while(rs.next()){
                    isikolom = rs.getString(1);
                    isi.add(isikolom);

                    String getkey2 = jTextField1.getText().toString();
                    byte[] key2 = getkey2.getBytes();
                    RC4 rc = new RC4(new String(key2));


                    String chiperText = isikolom;
                    byte[] desText = rc.decrypt(chiperText.getBytes());
                    String descrypted = new String(desText, "UTF-8");
                    StringBuilder sb = new StringBuilder(128);
                    sb.append("UPDATE ").append(jComboBox3.getSelectedItem().toString()).append(" SET ");
                    sb.append(box.get(i).getText().toString()+" = ").append("REPLACE ").append("("+box.get(i).getText().toString()+",");
                    sb.append("'"+isikolom+"'").append(",").append("'"+descrypted+"')");
                    String query2 = sb.toString();
                    System.out.println(query2);
                    PreparedStatement presatet2 = con.prepareStatement(query2);
                    //presatet2.executeUpdate();

                    isideskripsi.add(descrypted);

                 }
                 System.out.println("Plain Text / Text After Encryption : "+isi);
                 System.out.println("After Descryption : "+isideskripsi);


        }
     }

        end = System.currentTimeMillis();
        long time = end - start;
        JOptionPane.showMessageDialog(null, "Berhasil Deskripsi Dalam Waktu "+time+" Detik");


    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "Gagal Deskripsi, Error Pada : "+e);
        System.out.print(e);
    }

以下结果为加密和说明:

  1. 加密过程:

纯文本/真实文本(来自数据库列):[admin, pegawai, penyidik]

Chiper Text / After Encryption(保存到数据库列):[irU: �, xs_2�p, xsV * �u W]

  1. 流程说明:

纯文本/加密后的文本(来自数据库列):[irU: �, xs_2�p, xsV * �u W]

解密后(存入数据库列):[admit��, pegat��L�, peny����I� ']

描述应该是:[admin, pegawai, penyidik]

我不知道发生了什么以及如何克服它?

【问题讨论】:

  • 这个词是“解密”。请修正您的问题和标题。 RC4 几十年前就被破解了,2019 年不应该使用。
  • 在印度尼西亚语中,根据谷歌翻译,它是“dekripsi”。我也会修复你的软件。 “deskripsi” 的意思是“描述”。
  • 您正在使用来自其他列的密钥加密列值?您的代码非常不清楚。

标签: java mysql encryption rc4-cipher


【解决方案1】:

如果您坚持使用 RC4,请先检查一个好的描述。 $i$ 和 $j$ 索引也是密码状态的一部分。

S,i,j 设为 RC4 类的私有成员似乎是个好主意。 尚不清楚变量T 的用途。

一般来说,使用相同的密钥(应该是 bytes[] 对象,而不是字符串)加密多个项目对于像 RC4 这样的流密码来说是个坏主意。

【讨论】:

  • 我得到了同样的结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-05
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 2020-12-21
相关资源
最近更新 更多