大家知道有没有输出密文结果为定长的加密算法,要求可逆的.谢谢

caferd 2005-05-18 04:26:53
同上
...全文
231 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
吹泡泡的小猫 2005-05-19
  • 打赏
  • 举报
回复
DES,BlowFish等都是定长输入,定长输出的加密算法
AntonlioX 2005-05-19
  • 打赏
  • 举报
回复
DES 好像可以吧
柯本 2005-05-18
  • 打赏
  • 举报
回复
抄来的,你试试
encrypt.cpp
Code:

#include <string>
#include <cstring>

using namespace std;

void fw_swap(unsigned char* data, int first, int second)
{
unsigned char temp;
temp = data[second];
data[second] = data[first];
data[first] = temp;
}

void Encrypt(string data, string* key, string* crypted_data)
{
*crypted_data = "";
int current_run = 1; // keep track of whether this is an odd or even run
// 0 = even, 1 = odd
// Set up a loop to deal with 8 bytes (64 bits) of data at a time
for(string::size_type i = 0; i < data.length(); i += 8)
{
char* block_temp = new char[8];
data.substr(i,8).copy(block_temp, 8);
unsigned char* block = new unsigned char[8];
memset(block, 0, 8);
memcpy(block_temp, block, 8);
delete block_temp;
unsigned char* curr_key = new unsigned char[8];
char* trans_map = new char;
/*
Note to anyone reading this:
The if structure below has a lot of duplicated code, but this is
only an example so I can't be bothered to make it more efficient!
Its only a matter of moving a lot of it out of the if, else structure
but I'm trying to get this finished!!
*/
if(current_run)
{
// An odd run, use first half of key
char* key_half = new char[8];
(key->substr(0,8)).copy(key_half, 8);
memcpy(key_half, curr_key, 8);
delete key_half;
(key->substr(9,1)).copy(trans_map, 1);
// curr_key now contains the right bit of the key
// and trans_map holds the byte used to map the transposition
}
else
{
// an even run, use second half of key
char* key_half = new char[8];
(key->substr(8,8)).copy(key_half, 8);
memcpy(key_half, curr_key, 8);
delete key_half;
(key->substr(0,1)).copy(trans_map, 1);
// curr_key now contains the right bit of the key
// and trans_map holds the byte used to map the transposition
}
// Now we can XOR the bytes of block against curr_key
unsigned char* ciphertext = new unsigned char[8];
for(int i = 0; i < 8; i++)
{
ciphertext[i] = block[i] ^ curr_key[i];
}
delete curr_key;
// Next, apply the transposition
// This bit could get confusing!
char tr_0 = 0x01;
char tr_1 = 0x02;
char tr_2 = 0x04;
char tr_3 = 0x08;
char tr_4 = 0x10;
char tr_5 = 0x20;
char tr_6 = 0x40;
char tr_7 = 0x80;
unsigned char temp;
if(*trans_map & tr_0)
{
fw_swap(ciphertext, 0, 1);
}
if(*trans_map & tr_1)
{
fw_swap(ciphertext, 1, 2);
}
if(*trans_map & tr_2)
{
fw_swap(ciphertext, 2, 3);
}
if(*trans_map & tr_3)
{
fw_swap(ciphertext, 3, 4);
}
if(*trans_map & tr_4)
{
fw_swap(ciphertext, 4, 5);
}
if(*trans_map & tr_5)
{
fw_swap(ciphertext, 5, 6);
}
if(*trans_map & tr_6)
{
fw_swap(ciphertext, 6, 7);
}
if(*trans_map & tr_7)
{
fw_swap(ciphertext, 7, 0);
}
char* tmp = new char[8];
memcpy(ciphertext, tmp, 8);
string st_tmp = tmp;
delete tmp;
*crypted_data += st_tmp;
if(current_run)
{
current_run = 0;
}
else
{
current_run = 1;
}
}
}



decrypt.cpp
Code:

#include <string>
#include <cstring>

using namespace std;

void rev_swap(unsigned char* data, int first, int second)
{
unsigned char temp;
temp = data[first];
data[first] = data[second];
data[second] = temp;
}

void Decrypt(string data, string* key, string* decrypted_data)
{
*decrypted_data = "";
int current_run = 1; // keep track of whether this is an odd or even run
// 0 = even, 1 = odd
// Set up a loop to deal with 8 bytes (64 bits) of data at a time
for(string::size_type i = 0; i < data.length(); i += 8)
{
char* block_temp = new char[8];
data.substr(i,8).copy(block_temp, 8);
unsigned char* block = new unsigned char[8];
memset(block, 0, 8);
memcpy(block_temp, block, 8);
delete block_temp;
unsigned char* curr_key = new unsigned char[8];
char* trans_map = new char;
/*
Note to anyone reading this:
The if structure below has a lot of duplicated code, but this is
only an example so I can't be bothered to make it more efficient!
Its only a matter of moving a lot of it out of the if, else structure
but I'm trying to get this finished!!
*/
if(current_run)
{
// An odd run, use first half of key
char* key_half = new char[8];
(key->substr(0,8)).copy(key_half, 8);
memcpy(key_half, curr_key, 8);
delete key_half;
(key->substr(9,1)).copy(trans_map, 1);
// curr_key now contains the right bit of the key
// and trans_map holds the byte used to map the transposition
}
else
{
// an even run, use second half of key
char* key_half = new char[8];
(key->substr(8,8)).copy(key_half, 8);
memcpy(key_half, curr_key, 8);
delete key_half;
(key->substr(0,1)).copy(trans_map, 1);
// curr_key now contains the right bit of the key
// and trans_map holds the byte used to map the transposition
}
// First reverse the transposition:
char tr_0 = 0x01;
char tr_1 = 0x02;
char tr_2 = 0x04;
char tr_3 = 0x08;
char tr_4 = 0x10;
char tr_5 = 0x20;
char tr_6 = 0x40;
char tr_7 = 0x80;
unsigned char temp;
if(*trans_map & tr_0)
{
rev_swap(block, 0, 1);
}
if(*trans_map & tr_1)
{
rev_swap(block, 1, 2);
}
if(*trans_map & tr_2)
{
rev_swap(block, 2, 3);
}
if(*trans_map & tr_3)
{
rev_swap(block, 3, 4);
}
if(*trans_map & tr_4)
{
rev_swap(block, 4, 5);
}
if(*trans_map & tr_5)
{
rev_swap(block, 5, 6);
}
if(*trans_map & tr_6)
{
rev_swap(block, 6, 7);
}
if(*trans_map & tr_7)
{
rev_swap(block, 7, 0);
}
// Then XOR again to decrypt:
unsigned char* plaintext = new unsigned char[8];
for(int i = 0; i < 8; i++)
{
plaintext[i] = block[i] ^ curr_key[i];
}
delete curr_key;
char* tmp = new char[8];
memcpy(plaintext, tmp, 8);
string st_tmp = tmp;
delete tmp;
*decrypted_data += st_tmp;
if(current_run)
{
current_run = 0;
}
else
{
current_run = 1;
}
}
}

iamshuke 2005-05-18
  • 打赏
  • 举报
回复
最简单,最弱的就是两个字符串xor操作,Access就是这样的。不过很容易破解。

19,468

社区成员

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

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