AES密钥生成问题

玉宇逍遥 2011-08-17 10:30:30
现在在做项目,需要使用Openssl的AES算法进行加密和解密。JAVA可以使用:

//KeyGenerator提供对称密钥生成器的功能,支持各种算法
KeyGenerator keygen = KeyGenerator.getInstance("AES");
//SecretKey负责保存对称密钥
SecretKey deskey = keygen.generateKey();

这个自动生成密钥,但是VC使用Openssl该怎么做?
JAVA默认支持的是128bit 但是也可以通过安装一个SUN提供的工具生成192bit和256bit的KEY
请问用VC该怎么实现JAVA这样的自动密钥的生成?
在线急等!
...全文
1893 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
玉宇逍遥 2011-08-18
  • 打赏
  • 举报
回复
有没有人了解C++怎么实现JAVA中下面的两句话啊:

//KeyGenerator提供对称密钥生成器的功能,支持各种算法
KeyGenerator keygen = KeyGenerator.getInstance("AES");
//SecretKey负责保存对称密钥
SecretKey deskey = keygen.generateKey();

玉宇逍遥 2011-08-18
  • 打赏
  • 举报
回复
string strkey;
BASE64Encoder base64E = new BASE64Encoder();
strkey = base64E.encode(deskey.getEncoded());

这一段我懂该怎么做,但是前面生成deskey的这一段该怎么用C++和Openssl来实现?
玉宇逍遥 2011-08-18
  • 打赏
  • 举报
回复

//KeyGenerator提供对称密钥生成器的功能,支持各种算法
KeyGenerator keygen = KeyGenerator.getInstance("AES");
//SecretKey负责保存对称密钥
SecretKey deskey = keygen.generateKey();
//打印密钥
string strkey;
BASE64Encoder base64E = new BASE64Encoder();
strkey = base64E.encode(deskey.getEncoded());

JAVA中的这段话用openssl中的AES和Base64该怎么实现?
玉宇逍遥 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 rendao0563 的回复:]

aes.h
C/C++ code

#pragma once

#include "OpenSSL/aes.h"

//wrapper class for AES encryption

class CAES
{
public:
CAES(void);
~CAES(void);

void Decrypt(const unsigned char……
[/Quote]

你说的自动生成的密钥是哪个函数?你的函数都需要除const int bits的其他的输入。能帮忙解释一下你那些函数的作用吗?
shn521 2011-08-17
  • 打赏
  • 举报
回复
帮顶啦
rendao0563 2011-08-17
  • 打赏
  • 举报
回复
aes.h

#pragma once

#include "OpenSSL/aes.h"

//wrapper class for AES encryption

class CAES
{
public:
CAES(void);
~CAES(void);

void Decrypt(const unsigned char *in, int length, unsigned char *out);
void Encrypt(const unsigned char *in, int length, unsigned char *out);

int SetDecryptKey(const unsigned char *userKey, const int bits);
int SetEncryptKey(const unsigned char *userKey, const int bits);

protected:
void CryptData(const unsigned char *in, unsigned char *out, int length, const int enc);

protected:
AES_KEY m_aesEncKey;
AES_KEY m_aesDecKey;

public:
unsigned char m_userDecKey[128];
unsigned char m_userEncKey[128];
};


aes.cpp

#include "aes.h"

CAES::CAES(void)
{
}

CAES::~CAES(void)
{
}

void CAES::Decrypt(const unsigned char *in, int length, unsigned char *out)
{
CryptData(in, out, length, AES_DECRYPT);
}

void CAES::Encrypt(const unsigned char *in, int length, unsigned char *out)
{
CryptData(in, out, length, AES_ENCRYPT);
}

int CAES::SetDecryptKey(const unsigned char *userKey, const int bits)
{
//return AES_set_decrypt_key(userKey, bits, &m_aesDecKey);

memcpy(m_userDecKey, userKey, bits);
return 0;
}

int CAES::SetEncryptKey(const unsigned char *userKey, const int bits)
{
//return AES_set_encrypt_key(userKey, bits, &m_aesEncKey);
memcpy(m_userEncKey, userKey, bits);
return 0;
}

void CAES::CryptData(const unsigned char *in, unsigned char *out, int length, const int enc)
{
AES_KEY aesKey;
if (AES_ENCRYPT == enc)
AES_set_encrypt_key(m_userEncKey, 128, &aesKey);
else
AES_set_decrypt_key(m_userDecKey, 128, &aesKey);

unsigned long len = length;
unsigned char tmp[AES_BLOCK_SIZE];

while (len >= AES_BLOCK_SIZE)
{
memset(tmp,0,AES_BLOCK_SIZE);
memcpy(tmp,in,AES_BLOCK_SIZE);

if (AES_ENCRYPT == enc)
{
AES_encrypt(tmp, out, &aesKey);
}
else
{
AES_decrypt(tmp, out, &aesKey);
}

len -= AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE;
out += AES_BLOCK_SIZE;
}

if (len)
{
memset(tmp,0,AES_BLOCK_SIZE);
memcpy(tmp,in,AES_BLOCK_SIZE);
if (AES_ENCRYPT == enc)
{
AES_encrypt(tmp, tmp, &aesKey);
}
else
{
AES_decrypt(tmp, tmp, &aesKey);
}
memcpy(out, tmp, AES_BLOCK_SIZE);
}
}
chunyou128 2011-08-17
  • 打赏
  • 举报
回复
http://www.mathmagic.cn/bbs/read.php?tid=10054
玉宇逍遥 2011-08-17
  • 打赏
  • 举报
回复
难道要沉吗?有人给点提点吗?谢谢了!

19,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧