现有用户的密码字段,当将见面上Edit_Password->Text插入数据库时作加密运算,加密后再保存到数据库,如果修改密码就要将密码还原过来。
如:123456789是密码,插入数据库后看到的是一串乱码,但读取出来还是123456789

注:不要太复杂,如密码为123456插入数据库后不要是654321就行。

此问题很急,请大家速给帮个忙解决。

C/C++ code
void Des_Bit2Byte(byte *text1, byte *text2) { int i_temp1, i_temp2; byte mask, c_temp1; for (i_temp1 = 0; i_temp1 < 8; i_temp1++) { c_temp1 = text1[i_temp1]; mask = 0×80; for (i_temp2 = 0; i_temp2 < 8; i_temp2++) { if (c_temp1 & mask) text2[i_temp1 * 8 + i_temp2] = 1; else text2[i_temp1 * 8 + i_temp2] = 0; mask >>= 1; } }; return; } void Des_Byte2Bit(byte *text1, byte *text2) { int i_temp1, i_temp2; byte mask; memset(text2, 0, 8); for (i_temp1 = 0; i_temp1 < 8; i_temp1++) { mask = 0×80; for (i_temp2 = 0; i_temp2 < 8; i_temp2++) { if (text1[i_temp1 * 8 + i_temp2]) text2[i_temp1] |= mask; mask >>= 1; } } return; } void Des_DesEncry(byte *text1, byte *key) { byte c_temp1[64], c_temp2[32]; int i_temp1, i_temp2; Des_TransPose(text1, Des_Table0, 64); Des_TransPose(key, Des_Table1, 56); for (i_temp1 = 0; i_temp1 < 16; i_temp1++) { memcpy(c_temp1, text1+32, 32); Des_Process(i_temp1, key, c_temp1, c_temp2, L); for (i_temp2 = 0; i_temp2 < 32; i_temp2++) if (text1[i_temp2] == c_temp2[i_temp2]) c_temp1[i_temp2 + 32] = 0; else c_temp1[i_temp2 + 32] = 1; memcpy(text1, c_temp1, 64); } memcpy(text1, c_temp1+32, 32); memcpy(text1+32, c_temp1, 32); Des_TransPose(text1, Des_Table3, 64); return; } void Des_DesDecry(byte *text1, byte *key) { byte c_temp1[64], c_temp2[32]; int i_temp1, i_temp2; Des_TransPose(text1, Des_Table0, 64); memcpy(c_temp2, text1+32, 32); memcpy(text1+32, text1, 32); memcpy(text1, c_temp2, 32); Des_TransPose(key, Des_Table1, 56); for (i_temp1 = 0; i_temp1 < 16; i_temp1++) { memcpy(c_temp1 + 32, text1, 32); Des_Process(i_temp1, key, c_temp1 + 32, c_temp2, R); for (i_temp2 = 0; i_temp2 < 32; i_temp2++) if (text1[i_temp2 + 32] == c_temp2[i_temp2]) c_temp1[i_temp2] = 0; else c_temp1[i_temp2] = 1; memcpy(text1, c_temp1, 64); } Des_TransPose(text1, Des_Table3, 64); return; } void Des_TransPose(byte *text, byte *table, int index) { byte c_temp1[64]; int i_temp1; memcpy(c_temp1, text, 64); for (i_temp1 = 0; i_temp1 < index; i_temp1++) text[i_temp1] = c_temp1[table[i_temp1] - 1]; return; } void Des_Process(int i, byte *key, byte *R32, byte *PB, byte flag) { byte extr32[48], extkey[56], c_temp1; int i_temp1; memcpy(extr32, R32, 32); Des_TransPose(extr32, Des_Table4, 48); if (toupper(flag) == L) Des_Lshift(key, Des_Table5[i]); else Des_Rshift(key, Des_Table6[i]); memcpy(extkey, key, 56); Des_TransPose(extkey, Des_Table2, 48); for (i_temp1 = 0; i_temp1 < 48; i_temp1++) if (extr32[i_temp1] == extkey[i_temp1]) extr32[i_temp1] = 0; else extr32[i_temp1] = 1; for (i_temp1 = 0; i_temp1 < 8; i_temp1++) { c_temp1 = (byte)(extr32[i_temp1 * 6] * 32 + extr32[i_temp1 * 6 + 5] * 16 + extr32[i_temp1 * 6 + 1] * 8 + extr32[i_temp1 * 6 + 2] * 4 + extr32[i_temp1 * 6 + 3] * 2 + extr32[i_temp1 * 6 + 4]); c_temp1 = Des_Table8[i_temp1][c_temp1]; if (c_temp1 >= 8) { PB[i_temp1 * 4] = 1; c_temp1 = (byte)(c_temp1 - 8); } else PB[i_temp1 * 4] = 0; if (c_temp1 >= 4) { PB[i_temp1 * 4 + 1] = 1; c_temp1 = (byte)(c_temp1 - 4); } else PB[i_temp1 * 4 + 1] = 0; if (c_temp1 >= 2) { PB[i_temp1 * 4 + 2] = 1; c_temp1 = (byte)(c_temp1 - 2); } else PB[i_temp1 * 4 + 2] = 0; if (c_temp1 >= 1) PB[i_temp1 * 4 + 3] = 1; else PB[i_temp1 * 4 + 3] = 0; } Des_TransPose(PB, Des_Table7, 32); return; } void Des_Lshift(byte *key, byte cnt) { byte c_temp1, c_temp2; int i_temp1,i_temp2; for (i_temp2 = 0; i_temp2 < cnt; i_temp2++) { c_temp1 = key[0]; c_temp2 = key[28]; for (i_temp1 = 0; i_temp1 < 55; i_temp1++) key[i_temp1] = key[i_temp1 + 1]; key[27] = c_temp1; key[55] = c_temp2; } return; } void Des_Rshift(byte *key, byte cnt) { byte c_temp1, c_temp2; int i_temp1,i_temp2; for (i_temp2 = 0; i_temp2 < cnt; i_temp2++) { c_temp1 = key[27]; c_temp2 = key[55]; for (i_temp1 = 55; i_temp1 >0; i_temp1) key[i_temp1] = key[i_temp1 - 1]; key[0] = c_temp1; key[28] = c_temp2; } return;

简单的,异或

C/C++ code
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; class encrypt { public: encrypt(const char *); //构造函数,加密 ~encrypt(); void showcode(); void decrypt(); //解密 private: char *code; int length; }; encrypt::encrypt(const char *string) { int i=0; length=strlen(string);//获得原字符串的长度 code=new char[length+1];// 加上结束符’\0′ for(;i<length-1;i++) //从第一个字符开始,直到倒数第二个 { code[i]= string[i] ^ string[i+1]; //加密 } code[i]= string[i] ^ string[0];//i=length-1,最后一个字符 //跟第一个字符做异或 code[length] = \0; //加上结束符 } encrypt::~encrypt() { delete[] code; } void encrypt::showcode() { for(int i=0;i<length;i++) printf("%1c",code[i]); //防止’\0′,将字符逐个打出.不用iostream //iostream遇到’\0′会自动结束,,而加密过程中可能中间出现’\0′,不该结束 } void encrypt::decrypt() { char p; cout <<"\nThe First Character:"; cin >> p; code[length-1] ^= p; //最后一个字符跟第一个再次异或,得到原来的字符(两次异或,得到原值) for(int i=length-2; i>=0 ; i) //从倒数第二个开始 { code[i]^=code[i+1]; //跟后面一个做异或,得原值 } cout<<"the original string is:" <<endl; showcode(); cout <<endl; } int main() { encrypt e("hello world!"); e.showcode(); e.decrypt(); return 0; }

小弟刚学C++不久,还愿详细一点,现我的程序是窗口的形式,有个edt_password文本框,数据库中有个user表,表里有个password字段。
现我要将edt_password里的内容加密后放到password的字段中,然点再修改让password字段的内容显示到edt_password中,插入数据库是加密后的内容,读取出来要是解密后的内容,望兄弟给个这样的实际例子。

直接把edt_password里的内容放到password的字段中你会不会?

最简单的办法就是将各个位异或一下就可以了

搞个函数就可以了,忘了是引用谁的了,不过可以用。
AnsiString encode(AnsiString *source)//异或加密,带钥匙加密,加密钥匙内部定义
{
  AnsiString astrEcode;
//用户专用密码,可以修改其中的5个密匙
  static const char i_userkey[5] = {0xab,0xbc,0xcb,0xde,0xef};
//开始加密/解密
  if(source->Length()>0)
  {
    for(int i=1; key=source->Length(); i <=key; i++)
      astrEcode = astrEcode + (char)(*source->substring(i,1).c_str() ^ (i+i_userkey[i%5));
  return astrEcode;
}
//解密的时候再调用这个函数就可以了。

建议使用成熟的加密算法。