我正在尝试使用Crypto ++在ECB模式之上手动设置CTR(但仍然是)。 这个想法是:
对于单个块:只需使用ECB对于多个块,请使用CTR算法 (AFAIK):
//We have n block of plain data -> M PlainData M[n]; key; iv; char *CTR; cipher =""; for(i = 0; i<n; i++ ){ if(i ==0){ CTR = iv; } ei = encryptECB(CTR + i) cipherI = xor(ei, M[i]) cipher += cipherI; }
//我的xor()到XOR两个char数组
void xor(char *s1, char* s2, char *& result, int len){
try{
int i;
for (i = 0; i < len; i++){
int u = s1[i] ^ s2[i];
result[i] = u;
}
result[i] = '\0';
}
catch (...){
cout << "Errp";
}
}
测试1:100%加密++点击率
string auto_ctr(char * s1, long size){
CTR_Mode< AES >::Encryption e;
e.SetKeyWithIV(key, sizeof(key), iv, sizeof(iv));
string cipherZ;
StringSource s(s1, true,
new StreamTransformationFilter(e,
new StringSink(cipherZ), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING
)
);
return cipherZ;
}
测试2:基于ECB的手动点击率
string encrypt(char* s1, int size){
ECB_Mode< AES >::Encryption e;
e.SetKey(key, size);
string cipher;
string s(s1, size);
StringSource ss1(s, true,
new StreamTransformationFilter(e,
new StringSink(cipher), BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING
) // StreamTransformationFilter
); // StringSource
return cipher;
}
static string manual_ctr(char *plain, long &size){
int nBlocks = size / BLOCK_SIZE;
char* encryptBefore = new char[BLOCK_SIZE];
char *ci = new char[BLOCK_SIZE] ;
string cipher;
for (int i = 0; i < nBlocks; i++){
//If the first loop, CTR = IV
if (i == 0){
memcpy(encryptBefore, iv, BLOCK_SIZE);
}
encryptBefore[BLOCK_SIZE] = '\0';
memcpy(encryptBefore, encryptBefore + i, BLOCK_SIZE);
char *buffer = new char[BLOCK_SIZE];
memcpy(buffer, &plain[i], BLOCK_SIZE);
buffer[BLOCK_SIZE] = '\0';
//Encrypt the CTR
string e1 = encrypt(encryptBefore, BLOCK_SIZE);
//Xor it with m[i] => c[i]
xor((char*)e1.c_str(), buffer, ci, BLOCK_SIZE);
//Append to the summary cipher
/*for (int j = 0; j < BLOCK_SIZE/2; j++){
SetChar(cipher, ci[j], i*BLOCK_SIZE + j);
}*/
cipher += ci;
//Set the cipher back to iv
//memcpy(encryptBefore, ci, BLOCK_SIZE);
}
return cipher;
}
这是测试的主要内容:
void main(){
long size = 0;
char * plain = FileUtil::readAllByte("some1.txt", size);
string auto_result = auto_ctr(plain, size);
string manual_result = manual_ctr(plain, size);
getchar();
}
auto_result是:
“YZ +eÞsÂÙ\bü'\x1a¨Ü_ÙR•LD€|å«IIE [w®Ÿg\fT½\ Y7 P!\ r ^ IC†UP \位\ X3 \x1cZï.s%\ x1ei {UMO ...Pä¾õ\ X46 \ R5 \tâýï,ú\x16ç'Qiæ²\x15š€a ^ªê] w ^ ÊNqdŒ¥†¾j%8.Ìù\x6Þ> OI”并[c \ X19"
manual_result是:
“YZ +eÞsÂÙ\bü'\x1a¨Ü_Ù·\x18ýuù\ n \ NL \ X11A \x19À†Žaðƒºñ®GäþŽá•\x11ÇYœf+ ^ Q \ X1A \x13B³'QQμºëÑÌåM\” \ X12 \x115â\x10¿Ô “> S 0‰= \ X18 * \ X1C:²IF'n@ŠŠ¾mGÂzõžÀ\x1eÏ\SëYU¼í'” &GT;
我的工具有什么问题?
答案 0 :(得分:1)
由于您的第一个块似乎工作正常,我只搜索了计数器本身的管理问题,这似乎是我的错误:
memcpy(encryptBefore,encryptBefore + i,BLOCK_SIZE);
这里你试图增加你的IV i
次,我猜,但这不是发生了什么,你做的是试图将encryptBefore
指针复制到encryptBefore+i
的内容中指针跨越BLOCK_SIZE
个字节。这根本不会增加IV,但它适用于第一个块,因为i=0
。
你想要做的是使用CryptoPP::Integer
创建一个大整数用作IV并增加该整数然后使用CryptoPP Integer类中的Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
函数将其转换为字节数组当你需要使用字节而不是整数时。
Ps:执行I / O操作时,我建议您使用十六进制字符串,查看CryptoPP::HexEncoder
和HexDecoder
类,它们都是well documented on CryptoPP wiki。