I built an encryption database application using RC4, where I retrieved text from the database column and then the text was encrypted using RC4 and saved back into the database column. as well as the description process and the encryption has been successful.
https://i.stack.imgur.com/8byAX.jpg
the problem is when the description process. when I retrieve text from the database column (after encrypted), the results are not the same as the original text.
My RC4 class code :
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;
}
}
code how do i use encryption and description :
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);
}
The following results are encryption and description:
- Encryption Process:
Plain Text / Real Text (from the database column): [admin, pegawai, penyidik]
Chiper Text / After Encryption (saved into database column): [irU: �, xs_2�p, xsV * �u W]
- Process Description:
Plain Text / Text After Encryption (from the database column): [irU: �, xs_2�p, xsV * �u W]
After Descryption (saved into the database column): [admit��, pegat��L�, peny����I� ']
the description should be: [admin, pegawai, penyidik]
I do not know what happened and how to overcome it?