Java在同一字符串上返回不同的CRC32结果
我正在玩RC4和CRC32,试图模拟一个稍微翻转的攻击,我对Java中CRC32的行为摸不着头脑。据我所知,这应该是基于多项式计算的确定性结果。然而,我看到的是,无论文本是否发生了变化,CRC都以一种不可预测的方式发生变化。Javadoc只是在字节数组上声明update():使用指定的字节数组更新CRC-32校验和。对于getValue():返回CRC-32值。这里是否有某种盐或PRF是我没有考虑的(我不这么认为) 输出:Java在同一字符串上返回不同的CRC32结果,java,crc,crc32,Java,Crc,Crc32,我正在玩RC4和CRC32,试图模拟一个稍微翻转的攻击,我对Java中CRC32的行为摸不着头脑。据我所知,这应该是基于多项式计算的确定性结果。然而,我看到的是,无论文本是否发生了变化,CRC都以一种不可预测的方式发生变化。Javadoc只是在字节数组上声明update():使用指定的字节数组更新CRC-32校验和。对于getValue():返回CRC-32值。这里是否有某种盐或PRF是我没有考虑的(我不这么认为) 输出: run: Ciphertext = [B@34e51b72, and C
run:
Ciphertext = [B@34e51b72, and CRC = 232697804
Now ciphertext = [B@34e51b72, and CRC = 1990877612
Now ciphertext = [B@34e51b72, and CRC = 1720857375
Now ciphertext = [B@34e51b72, and CRC = 4144065286
Now ciphertext = [B@34e51b72, and CRC = 1992352640
BUILD SUCCESSFUL (total time: 1 second)
代码:
update()
用于增量计算字节序列上的校验和。您的代码在提供给同一crc32
对象上的update()
的所有密文的集中计算crc32
试一试
嗯,真的。我为新字符串使用了一个新的CRC32,现在我得到了与预期相同的输出。
package rc4_crc32;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.zip.CRC32;
public class RC4_CRC32 {
public static void main(String[] args) throws Exception{
byte[] key, ciphertext;
CRC32 c = new CRC32();
javax.crypto.Cipher r;
r = Cipher.getInstance("RC4");
key = "1@m@L33tH@x0r!".getBytes("ASCII");
SecretKeySpec rc4Key = new SecretKeySpec(key, "RC4");
r.init(Cipher.ENCRYPT_MODE, rc4Key);
ciphertext = r.update("Secret!".getBytes("ASCII"));
c.update(ciphertext);
System.out.println("Ciphertext = " + ciphertext + ", and CRC = " + c.getValue());
ciphertext[0] = (byte)0x2c;
c.update(ciphertext);
System.out.println("Now ciphertext = " + ciphertext + ", and CRC = " + c.getValue());
c.update(ciphertext);
System.out.println("Now ciphertext = " + ciphertext + ", and CRC = " + c.getValue());
c.update(ciphertext);
System.out.println("Now ciphertext = " + ciphertext + ", and CRC = " + c.getValue());
c.update(ciphertext);
System.out.println("Now ciphertext = " + ciphertext + ", and CRC = " + c.getValue());
}
}
c.reset()
c.update(ciphertext);
System.out.println("Now ciphertext = " + ciphertext + ", and CRC = " + c.getValue());