Buenas, quería saber si alguién me podría dar una mano. Estaba armando un código para cifrar un archivo cualquiera mediante RC4, el problema es que me genera una excepción (ArrayIndexOutOfBoundException) cuando intento ejecutarlo lo cual es raro ya que pude ejecutar el mismo código ligeramente modificado en C++(WINAPI) y C#... Si alguien me dá una mano se lo agradecería.
PS: El código de RC4 lo saque del foro: http://foro.elhacker.net/programacion_cc/encriptacion_rc4arc4-t210711.0.html;msg1000447#msg1000447
B#
Código (java) [Seleccionar]
private byte[] RC4(byte[] szBuf,byte[] szKey,int dwBufLen,int dwKeyLen){
int i,j=0;
int []s=new int[256];
int dw;
byte tmp;
byte[] Buf=szBuf;
byte[] Key=szKey;
for(i=0;i<256;i++){
s[i]=i;
}
for(i=0;i<256;i++){
j=(j+s[i]+Key[i%dwKeyLen])%256;
tmp=(byte)s[i];
s[i]=s[j];
s[j]=(int)tmp;
}
for(dw=0;dw<dwBufLen;dw++){
i=(i+1)%256;
j=(j+s[i])%256;
tmp=(byte)s[i];
s[i]=s[j];
s[j]=(int)tmp;
Buf[dw]^=(byte)s[(s[i]+s[j])%256];
}
return Buf;
}
private void btnCifrarActionPerformed(java.awt.event.ActionEvent evt) {
if(txtArchivo.getText().length()==0)
JOptionPane.showMessageDialog(this,"No hay Archivo, seleccione uno...","RC4 para Archivos - BloodSharp",JOptionPane.ERROR_MESSAGE);
else if(txtPassword.getText().length()==0)
JOptionPane.showMessageDialog(this,"Escriba una contraseña...","RC4 para Archivos - BloodSharp",JOptionPane.ERROR_MESSAGE);
else{
try{
File file=new File(txtArchivo.getText());
int longitud=(int)file.length();
byte[] Bytes=new byte[longitud];
FileInputStream fileInput=new FileInputStream(file);
BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
bufferedInput.read(Bytes,0,longitud);
Bytes=RC4(Bytes,txtPassword.getText().getBytes(),longitud,txtPassword.getText().length());
fileInput.close();
bufferedInput.close();
FileOutputStream fileOutput=new FileOutputStream(file);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutput);
bufferedOutput.write(Bytes,0,longitud);
fileOutput.close();
bufferedOutput.close();
JOptionPane.showMessageDialog(this,"El archivo "+txtArchivo.getText()+" fue Cifrado/Descifrado","RC4 para Archivos - BloodSharp",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this,e.getClass().toString()+" "+e.getMessage(),"RC4 para Archivos - BloodSharp",JOptionPane.ERROR_MESSAGE);
}
}
}
PS: El código de RC4 lo saque del foro: http://foro.elhacker.net/programacion_cc/encriptacion_rc4arc4-t210711.0.html;msg1000447#msg1000447
B#