简体   繁体   English

将密文的确切大小隐藏在图像中的问题,稍后将其提取然后解密

[英]Problem in hiding exact size of cipherText inside image, later extract it and then decrypt it

I am having a problem in storing exact size of encrypted message inside an image.我在将加密消息的确切大小存储在图像中时遇到问题。

What I want to do is that I want to encrypt a message with AES cipher and then I want to hide each byte of message in a random pixel by replacing only one component(either red or green or blue component) of pixel with the message byte.我想要做的是我想用AES密码加密一条消息,然后我想通过用消息字节仅替换像素的一个组件(红色或绿色或蓝色组件)来隐藏随机像素中的每个消息字节.

I have done that but I am unable to store with the same size of ciphertext.我已经这样做了,但我无法存储相同大小的密文。

First I have done encryption of message with the help of a key using AES/CBC/PKCS5Padding using Java Cryptographic Extension.首先,我使用 AES/CBC/PKCS5Padding 使用 Java 加密扩展,借助密钥对消息进行了加密。

byte[] cipherText = encrypt(message, keyBytes);

Then I had to convert cipherText to UTF-8 byte array and then store inside image.然后我必须将 cipherText 转换为 UTF-8 字节数组,然后存储在图像中。 If not I will not be able to decrypt it later.如果不是,我以后将无法解密它。 If I don't encode properly, I cannot decrypt it after extraction.如果我没有正确编码,我无法在提取后解密它。 The problem here is that the size of messageBytes > size of cipherText.这里的问题是 messageBytes 的大小 > cipherText 的大小。 It takes more space than the encrypted message ie cipherText.它比加密消息(即密文)占用更多空间。 Example: if cipherText is 32 bytes, I get 148 bytes of messageBytes.示例:如果 cipherText 是 32 个字节,我得到 148 个字节的 messageBytes。

But I want to store the byte array of size cipherText and be able to extract it later.但我想存储大小为 cipherText 的字节数组,以后可以提取它。

String encryptedMessage = Arrays.toString(cipherText);
byte[] messageBytes = encryptedMessage.getBytes(StandardCharsets.UTF_8);

Now 148 bytes are stored in the image.现在图像中存储了 148 个字节。 Then I have stored each byte inside one of the component(r or g or b) of random pixel.然后我将每个字节存储在随机像素的一个分量(r 或 g 或 b)中。

for(int i=0; i<messageBytes.length; i++) {
    storeInsidePixel(image, messageBytes[i], secureRandom)
}

In the extraction part, I retrieved the messageBytes back and converted to encryptedMessage to get the hidden cipherText.在提取部分,我取回了 messageBytes 并转换为 encryptedMessage 以获取隐藏的密文。

String encryptedMessage = new String(byteArray, StandardCharsets.UTF_8);
String newString = encryptedMessage.substring(1, encryptedMessage.length()-1);
String[] stringArray = newString.split(", ");

byte[] cipherText = new byte[stringArray.length];
for(int i=0; i<stringArray.length; i++) {
    cipherText[i] = (byte) (Integer.parseInt(stringArray[i]));
}

Then I decrypted it using the same cipher and key.然后我使用相同的密码和密钥对其进行解密。

String output = decrypt(cipherText, keyBytes);

I want to store the byte array of size cipherText and be able to extract it with same size and decrypt it later.我想存储大小为 cipherText 的字节数组,并能够以相同的大小提取它并稍后解密。

If I don't encode to utf-8 and store it, I don't get the same output because if the cipherText stored in image is 1040 bytes and later extract it, I will get the same byte values but the size of byte array is just 205 bytes and decryption fails.如果我不编码为 utf-8 并存储它,我不会得到相同的 output 因为如果存储在图像中的密文是 1040 字节然后提取它,我将得到相同的字节值,但字节数组的大小只是205 字节,解密失败。

Is there any to store exactly 1040 bytes of cipherText in image and extract the same 1040 bytes of cipherText and be able to decrypt it?有没有可以在图像中准确存储 1040 字节的密文并提取相同的 1040 字节密文并能够解密它? Is there any way to get it like that?有什么办法可以得到它吗?

Then I had to convert cipherText to UTF-8 byte array and then store inside image.然后我必须将 cipherText 转换为 UTF-8 字节数组,然后存储在图像中。 If not I will not be able to decrypt it later.如果不是,我以后将无法解密它。 If I don't encode properly, I cannot decrypt it after extraction.如果我没有正确编码,我无法在提取后解密它。

No, here is where you go wrong.不,这里是你 go 错误的地方。 Your cipherText should be binary already (as shown by byte[] cipherText = encrypt(message, keyBytes); ), and image formats are binary as well.您的cipherText应该已经是二进制的(如byte[] cipherText = encrypt(message, keyBytes); ),并且图像格式也是二进制的。 So you should never have to encode it again.因此,您永远不必再次对其进行编码。 Just directly use cipherText .直接使用cipherText

The problem here is that the size of messageBytes > size of cipherText .这里的问题是messageBytes的大小 > cipherText的大小。 It takes more space than the encrypted message ie cipherText .它比加密消息(即cipherText )占用更多空间。 Example: if cipherText is 32 bytes, I get 148 bytes of messageBytes .示例:如果cipherText是 32 个字节,我得到 148 个字节的messageBytes ... String encryptedMessage = Arrays.toString(cipherText); ... String encryptedMessage = Arrays.toString(cipherText);

Yeah, well, duh.是的,嗯,呵呵。 If you do Arrays.toString then you will get a textual representation of the array, including separators and whatnot, all which you can do without.如果您执行Arrays.toString ,那么您将获得数组的文本表示,包括分隔符和诸如此类的东西,所有这些您都可以不用。

If I don't encode to utf-8 and store it, I don't get the same output because if the cipherText stored in image is 1040 bytes and later extract it, I will get the same byte values but the size of byte array is just 205 bytes and decryption fails.如果我不编码为 utf-8 并存储它,我不会得到相同的 output 因为如果存储在图像中的密文是 1040 字节然后提取它,我将得到相同的字节值,但字节数组的大小只是205 字节,解密失败。

We cannot see why you get a smaller value back here, but this is what needs to be solved - and it should not be solved by encoding / decoding.我们看不出为什么你会在这里得到一个较小的值,但这是需要解决的问题——它不应该通过编码/解码来解决。 Feel free to post a follow-up question with a minimal reproducible example .随意发布一个带有最小可重复示例的后续问题。

Is there any to store exactly 1040 bytes of cipherText in image and extract the same 1040 bytes of cipherText and be able to decrypt it?有没有可以在图像中准确存储 1040 字节的密文并提取相同的 1040 字节cipherText并能够解密它? Is there any way to get it like that?有什么办法可以得到它吗?

There are two problems here: length indication -> you need some way to indicate the size of the ciphertext.这里有两个问题:长度指示->您需要某种方式来指示密文的大小。 Furthermore, if you use the same key for multiple messages then you would need to store the IV as well.此外,如果您对多条消息使用相同的密钥,那么您还需要存储 IV。

For detecting the size: you can eg prefix a 16 bit ( short ) length indicator if your ciphertext doesn't exceed 65536 bytes.用于检测大小:如果您的密文不超过 65536 字节,您可以例如为 16 位( short )长度指示符添加前缀。 Or, for CBC, you could use it to count the number of blocks as the unpadding will then reveal the exact size.或者,对于 CBC,您可以使用它来计算块的数量,因为 unpadding 将显示确切的大小。 This would let you store 16 times larger ciphertext.这将使您存储 16 倍大的密文。

With a 2 byte size indicator and 16 byte IV you'd grow your ciphertext by 18 bytes.使用 2 字节大小指示符和 16 字节 IV,您可以将密文增加 18 字节。 Since your ciphertext is already up to 16 bytes larger than the plaintext this may be a small enough size increase for you to handle.由于您的密文已经比明文大 16 个字节,这可能是您可以处理的足够小的大小增加。

Thank you for taking your time in answering.感谢您花时间回答。 But I am really sorry.但我真的很抱歉。 I assumed some things incorrectly and the question needs to be changed completely.我假设一些事情不正确,问题需要完全改变。 But Now, I got the result without encoding and have no question.但是现在,我得到了没有编码的结果,毫无疑问。 By simply storing length of ciphertext first and then the ciphertext.只需先存储密文的长度,然后再存储密文。 I can extract it and decrypt it later.我可以提取它并稍后解密。 Just 4 extra bytes are added.仅添加了 4 个额外字节。 I cannot delete this question or close it down.我无法删除或关闭此问题。 Please close this question if anyone is able to.如果有人可以,请关闭此问题。

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

相关问题 如何仅对NimbusDS使用密文进行解密? - How to decrypt using only ciphertext for NimbusDS? 从PFX文件中读取私钥,并使用该私钥解密密文 - Read private key from PFX file, decrypt the ciphertext with that private key 无法在Blackberry中解密php代码的AES密文 - Unable to decrypt AES's ciphertext of php code in blackberry 上传不同的图片尺寸或单个尺寸以便稍后处理? - Upload different image sizes or a single size to be processed later? 在Android中隐藏UI元素后如何最小化滚动视图的大小? - How to minimize size of scrollview after hiding UI elements inside in Android? 使用twitter4j检索确切的图像大小 - Retrieve exact image size using twitter4j 如何将textview转换为具有确切大小的图像,如屏幕截图 - how to convert textview into image with exact size like screenshot Apache FOP,将图像缩小到精确的大小,以毫米为单位 - Apache FOP, scale image down to exact size in millimeters 将图像保存在特定的文件夹内,并将路径存储在mysql中,以便以后显示 - Save image inside specific folder and store path in mysql to display later 如何将图像(应该是精确大小)写入外部存储 - How to write an image (should be exact size) to an external storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM