gpt4 book ai didi

java - 密码学 : AES/CTR Decryption

转载 作者:太空宇宙 更新时间:2023-11-04 01:49:56 25 4
gpt4 key购买 nike

我搭建了一个服务端和客户端应用,用于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:解密从客户端发送的消息后,我得到了一些垃圾字符。我没有得到加密的实际消息。

最佳答案

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

关于java - 密码学 : AES/CTR Decryption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45589595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com