基本上我可以成功实现s 的RC4
算法String
,它需要一个 Byte[]
数组key
:
byte [] key = "AAAAA".getBytes("ASCII");
如果我将 clearText 作为 String 说 "24" 那么密文范围非常高,比如 > 2000 。但是对于我的算法,我需要将其限制在较小的范围内~200。
那么我可以为int
's 提供更好的选择吗?
这就是我对 Strings 所做的事情:
加密模式:
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));
-可以对这些类型执行任何技巧以获得较低的int值吗?
解密:
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"));
}
}