设为首页 加入收藏

TOP

c++ 使用Tea算法进行加密解密。(三)
2015-11-21 01:03:59 来源: 作者: 【 】 浏览:6
Tags:使用 Tea 算法 进行 加密解密
r net data.?
//not useful.
? ? ulong ntoh(ulong netlong){return _isNetByte ? /*ntohl(netlong)*/netlong : netlong;}
? ? ulong hton(ulong hostlong){ return _isNetByte ? /*htonl(hostlong)*/hostlong : hostlong; }
?
protected:
? ? int _round;//iteration round to encrypt or decrypt
? ? // Not useful in this project.
? ? bool _isNetByte;//whether input bytes come from network.
? ? byte _key[16];//encrypt or decrypt key
};
?
在TeaEd.cpp文件中
?
?
TeaEd::TeaEd(const byte * key, ? ?int round/*= 32*/, bool isNetByte/*false*/):
_round(round),
_isNetByte(false/*isNetByte*/)
{
? ? if (key != 0)
? ? {
? ? ? ? memcpy(_key,key,16);
? ? }else
? ? {
? ? ? ? memset(_key,0,16);
? ? }
}
TeaEd::TeaEd(const TeaEd &rhs):
_round(rhs._round),
_isNetByte(/*rhs._isNetByte*/false)
{
? ? memcpy(_key,rhs._key,16);
}
?
TeaEd & TeaEd::operator = (const TeaEd &rsh)
{
? ? if (&rsh != this)
? ? {
? ? ? ? _round = rsh._round;
? ? ? ? _isNetByte = /*rsh._isNetByte*/false;
? ? ? ? memcpy(_key, rsh._key, 16);
? ? }
? ? return *this;
}
void TeaEd::encrypt(const byte * in, byte * out)
{
? ? ?encrypt((const ulong*)in, (ulong*)out);
}
void TeaEd::decrypt(const byte * in, byte * out)
{
? ? decrypt((const ulong*)in, (ulong*)out);
}
?
void TeaEd::encrypt(const ulong * in, ulong * out)
{
? ? ulong * k = (ulong*)_key;
? ? register ulong y = in[0];//ntoh(in[0]);
? ? register ulong z = in[1];//ntoh(in[1]);
? ? register ulong a = k[0];//ntoh(k[0]);
? ? register ulong b = k[1];//ntoh(k[1]);
? ? register ulong c = k[2];//ntoh(k[2]);
? ? register ulong d = k[3];//ntoh(k[3]);
? ? register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */
? ? register int round = _round;
? ? register ulong sum = 0;
? ? while (round--)?
? ? { ?
? ? ? ? ? /* basic cycle start */
? ? ? ? sum += delta;
? ? ? ? y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
? ? ? ? z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
? ? }/* end cycle */
? ? out[0] = y;//ntoh(y);
? ? out[1] = z;//ntoh(z);
}
?
void TeaEd::decrypt(const ulong * in , ulong * out)
{
? ? ulong *k = (ulong*)_key;
? ? register ulong y = in[0];//ntoh(in[0]);
? ? register ulong z = in[1];//ntoh(in[1]);
? ? register ulong a = k[0];//ntoh(k[0]);
? ? register ulong b = k[1];//ntoh(k[1]);
? ? register ulong c = k[2];//ntoh(k[2]);
? ? register ulong d = k[3];//ntoh(k[3]);
? ? register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */
? ? register int round = _round;
? ? register ulong sum = 0;
?
? ? if (round == 32)
? ? ? ? sum = 0xC6EF3720; /* delta << 5*/
? ? else if (round == 16)
? ? ? ? sum = 0xE3779B90; /* delta << 4*/
? ? else
? ? ? ? sum = delta << static_cast(logbase(2, round));
? ??
? ? while (round--)?
? ? { ? ?
? ? ? ? /* basic cycle start */
? ? ? ? ?z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
? ? ? ? y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
? ? ? ? sum -= delta;
? ? }/* end cycle */
? ? out[0] = y;//ntoh(y);
? ? out[1] = z;//ntoh(z);
}
?
?
?
因为加密解密需要在全局使用一个密码。所以我们用一个单例类管理加密解密功能。
?
同样都在TeaEd.h 和TeaEd.cpp中。
?
头文件的单例管理类部分。由于在测试的时候尝试了很多很多办法。 所以有很多无用的接口。真正使用到的接口只有
?
void enCryWithFileName(const string & strFile,const string & strOutFile);
void deCryWithFileName(const string & strFileName,const string& strOutfile);
?
这两个接口。 其中decry 部分使用到的第二个参数无用。
?
单例类在构造之初就通过密码创建了Tea对象,由于工作比较忙, 先不详细介绍了。。
首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 1375 Intervals(解析几何 过.. 下一篇LeetCode(1) Two Sum

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: