Cifrado JAVA DES DES3 RC4 ¿Cuánto más grande será la cadena después del cifrado?
clase pública RC4 {
cadena estática pública decry_RC4(byte[] datos, clave de cadena) {
if (datos == nulo || clave == nulo) {
devolver nulo
}
devolver asString(RC4Base(datos, clave)); > }
cadena estática pública decry_RC4 (datos de cadena, clave de cadena) {
if (datos == nulo || clave == nulo) {
retorno null;
}
Devuelve una nueva cadena (RC4Base(datos), clave).
Devuelve nueva cadena(RC4Base(HexString2Bytes(datos), clave));
}
}
}
byte estático público[] encry_RC4_byte(datos de cadena, clave de cadena) {
if (data == null || key == null) {
return null ; p> p>
}
}
byte b_ data[] = data.getBytes();
return RC4Base(b_data, clave);
p>
}
cadena estática pública encry_RC4_string(datos de cadena, clave de cadena) {
if (datos == nulo || clave == nulo) {
return nulo
}
Volver aHexString(asString(encry_RC4_byte(datos, clave))
}
}
}
cadena estática privada asString(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length
for (int i = 0; i lt; buf.length. i ) {
strbuf.append((char) buf[i]);
}
return strbuf.append((char) buf[i]).
Return strbuf.toString();
}
byte estático privado [] initKey(String aKey) {
byte[] b_key = aKey.getBytes();
estado del byte[] = nuevo byte[256] ;
for (int i = 0; i lt; 256; i) {
estado[i] = (byte) i
}
int index1 = 0;
int index2 = 0
if (b_key == null || b_key.length == 0) {
return null;
}
for (int i = 0; i lt; 256; i) {
index2 = (( b_key[index1] amp; 0xff ) (estado[i] amp; 0xff) index2) 0xff
byte tmp = estado[i]; estado[índice2];
p>
estado[index2] = tmp;
index1 = (index1 1) b_key.
Estado de retorno}
Estado de retorno
}
cadena estática privada toHexString(String s) {
String str = ""
for (int i = 0; i lt; s .length(); i ) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch amp; 0xFF); p>
p>
if (s4.length() == 1) {
s4 = '0' s4
}
str = str s4 ;
}
return str; // 0x representa hexadecimal
}
byte estático privado[] HexString2Bytes (String src) {
int tamaño = src.length();
byte[] ret = nuevo byte[tamaño / 2]; ] tmp = src.getBytes();
for (int i = 0; i lt; tamaño / 2; i) {
ret[i] = uniteBytes(tmp[i * 2 ], tmp[i * 2 1]);
} Devuelve ret.
Devuelve ret;
}
byte estático privado uniteBytes(byte src0, byte src1) {
char _b0 = (char) Byte.decode("0x" nueva cadena (nuevo byte[] { src0 })).byteValue();
_b0 = (char) (_b0 lt; lt; 4); p> char _b1 = (char) Byte.decode("0x" new String(new byte[] { src1 }). byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
byte estático privado[] RC4Base(byte[] entrada, String mKkey) {
int x = 0;
int y = 0;
clave de byte[] = initKey(mKkey); [] resultado = nuevo byte[input.length];
for (int i = 0; i lt; input.for (int i = 0; i lt; input.length; i) { p>
x = (x 1) amp; 0xff;
y = ((clave[x] amp; 0xff) y) 0xff;
byte tmp; clave[x];
clave[x] = clave[y];
clave[y] = tmp
xorIndex = ((clave[x; ] amp; 0xff) (clave[y] amp; 0xff)) amp; 0xff;
resultado[i] = (byte) (entrada[i] ^ clave[ xorIndex]);
}
Devuelve el resultado
Devuelve el resultado
}
public static void main(String[] args; ) {
String inputStr = "Sé un buen hombre";
String str = encry_RC4_string(inputStr, "123456"); (cadena
System.out.println(decry_RC4(cadena, "123456")); }