密码学:AES / CTR解密

时间:2017-08-09 11:38:00

标签: java c encryption cryptography aes

我构建了一个服务器和客户端应用程序,用于加密和解密AES / CTR算法。客户端用C语言构建,服务器端用Java语言构建。但加密密文不能解密在服务器中,反之亦然。

这是代码: (客户端)

$ Client(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;
  }

$ Server(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:在解密客户端发送的消息后,我收到了一些垃圾字符。我没有收到加密的实际消息。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

不同的系统有不同的默认值,任何差异都会破坏你的解密。检查所有是否是逐字节的相同。显示两个系统上的十六进制值以检查:key,nonce / IV,cyphertext bytes,plaintext bytes(not text)。

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

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

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