【问题标题】:Cryptography : AES/CTR Decryption密码学:AES/CTR 解密
【发布时间】:2017-08-09 11:38:00
【问题描述】:

我已经建立了一个服务器和客户端应用程序,用于在AES / CTR算法上进行加密和解密。客户端用C语言构建,服务器端用Java语言构建。但是,加密后的密文无法解密在服务器中,反之亦然。

这里是代码: (客户)

$ 客户端(C):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mcrypt.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>

int encrypt(
   void* buffer,
int buffer_len, /* Because the plaintext could include null bytes*/
char* IV, 
char* key,
int key_len 
){
 MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "ctr", NULL);
 int blocksize = mcrypt_enc_get_block_size(td);
 if( buffer_len % blocksize != 0 ){return 1;}
 mcrypt_generic_init(td, key, key_len, IV);
 mcrypt_generic(td, buffer, buffer_len);
 mcrypt_generic_deinit (td);
 mcrypt_module_close(td);
 return 0;
}

int decrypt(
  void* buffer,
  int buffer_len,
  char* IV, 
  char* key,
  int key_len 
){
  MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "ctr", NULL);
  int blocksize = mcrypt_enc_get_block_size(td);
  if( buffer_len % blocksize != 0 ){return 1;} 
  mcrypt_generic_init(td, key, key_len, IV);
  mdecrypt_generic(td, buffer, buffer_len);
  mcrypt_generic_deinit (td);
  mcrypt_module_close(td);  
  return 0;
 }

void display(char* ciphertext, int len){
 int v;
 for (v=0; v<len; v++){
  printf("%d ", ciphertext[v]);
 }
 printf("\n");
}

int main() {
  MCRYPT td, td2;
  char * plaintext = "test text 123";
  char* IV = "AAAAAAAAAAAAAAAA";
  char *key = "0123456789abcdef";
  int keysize = 16; /* 128 bits */
  char* buffer;
  int buffer_len = 16;
  buffer = calloc(1, buffer_len);
  strncpy(buffer, plaintext, buffer_len);
  printf("==C==\n");
  printf("plain:   %s\n", plaintext);
  encrypt(buffer, buffer_len, IV, key, keysize); 
  printf("cipher:  "); display(buffer , buffer_len);
  decrypt(buffer, buffer_len, IV, key, keysize);
  printf("decrypt: %s\n", buffer);
  return 0;
  }

$ 服务器(Java):

import java.security.MessageDigest; 
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.util.*;
import java.net.*;

public class AESEnc {
  static String IV = "AAAAAAAAAAAAAAAA";
  static String encryptionKey = "Hello World nexg";
  public static void main(String [] args) {
    try {
    InetAddress IPAddress = InetAddress.getByName("192.168.1.7");
    DatagramSocket clientSocket = new        DatagramSocket(5038,IPAddress);
    byte[] data=new byte[2048];
    DatagramPacket packet = new DatagramPacket(data, 2048);
    packet.setLength(2048); 
    System.out.println("Waiting for packet");
    clientSocket.receive(packet);
    String strMessage=new String(packet.getData(),0,packet.getLength());
    System.out.println("Recieved packet");
    System.out.println("==Java==");
    System.out.println("plain:   " + strMessage);
    String decrypted = decrypt(strMessage.getBytes(),encryptionKey);

} catch (Exception e) {
  e.printStackTrace();
} 
}

  public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
    return cipher.doFinal(plainText.getBytes("UTF-8"));
  }

  public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
    return new String(cipher.doFinal(cipherText),"UTF-8");
  }
}

这些是我的服务器和客户端源代码,如果您发现其中有错误,请告诉我。

提前致谢,

P.S : 在解密从客户端发送的消息后,我得到了一些垃圾字符。我没有得到加密的实际消息。

【问题讨论】:

  • 运行此程序是否收到任何错误?
  • 有什么问题?问题是什么?
  • 您如何确定问题出在哪里?
  • "mostly" 没用。如果您的代码没有做它需要做的事情,那么您对它的理解与它实际做的事情之间存在不匹配。如果你想知道它实际上做了什么,你需要调试它。如果您不知道怎么做,请阅读ericlippert.com/2014/03/05/how-to-debug-small-programs。我怎么知道你做错了什么?您对我或其他有能力帮助您的人有什么期望?你想让我为你调试你的代码吗?是什么让您认为这是一个合理的期望?另见stackoverflow.com/help/how-to-ask
  • 版主备注: Be Nice.

标签: java c encryption cryptography aes


【解决方案1】:

您的客户端和服务器看起来不错,但是在从数据包中获取数据并对其进行解密时,您的 UDP 数据包处理存在一个小错误。尝试更改它,我认为您的代码会起作用。 :)

【讨论】:

    【解决方案2】:

    不同的系统有不同的默认值,任何不同都会影响你的解密。检查 everything 是否逐字节相同。在两个系统上显示十六进制值以检查:密钥、nonce/IV、密文字节、明文字节(不是文本)。

    最好不要依赖任何默认值,而是明确指定两个系统上的所有内容。

    一个常见的错误来源是文本到字节的转换,这可能会影响初始明文以及用作输入的密码。

    您的垃圾字符可能是您分配它时位于 C 缓冲区内存中的任何内容。

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 2020-01-20
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多