I have two applications running on different servers, one having JDK 1.6 and another having JRockit.
I am using RC4 algorithm to mask a string and send to the different application hosted in different server.
Below program can be used for mask and unmask, And both the servers have same program running.
I have tried putting "ISO-8859-1" encoding format in both the servers, but it didn't help me. While decoding the value program fails, and gives garbage. Previously when I had these two applications hosted in same server, it was working and had no issues.
Below is the program...please help...
String prefix = "dEncrypt";
if(null==value||value.length()<1){
return value;
}
else{
byte[] input = null;
try {
value = new String(value);
//String value1 = new String(value,"UTF-8");
input = URLDecoder.decode(value).getBytes("ISO-8859-1");
for(int i =0 ;i<input.length ; i++)
System.out.println("input=" + input[i]);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] key = null;
try {
key = "123456789123456789123456789".getBytes("ISO-8859-1");
for(int i =0 ;i<key.length ; i++)
System.out.println("key=" + key[i]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] state = new byte[256];
int x, y;
for( int i = 0; i < state.length; i++ ) {
state[i] = (byte) i;
}
x = 0;
for( int i = 0; i < state.length; i++ ) {
x = (x + key[i % key.length] + state[i]) & 0xFF;
//System.out.println("x=" + x);
byte swap = state[i];
state[i] = state[x];
state[x] = swap;
}
x = 0;y=0;
byte[] output = new byte[input.length];
for( int i = 0; i < input.length; i++ ) {
x = (x + 1) % 256;
y = (state[x] + y) & 0xFF;
byte swap = state[x];
state[x] = state[y];
state[y] = swap;
byte r = state[(state[x] + state[y]) & 0xFF];
output[i] = (byte) (input[i] ^ r);
System.out.println("output=" + output[i]);
}
try {
//System.out.println(" New string " +URLEncoder.encode(new String(output,"UTF-16") ));
byte [] enc = Charset.forName("ISO-8859-1").encode(new String(output)).array();
System.out.println(Charset.isSupported("base64"));
//Charset.
System.out.println(new String(enc));
System.out.println("URLEncoded1=" + URLEncoder.encode(new String(enc)));
System.out.println("URLEncoded2=" + URLEncoder.encode(new String(output,"ISO-8859-1")));
return URLEncoder.encode(new String(output,"ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return URLEncoder.encode(new String(output));
}