安卓(客户端)上的Aes加密和服务器上的C++解密

Aes encryption on Android(client) and Decryption on Server with C++

本文关键字:服务器 C++ 解密 加密 客户端 上的 Aes 安卓      更新时间:2023-10-16

我在android应用程序上使用AES加密,遇到了一个问题。我希望我的应用程序使用Aes_256_Cbc算法加密密码。加密的字符串每次都必须不同,这就是为什么我每次都需要一个真正随机的iv。我必须加密这个词的代码是:

import android.content.Context;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Encryptor
{
Context context;
private static final String WORD = "aefbjolpigrschnx";
private static final String KEY = "kumyntbrvecwxasqertyplmqazwsxedc";
private final static String HEX = "0123456789ABCDEF";

public Encryptor(Context c)
{
    context = c;
}
public String getEncryptedPasswd()
{
    String ivHex = "";
    String encryptedHex = "";
    try 
    {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        ivHex = toHex(iv);
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("UTF-8"), "AES");

        Cipher encryptionCipher = Cipher.getInstance("AES/CBC");
        encryptionCipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
        byte[] encryptedText = encryptionCipher.doFinal(WORD.getBytes("UTF-8"));
        encryptedHex = toHex(encryptedText);
    } 
    catch (Exception e) 
    {
    }
    return ivHex + encryptedHex;
}
public static String toHex(byte[] buf) 
{
    if (buf == null)
    {
        return "";
    }       
    StringBuffer result = new StringBuffer(2 * buf.length);
    for (int i = 0; i < buf.length; i++)
    {
            result.append(HEX.charAt((buf[i]>>4)&0x0f)).append(HEX.charAt(buf[i]&0x0f));
    }
    return result.toString();
}

我的应用程序多次调用函数getEncryptedPasswd(),每次都会按照预期给出不同的十六进制输出。然后它将加密的密码发送到服务器,我的代码必须使用C++。但是,当我尝试使用openssl解密密码时,我不会得到正确的输出。有人能帮我吗?我在服务器上的代码是:

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <openssl/aes.h>
#define KEY "kumyntbrvecwxasqertyplmqazwsxedc"
using namespace std;
unsigned int hex2bin(char *ibuf, unsigned char *obuf, unsigned int ilen)
{
unsigned int i;
unsigned int j;
unsigned int by = 0;
unsigned char ch;
// process the list of characaters
for (i = 0; i < ilen; i++)
{
        ch = toupper(*ibuf++);
    // do the conversion
        if(ch >= '0' && ch <= '9')
    {
        by = (by << 4) + ch - '0';
    }    
    else if(ch >= 'A' && ch <= 'F')
    {
            by = (by << 4) + ch - 'A' + 10;
    }
        else
    {
        memcpy(obuf, "ERROR", 5);
            return 0;
        }
        // store a byte for each pair of hexadecimal digits
        if (i & 1) 
    {
            j = ((i + 1) / 2) - 1;
            obuf[j] = by & 0xff;
        }
}
return (j+1);
}
string iv_str = auth.substr(0, 16);
string enc_str = auth.substr(16);
char *iv_buf = new char [iv_str.length() + 1];
strcpy(iv_buf, iv_str.data());
char *enc_buf = new char [enc_str.length() + 1];
strcpy(enc_buf, enc_str.data());
unsigned long ilen;
unsigned char iv[16];
unsigned char enc_word[16]; // hex decrypt output
unsigned char word[16]; // decrypt output
unsigned char enc_key[] = KEY;
AES_KEY aeskeyDec;
    AES_set_decrypt_key(enc_key, 256, &aeskeyDec);
    ilen = hex2bin(enc_buf, enc_word, (int) strlen(enc_buf));
hex2bin(iv_buf, iv, (int) strlen(iv_buf));
    AES_cbc_encrypt(enc_word, word, ilen, &aeskeyDec, iv, AES_DECRYPT);

我是一个C++爱好者,而不是java,所以我不能对你的这一行发表评论,只能说它看起来可以满足你的需要:

Cipher encryptionCipher = Cipher.getInstance("AES/CBC");

但为了防止出现更多问题,请注意,我可以确认Java默认使用ECB模式。和你一样,我也必须用C++对一些用Java加密的文本进行AES解密。我花了一段时间才弄清楚发生了什么:如何使用OpenSSL解密Java AES加密的数据?