AES
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。严格地说,AES和Rijndael加密法并不完全一样(虽然在实际应用中二者可以互换),因为Rijndael加密法可以支持更大范围的区块和密钥长度:AES的区块长度固定为128 比特,密钥长度则可以是128,192或256比特;而Rijndael使用的密钥和区块长度可以是32位的整数倍,以128位为下限,256比特为上限。包括AES-ECB,AES-CBC,AES-CTR,AES-OFB,AES-CFB
DES
DES是对称性加密里面常见一种,全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法。密钥长度是64位(bit),超过位数密钥被忽略。所谓对称性加密,加密和解密密钥相同。对称性加密一般会按照固定长度,把待加密字符串分成块。不足一整块或者刚好最后有特殊填充字符。往往跨语言做DES加密解密,经常会出现问题。往往是填充方式不对、或者编码不一致、或者选择加密解密模式(ECB,CBC,CTR,OFB,CFB,NCFB,NOFB)没有对应上造成。常见的填充模式有: ‘pkcs5′,’pkcs7′,’iso10126′,’ansix923′,’zero’ 类型,包括DES-ECB,DES-CBC,DES-CTR,DES-OFB,DES-CFB。
3DES
(又叫Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。密钥长度是128位,192位(bit),如果密码位数少于等于64位,加密结果与DES相同。原版DES容易被破解,新的3DES出现,增加了加密安全性,避免被暴力破解。它同样是对称性加密,同样涉及到加密编码方式,及填充方式。包括3DES-ECB,3DES-CBC,3DES-CTR,3DES-OFB,3DES-CFB
个人啰嗦
这3种加密方式是目前WEB,JAVA端上面最常用的,
但是往往易语言计算出来的跟实际的有时往往差别很多。也已经混扰我很久,至今还是一头雾水。
下遍了所有易语言,JS调用。有些都无法跟java端上面正确解密出来的对应上
有些会出现一些莫名其妙的BUG,具体引起的原因也大致清楚,但是没有去深究了。
他们所用也非变异算法,下面网页也能计算出来
网页端:http://tool.chacuo.net/crypt3des
也尝试过PHP+jar的方式来调用解密,但是浪费资源跟效率
今天刚好接了个单,又碰巧又是遇到这问题,所以发出来记录一下。
最后我还是放弃了在易语言或者JAR上面突破,转战C++,曲线求国
通过C++上面调用Crypto++这个开源库,从而调用标准的加解密。
生成DLL把接口放出来之后通过易调用,完全解决平台下的加解密统一。
就是生成出来的DLL有点大,7MB左右
例子代码
代码很挫,勿见笑。
#include "mydll.h"
#include "RandPool.h"
#include "files.h"
#include "afx.h"
#include <aes.h>
#include <rsa.h>
#include <des.h>
#include <base64.h>
#include <Hex.h> // StreamTransformationFilter
#include <modes.h> // CFB_Mode //ECB_Mode<BC>, CTR_Mode<BC>, CBC_Mode<BC>, CFB_FIPS_Mode<BC>, OFB_Mode<BC>, GCM<AES>
#include <iostream> //std:cerr
#include <sstream> //std::stringstream
#include <string>
using namespace std;
using namespace CryptoPP;
#pragma comment( lib, "cryptlib.lib" )
std::string CBC_AESEncryptStr(unsigned char* key, unsigned char* iv, const char *plainText)
{
std::string outstr;
int keysize=16;
CBC_Mode<AES>::Encryption Encryptor1(key,keysize,iv);
StringSource( plainText,
true,
new StreamTransformationFilter( Encryptor1,
new HexEncoder(new StringSink( outstr) ),
BlockPaddingSchemeDef::BlockPaddingScheme::DEFAULT_PADDING,//DEFAULT_PADDING NO_PADDING ONE_AND_ZEROS_PADDING
true)
);
return outstr;
}
std::string CBC_AESDecryptStr(unsigned char* key, unsigned char* iv, const char *plainText)
{
std::string outstr;
CBC_Mode<AES>::Decryption Decryptor1(key, 16,iv);
HexDecoder decryptor(new StreamTransformationFilter(Decryptor1, new StringSink(outstr)));
decryptor.Put((byte *)plainText, strlen(plainText));
decryptor.MessageEnd();
return outstr;
}
std::string CBC_DES3EncryptStr(unsigned char* key, unsigned char* iv, const char *plainText)
{
std::string outstr;
CBC_Mode<DES_EDE3>::Encryption Encryptor1(key, DES_EDE3::MAX_KEYLENGTH,iv);
StringSource(plainText,
true,
new StreamTransformationFilter(Encryptor1,
new HexEncoder(new StringSink(outstr)),
BlockPaddingSchemeDef::BlockPaddingScheme::DEFAULT_PADDING,//DEFAULT_PADDING NO_PADDING ONE_AND_ZEROS_PADDING ZEROS_PADDING
true)
);
return outstr;
}
std::string CBC_DES3DecryptStr(unsigned char* key, unsigned char* iv, const char *plainText)
{
std::string outstr;
CBC_Mode<DES_EDE3>::Decryption Decryptor1(key, DES_EDE3::MAX_KEYLENGTH,iv);
HexDecoder decryptor(new StreamTransformationFilter(Decryptor1, new StringSink(outstr)));
decryptor.Put((byte *)plainText, strlen(plainText));
decryptor.MessageEnd();
return outstr;
}
//通用AES_CBC解密
char* __stdcall aes_cbcde(const char * plainText,const char * key,const char * iv){
unsigned char* k;
unsigned char* i;
k = (unsigned char*)key;
i=(unsigned char*)iv;
string cipherText;
cipherText = CBC_AESDecryptStr(k, i, plainText);
char *data;
int len = cipherText.length();
data = (char *)malloc((len+1)*sizeof(char));
strcpy(data, cipherText.c_str());
return data;
}
void GenerateRSAKey(unsigned int keyLength,string &strPriy,string &strPub, const char *seed);
string RSAEncryptString(const string &strPub, const char *seed, const char *message);
string RSADecryptString(const char *privFilename, const char *ciphertext);
RandomPool & GlobalRNG();
char* __stdcall ras(const char* plainText,const char* pri,const char* pub,const char* sed)
{
string pubb = pub;
string privv = pri;
char seed[1024]= {0};
strcpy(seed,sed);
GenerateRSAKey(1024, privv, pubb, seed);
// RSA加解密
char message[1024] = {0};
strcpy(message,"13692228014");
string encryptedText = RSAEncryptString(pubb, seed, message);// RSA加密
//string decryptedText = RSADecryptString(priKey, encryptedText.c_str());// RSA解密
string ok =encryptedText;
char* data;
int len = ok.length();
data = (char *)malloc((len+1)*sizeof(char));
strcpy(data, ok.c_str());
return data;
}
//------------------------
// 生成RSA密钥对
//------------------------
void GenerateRSAKey(unsigned int keyLength,string &strPriy,string &strPub, const char *seed)
{
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);
HexEncoder PrivateEncoder(new StringSink(strPriy));
priv.DEREncode(PrivateEncoder);
PrivateEncoder.MessageEnd();
RSAES_OAEP_SHA_Encryptor pub(priv);
HexEncoder PublicEncoder(new StringSink(strPub));
pub.DEREncode(PublicEncoder);
PublicEncoder.MessageEnd();
}
//------------------------
// RSA加密
//------------------------
string RSAEncryptString(const string &strPub, const char *seed, const char *Plaintext)
{
StringSource PublicKey(strPub, true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor pub(PublicKey);
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
string result;
StringSource(Plaintext, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));
return result;
}
//------------------------
// RSA解密
//------------------------
string RSADecryptString(const char *privFilename, const char *ciphertext)
{
FileSource privFile(privFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor priv(privFile);
string result;
StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
return result;
}
//------------------------
// 定义全局的随机数池
//------------------------
RandomPool & GlobalRNG()
{
static RandomPool randomPool;
return randomPool;
}