安卓(客户端)上的Aes加密和服务器上的C++解密
Aes encryption on Android(client) and Decryption on Server with C++
我在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加密的数据?
相关文章:
- "unknown ca"自生成的 CA、证书和客户端/服务器
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 有没有办法知道Tracer是否成功地完全连接到了jaegerclientcpp中的jaeger后端服务器
- 从服务器传输到客户端的消息不会出现
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- OpenSSL TLS服务器-使用客户端证书白名单
- 我正在开发服务器,ip作为参数传递不起作用
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- 提升 Asio TCP 服务器 处理多个客户端
- 如何在 Minecraft 服务器上执行命令
- 调试编译的服务器在数据库打开时崩溃
- C++套接字客户端到 Python 服务器未创建连接
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 将相机数据从服务器实时流式传输到客户端
- 为什么创建服务器后C++ httplib 库阻塞了我的主线程?
- 每个服务器请求的内存预算
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- 使用嵌入式猫鼬服务器托管应用程序
- NodeJs 服务器充斥着 UDP 广播,不发送响应