解密程序如下:
-
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <fstream>
-
#include <sstream>
-
#include <iostream>
-
#include <string>
-
#include <string.h>
-
-
#define FAILED 1
-
#define SUCCESS 0
-
using namespace std;
-
-
const char *stampKey = "linfang2b";
-
static string stampFile= "/etc/measure/measured.conf";
-
void strToByte(const char *sptr,int slen,char *bptr)
-
{
-
int i,j,op,tmp;
-
for(i=0;i<slen;i++)
-
{
-
op=0x80;
-
for(j=0;j<8;j++)
-
{
-
tmp=sptr[i]&op;
-
-
if(tmp == 0)
-
-
bptr[i*8+j] = 0x0;
-
else
-
-
bptr[i*8+j] = 0x1;
-
op>>=1;
-
}
-
}
-
}
-
-
void byteTostr(const char* bptr,int blen,char* sptr)
-
{
-
-
int j,op;
-
op=0;
-
for(j=0;j<blen;j++)
-
{
-
op|=bptr[j];
-
-
if (j%8 == 7)
-
{
-
sptr[(j+1)/8-1]=op;
-
op=0;
-
}
-
else
-
op<<=1;
-
}
-
}
-
-
char* alignStr(const char* src,int slen)
-
{
-
char* dest;
-
int alignLen=0,i;
-
if(slen%8 != 0)
-
alignLen = 8-slen%8;
-
dest = (char*)malloc((slen+alignLen+1)*sizeof(char));
-
strncpy(dest,src,slen);
-
for(i=0;i<alignLen;i++)
-
dest[slen+i]='$';
-
dest[slen+alignLen]='\0';
-
return dest;
-
}
-
-
int unAlignStr(char *string, int slen)
-
{
-
-
int tlen=slen;
-
while(1)
-
{
-
if(string[tlen-1]=='$')
-
string[tlen-1]='\0';
-
-
else
-
-
return tlen;
-
tlen--;
-
}
-
}
-
-
int enCryptStamp(char* string,int slen,const char* key,int flag)
-
{
-
char* ptr,*buffer,bytekey[64];
-
int i,buflen;
-
strToByte(key,8,bytekey);
-
setkey(bytekey);
-
buflen=slen*8;
-
buffer=(char*)malloc(buflen*sizeof(char));
-
if(NULL == buffer)
-
{
-
cout << "malloc failed" << endl;
-
return FAILED;
-
}
-
strToByte(string,slen,buffer);
-
for(i=0;i<buflen;i+=64)
-
{
-
ptr=&buffer[i];
-
encrypt(ptr,flag);
-
}
-
byteTostr(buffer,buflen,string);
-
free(buffer);
-
buffer = NULL;
-
return SUCCESS;
-
}
-
//int readFromStampConf(string &str)
-
//{
-
// fstream file;
-
// file.open(stampFile.c_str(), ios::in);
-
// if(!file)
-
// {
-
// cout << "File op failed" << endl;
-
// return FAILED;
-
// }
-
// file >> str;
-
//
-
// file.close();
-
// return SUCCESS;
-
//
-
//}
-
int readFromStampConf(char *str)
-
{
-
fstream file;
-
file.open(stampFile.c_str(), ios::in);
-
if(!file)
-
{
-
cout << "File op failed" << endl;
-
return FAILED;
-
}
-
int len = 0;
-
-
//------存在问题,len多加一----------------
-
//------在file判断eof时,eof()返回true时是读到文件结束符0xFF,而文件结束符是最后一个字符的下一个字符
-
//------因此多加一
-
//while(!file.eof())
-
//{
-
// file.get(str[len++]);
-
//}
-
//----------------------------------------
-
-
//------在网上搜了一下,有人说通过file.good()方法判断,本人试过,无效!
-
//------也可能是使用方法有误,请指出,谢!
-
//while(!file.eof())
-
//{
-
// if(file.good())
-
// file.get(str[len++]);
-
//}
-
//----------------------------------------
-
-
-
//------也有人说通过file.fail()方法判断,本人试过,无效!
-
//------也可能是使用方法有误,请指出,谢!
-
//while(!file.eof())
-
//{
-
// if(file.fail())
-
// break;
-
// file.get(str[len++]);
-
//}
-
//----------------------------------------
-
-
-
-
//------通过file.peek方法判断,可行!-----
-
//while(file.peek() != EOF)
-
//{
-
// file.get(str[len++]);
-
//}
-
//---------------------------------------
-
-
//-----下面形式的get也有问题,导致len多加一
-
//while(file.get(str[len++]));
-
//-----------------------------------------
-
-
//-----下面方法经尝试也可行!
-
char c;
-
while(file.get(c))
-
str[len++] = c;
-
-
-
str[len] = '\0';
-
file.close();
-
return SUCCESS;
-
-
}
-
int main()
-
{
-
char str[100];
-
//memset(str, 0, 100);
-
readFromStampConf(str);
-
-
enCryptStamp((char*)str, strlen(str), stampKey, 1);
-
int len = unAlignStr((char*)str, strlen(str));
-
str[len] = '\0';
-
cout <<"The encrypt content is " << str<< endl;
-
-
-
return 0;
-
}
加密程序如下:
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include <string>
-
#include <iostream>
-
#include <fstream>
-
using namespace std;
-
#define SUCCESS 0
-
#define FAILED 1
-
static string stampFile= "/etc/measure/measured.conf";
-
const char *stampKey = "linfang2b";
-
void strToByte(const char *sptr,int slen,char *bptr)
-
{
-
-
int i,j,op,tmp;
-
for(i=0;i<slen;i++)
-
{
-
op=0x80;
-
for(j=0;j<8;j++)
-
{
-
tmp=sptr[i]&op;
-
-
if(tmp == 0)
-
-
bptr[i*8+j] = 0x0;
-
else
-
-
bptr[i*8+j] = 0x1;
-
op>>=1;
-
}
-
}
-
}
-
-
void byteTostr(const char* bptr,int blen,char* sptr)
-
{
-
-
int j,op;
-
op=0;
-
for(j=0;j<blen;j++)
-
{
-
op|=bptr[j];
-
-
if (j%8 == 7)
-
{
-
sptr[(j+1)/8-1]=op;
-
op=0;
-
}
-
else
-
op<<=1;
-
}
-
}
-
-
char* alignStr(const char* src,int slen)
-
{
-
char* dest;
-
int alignLen=0,i;
-
if(slen%8 != 0)
-
alignLen = 8-slen%8;
-
dest = (char*)malloc((slen+alignLen+1)*sizeof(char));
-
strncpy(dest,src,slen);
-
for(i=0;i<alignLen;i++)
-
dest[slen+i]='$';
-
dest[slen+alignLen]='\0';
-
return dest;
-
}
-
-
int unAlignStr(char *string, int slen)
-
{
-
-
int tlen=slen;
-
while(1)
-
{
-
if(string[tlen-1]=='$')
-
string[tlen-1]='\0';
-
-
else
-
-
return tlen;
-
tlen--;
-
}
-
}
-
-
int enCryptStamp(char* string,int slen,const char* key,int flag)
-
{
-
char* ptr,*buffer,bytekey[64];
-
int i,buflen;
-
strToByte(key,8,bytekey);
-
setkey(bytekey);
-
buflen=slen*8;
-
buffer=(char*)malloc(buflen*sizeof(char));
-
if(NULL == buffer)
-
{
-
return FAILED;
-
}
-
strToByte(string,slen,buffer);
-
for(i=0;i<buflen;i+=64)
-
{
-
ptr=&buffer[i];
-
encrypt(ptr,flag);
-
}
-
byteTostr(buffer,buflen,string);
-
free(buffer);
-
buffer = NULL;
-
return SUCCESS;
-
}
-
int isValidStamp(string &stamp)
-
{
-
for(unsigned int i=0; i<stamp.size(); i++)
-
{
-
if(!isdigit(stamp[i]))
-
return FAILED;
-
}
-
return SUCCESS;
-
}
-
int writeToStampConf(const char *key)
-
{
-
fstream file;
-
file.open(stampFile.c_str(), ios::out);
-
if(!file)
-
{
-
return FAILED;
-
}
-
-
file << key;
-
-
file.close();
-
return SUCCESS;
-
-
}
-
int main()
-
{
-
string stamp("36");
-
char *key = NULL;
-
int iRet;
-
key = alignStr(stamp.c_str(), stamp.size());
-
iRet = enCryptStamp(key, strlen(key), stampKey, 0);
-
if(iRet)
-
{
-
return FAILED;
-
}
-
iRet = writeToStampConf(key);
-
if(iRet)
-
return FAILED;
-
-
free(key);
-
key = NULL;
-
return SUCCESS;
-
}
如解密程序注释部分,之前对file.eof的理解有误区,导致解密程序对len计算有问题,导致解密失败。
其实只要将memset那句注释打开,有些问题也就不存在了,因为str[100]可能有非0初值,因此当len在判断eof时多加一,碰巧那一位有值,导致解密失败。
如果在其他环境下,应该对eof进行合理的判断。
此外,encrypt函数在调用过程中需要将字符串转换为“01串”!
阅读(1660) | 评论(0) | 转发(0) |