简体   繁体   English

如何解密密文分区-AES,Java

[英]how to decrypt partitions of cipertext - AES, Java

I , wrote java method, which encrypts - decrypts data. 我写了java方法来加密-解密数据。

    String message = "Hello world 123456";

    // Creating Key. Key of size = 128
    byte [] raw ={-49, -44, 51, -114, 58, 79, 83, -38, 107, 64, 67, -108, -52, 109, 85, 77};
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    // Encript
    byte[] encrypted = cipher.doFinal(message.getBytes());
    System.out.println("encripted: " + Arrays.toString(encrypted));
    System.out.println("encripted: "+ asHex(encrypted));

    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decript = cipher.doFinal(encrypted, 0, 32);

Everything works well! 一切正常! encrypted bytes size is 32. That is correct. 加密的字节大小为32。是正确的。 But can I dencrypt part to part (block to block - for instance, decrypt first block, then second) ? 但是我可以对部分进行解密(块对块-例如,先解密第一个块,然后解密第二个)吗? I mean to decript firstly 16 byte and then second 16 byte and etc.. 我的意思是先描述16个字节,然后再描述16个字节,依此类推。

might be something like this. 可能是这样的。

byte[] decript = cipher.doFinal(encrypted, 0, 16);

I have such error: 我有这样的错误:

javax.crypto.BadPaddingException: Given final block not properly padded

In the other word, can I decrypt AES cipher text, block to block? 换句话说,我可以逐块解密AES密文吗?

Update: I have encrypted video file. 更新:我已经加密了视频文件。 It is for about 1 gb. 这大约是1 GB。 I want to write my video player, which will open my file. 我想写我的视频播放器,它将打开我的文件。 If I will decript full video there will be memory problem. 如果我要录制完整的视频,将会出现内存问题。 So I will give bytes of stream step to step to my player (block to block). 因此,我将逐步将流字节提供给播放器(逐块)。 Can I use AES for that? 我可以使用AES吗?

You are currently encrypting using ECB mode encryption. 您当前正在使用ECB模式加密进行加密。 The algorithm string for Cipher , "AES" , uses the provider default for mode of encryption and padding. Cipher的算法字符串"AES"使用提供程序默认的加密和填充模式。 For the SunJCE that translates to "AES/ECB/PKCS5Padding" (which actually performs PKCS#7 padding). 对于转换为"AES/ECB/PKCS5Padding" (实际上执行PKCS#7填充)的SunJCE。

ECB encrypts decrypts blocks of 16 bytes. ECB加密解密16字节的块。 This means that the message is conceptually converted to a concatenation of 16 byte blocks, and each block is then encrypted separately. 这意味着将消息从概念上转换为16个字节的块的串联,然后分别加密每个块。 As each block is encrypted separately, you can also decrypt the blocks separately. 由于每个块都是分别加密的,因此您也可以分别解密这些块。 That means starting at a block boundary, and then decrypting x times the block size. 这意味着从块边界开始,然后解密x块大小。 As you don't want to perform the PKCS#7 unpadding until you reach the last block, you should use an update method instead of a doFinal method (or you can use "AES/ECB/NoPadding" ). 由于在到达最后一个块之前,您不希望执行PKCS#7的填充操作,因此应该使用update方法而不是doFinal方法(或者可以使用"AES/ECB/NoPadding" )。

If you want to encrypt files you should definitely take a look at java.nio , and Cipher methods that use ByteBuffer as input / output. 如果要加密文件,则一定要看一下java.nio和使用ByteBuffer作为输入/输出的Cipher方法。


Note that this is only a direct answer; 注意,这只是直接答案; you should not be using ECB mode encryption. 您不应使用ECB模式加密。 Use CTR with a unique IV (nonce) per key/file combination instead to provide confidentiality of the stream. 可以将CTR与每个密钥/文件组合的唯一IV(即时)结合使用,以提供流的机密性。

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

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