有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

crc Java在同一字符串上返回不同的CRC32结果

我正在玩RC4和CRC32,试图模拟一个稍微翻转的攻击,我对Java中CRC32的行为摸不着头脑。据我所知,这应该是基于多项式计算的确定性结果。然而,我看到的是,无论文本是否发生了变化,CRC都以一种不可预测的方式发生变化。Javadoc只是在字节数组上声明update():使用指定的字节数组更新CRC-32校验和,对于getValue():返回CRC-32值这里是否涉及某种盐或PRF,而我没有对此进行说明(我不这么认为)

输出:

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)

代码:

 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());
    }    
}

共 (1) 个答案

  1. # 1 楼答案

    update()用于增量计算字节序列上的校验和。您的代码在同一CRC32对象上提供给update()的所有密文的集中计算crc32

    试试看

    c.reset()
    c.update(ciphertext);   
    System.out.println("Now ciphertext = " + ciphertext + ", and CRC = " + c.getValue());