简体   繁体   English

我的加密逻辑出了什么问题?

[英]What's wrong with my encryption logic?

I thought I had my encryption classes sorted but a problem has arisen. 我以为我的加密类已排序,但是出现了问题。

My client/server model is simple. 我的客户端/服务器模型很简单。 The client connects to the server, sends any messages in the message queue and then disconnects. 客户端连接到服务器,发送消息队列中的所有消息,然后断开连接。

The first message that is sent is absolutely fine, but any messages sent after that it appears that the first block (first 16 characters of the string) is wrong. 发送的第一条消息绝对正确,但是此后发送的所有消息似乎都显示出第一个块(字符串的前16个字符)是错误的。 The strange thing is that all the other blocks appear to be fine, which is very strange as I am CBC. 奇怪的是,其他所有块似乎都很好,因为我是CBC,这很奇怪。

Encryption settings: 加密设置:

encryptionAlgorithm = "AES";
encryptionBitCount = 256;
encryptionMessageLength = 176;
hashingAlgorithm = "PBEWithSHA256And256BitAES-CBC-BC";
hashingCount = //some number;
cipherTransformation = "AES/CBC/PKCS7Padding";
salt = //some bytes;

My logic for client works as following: 我的客户端逻辑如下:

  • Init cipher via: cipher.init(Cipher.ENCRYPT_MODE, cipherInitKey); 通过以下cipher.init(Cipher.ENCRYPT_MODE, cipherInitKey);初始化密码: cipher.init(Cipher.ENCRYPT_MODE, cipherInitKey);
  • Connect to server 连接到服务器

for each message is message queue 每个消息就是消息队列

  • Generate and write IV to socket via: stream.write(cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV()); 通过以下方式生成IV并将其写入套接字: stream.write(cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV());
  • Generate a write ciphertext to socket via: stream.write(cipher.doFinal(message)); 通过以下stream.write(cipher.doFinal(message));生成套接字的写密文: stream.write(cipher.doFinal(message));

end for 结束于

  • Disconnect 断开

My logic for server works as following: 我的服务器逻辑如下:

for each message received 对于收到的每条消息

  • Read IV from socket (16 bytes). 从套接字读取IV(16个字节)。
  • Init the cipher by: cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); 通过以下cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));初始化密码: cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
  • Read the message from socket (its a fixed size). 从套接字(固定大小)中读取消息。 (176bytes) (176字节)
  • Decipher message via cipher.doFinal(message) 通过cipher.doFinal(message)解密消息cipher.doFinal(message)

end for 结束于

Strange thing is that it works perfectly locally with a java client and server. 奇怪的是,它与Java客户端和服务器在本地完美协作。 But when I put the server on my VPS and have an andriod client it breaks. 但是,当我将服务器放置在VPS上并拥有一个Andriod客户端时,它就会损坏。

Update: It appears that it may actually be that I am getting an extra block at the beginning of message - doing more testing. 更新:看来,实际上可能是我在消息开始时遇到了一个额外的障碍-做更多测试。

You probably get the IV at the beginning. 您可能在一开始就获得IV。 You should use the first bytes as IV, then decrypt with the key and the given IV. 您应该将第一个字节用作IV,然后使用密钥和给定的IV进行解密。 This is a common technique, and will display exactly the problem as you are describing. 这是一种常见的技术,可以按照您的描述准确显示问题。 You might even skip the first output block and simply use the following blocks as plain text (although that's called hacking). 您甚至可以跳过第一个输出块,而仅将以下块用作纯文本(尽管这称为黑客攻击)。 One thing is sure: if your first block is garbage, then the prolbem is the IV. 可以肯定的是:如果您的第一个块是垃圾,那么问题就是IV。

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

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