Basically I can successfully implement RC4
algorithm for String
s, which takes an Byte[]
array for the key
:
byte [] key = "AAAAA".getBytes("ASCII");
If I take clearText as String say "24" then the cipher text range is very high, say > 2000 . But for my algorithm, I need to restrict it to fewer range ~200.
So can I have better option for int
's ?
This is what I am doing with Strings :
Encrypt Mode:
byte [] key = "AAAAA".getBytes("ASCII");
String clearText = "66";
Cipher rc4 = Cipher.getInstance("RC4");
SecretKeySpec rc4Key = new SecretKeySpec(key, "RC4");
rc4.init(Cipher.ENCRYPT_MODE, rc4Key);
byte [] cipherText = rc4.update(clearText.getBytes("ASCII"));
Check values:
System.out.println("clear (ascii) " + clearText);
System.out.println("clear (hex) " + DatatypeConverter.printHexBinary(clearText.getBytes("ASCII")));
System.out.println("cipher (hex) is " + DatatypeConverter.printHexBinary(cipherText));
- Can any trick be preformed on these types to get a lower int value?
Decrypt :
Cipher rc4Decrypt = Cipher.getInstance("RC4");
rc4Decrypt.init(Cipher.DECRYPT_MODE, rc4Key);
byte [] clearText2 = rc4Decrypt.update(cipherText);
SSCCE :
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class MyArcFour
{
public static void main(String args[])throws Exception
{
byte [] key = "AAAAA".getBytes("ASCII");
String clearText = "66";
Cipher rc4 = Cipher.getInstance("RC4");
SecretKeySpec rc4Key = new SecretKeySpec(key, "RC4");
rc4.init(Cipher.ENCRYPT_MODE, rc4Key);
byte [] cipherText = rc4.update(clearText.getBytes("ASCII"));
System.out.println("clear (ascii) " + clearText);
System.out.println("clear (hex) " + DatatypeConverter.printHexBinary(clearText.getBytes("ASCII")));
System.out.println("cipher (hex) is " + DatatypeConverter.printHexBinary(cipherText));
Cipher rc4Decrypt = Cipher.getInstance("RC4");
rc4Decrypt.init(Cipher.DECRYPT_MODE, rc4Key);
byte [] clearText2 = rc4Decrypt.update(cipherText);
System.out.println("decrypted (clear) is " + new String(clearText2, "ASCII"));
}
}