简体   繁体   English

整数的RC4加密算法

[英]Encryption RC4 algorithm for integers

Basically I can successfully implement RC4 algorithm for String s, which takes an Byte[] array for the key : 基本上,我可以为String成功实现RC4算法,该算法需要Byte[]数组作为key

byte [] key = "AAAAA".getBytes("ASCII");

If I take clearText as String say "24" then the cipher text range is very high, say > 2000 . 如果我将clearText作为String说“ 24”,则密文范围非常大,例如> 2000。 But for my algorithm, I need to restrict it to fewer range ~200. 但是对于我的算法,我需要将其限制在200左右。

So can I have better option for int 's ? 那么我可以为int提供更好的选择吗?

This is what I am doing with Strings : 这就是我在用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? -可以对这些类型执行任何技巧以获得较低的int值吗?

Decrypt : 解密

  Cipher rc4Decrypt = Cipher.getInstance("RC4");
  rc4Decrypt.init(Cipher.DECRYPT_MODE, rc4Key);
  byte [] clearText2 = rc4Decrypt.update(cipherText);

SSCCE : 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"));
   }
}

When using a stream cipher (as RC4 is) the length of the ciphertext will always equal the length of the plaintext. 使用流密码时 (如RC4一样 ),密文的长度将始终等于明文的长度。

That means when you encrypt an int (which has 4 bytes), you will always receive another int (4 bytes) as encrypted output. 这意味着当您加密一个int (具有4个字节)时,您将始终收到另一个int(4个字节)作为加密输出。 So theoretically when you encrypt 0 you could get as a result 2 20 . 因此,从理论上讲,当您加密0时,结果可能是2 20
If you want a value in range 0 - 255 you can only encrypt a single byte. 如果要在0 - 255的值,则只能加密一个字节。

Any good encryption algorithm must satisfy the property that when using the same encryption key k any distinct plaintext p must encrypt to a distinct ciphertext c . 任何好的加密算法都必须满足以下特性:在使用相同的加密密钥k任何不同的明文p必须加密为不同的密文c
This property is only achievable when size(output) >= size(input) . 仅当size(output) >= size(input)时才能实现此属性。

Why must that the be case? 为什么一定要这样呢?
Well, consider the following encryption function: 好吧,考虑以下加密功能:

/* output = E(key, input); key = 123 */

i | o
======
1 | 17
2 | 13
3 | 17
4 | 125

Then when you have the ciphertext 17 and the key 123 there is no way to tell if the originally encrypted value was 1 or 3 . 然后,当您具有密文17和密钥123 ,将无法分辨原始加密的值是1还是3

That means if you want to be able to non ambiguously decrypt the values afterwards, there is no way to decrease the size of the resulting ciphertext. 这意味着,如果您希望以后能够无歧义地解密这些值,则无法减小所得密文的大小。

If decrypting is not necessary, you could always do output % some_number or use a hash function with desired output length. 如果不需要解密,则始终可以output % some_number或使用具有所需输出长度的哈希函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM