65.9K
CodeProject 正在变化。 阅读更多。
Home

实验性加密

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.20/5 (4投票s)

2010年8月29日

CPOL

3分钟阅读

viewsIcon

39026

downloadIcon

547

实验性加密

引言

警告:本文不是关于传统密码的。

我对密码学很感兴趣 - 从简单的替换密码到公钥密码和单向函数,例如模运算。这种实验性密码非常简单,使用对称密钥,并且严重依赖于哈希算法的实现。

可靠性

该密码的设计非常差,将用于非安全性的项目,其中安全性不是挑战。如果您想要真正全球使用的密码,也许您应该寻找Rijndael作为当前的AES对称私钥密码,或者RSA和PGP,如果您真的想要“非常好的隐私”。

基本思想

  1. 获取密码
  2. 从中计算哈希值
  3. 将明文与该哈希值进行异或(一次一个字节,最多4个字节)
  4. 当哈希值的全部四个字节都使用完后,则将新的哈希值计算为
    HASH = Hash(HASH)

因此,我们应该获得另一个哈希值,该哈希值由前一个哈希值生成。虽然核心很原始,可能有点争议,但很有趣。

更有趣的是我的 CBC(虽然不是真正的CBC,只是伪CBC),可以通过将

HASH = HASH XOR source_plaintext_byte

对于加密,放在循环的开头,并且...

HASH = HASH XOR destination_plaintext_byte

...在解密循环中放在结尾。

还有两个其他的,次要的函数(宏),在这里用于实验目的。它们不应该对密文的失败/安全负责。它们的唯一目的是隐藏(可能的)密文中的伪影。但基本核心是哈希函数本身

unsigned int Hash(BYTE *key, int length){       
     unsigned int hash = 0;
     int i;
     for (i = 0; i < length; i++){            
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
      }
      hash += (hash << 3);
      hash ^= (hash >> 11);
      hash += (hash << 15);
      return hash; 
}

unsigned int 是32位长的数字,unsigned long long 会将我们简单的、糟糕的哈希扩展到64位,但这也不能解决我们的问题。哈希数字太短,无法保证任何类似于“安全”的东西。128或256位哈希将是理想的,但如果我能够编码任何哈希,那么我可以编码任何哈希,并扩展位长度。

我不记得在哪里遇到过这个哈希,但可能是Bob Jenkins的文章这里的“Jenkins One-at-a-time hash”。但对于实现,请参见此链接

我认为使用更好的哈希算法可能会产生更好的机会,使得第一个密钥哈希不会被破解。这必须经过验证。

背景

您可以在Wiki页面上了解关于CBC、密码、私钥和公钥、模式等内容

代码

这是纯C代码,使用了C++类。该算法具有广泛的可移植性,只要有C语言可用。

基类

// base (the only one) class
class Perfect_Crypt{
  public:
    bool Crypt(BYTE *src, int srclength, BYTE *&dest,
        int &destlength,BYTE *password, int passwordlength,
        TCryptParam param);
};

核心

        // core functions - macros
        #define neg(I) ( ~I)  			// negation
        #define rot(I) ( (I << 3) ^ (I >> (32-3)))  	// bit rotation by 3
        // hash, from an article by Bob Jenkins
        unsigned int Hash(BYTE *key, int length){
           unsigned int hash = 0;
           int i;
           for (i = 0; i < length; i++){
              hash += key[i];
              hash += (hash << 10);
              hash ^= (hash >> 6);
           }
           hash += (hash << 3);
           hash ^= (hash >> 11);
           hash += (hash << 15);
           return hash; 
        }
        // crypt
        addr = (BYTE *)&HASH;  	// get the address of BYTE of our HASH number
        while(srcptr != stop){
           HASH^=srcChr;           	// pseudo cipher block chaining
           HashChr = *(addr+four); 	// take particular byte from HASH into HashChr
           srcChr = (*srcptr);     	// copy plaintext char into srcChr
           (*destptr) = srcChr ^ HashChr; 	// output ciphertext byte as xored 
                                          	// srcChr and HashChr
           four++;    		// used when taking bytes from hash number
           if (four == 4){                	// if all bytes from hash were 
                                          	// used 4 for 32 bit integers
              HASH = Hash((BYTE*)&HASH, 4); 	// make new hash
              HASH = neg(HASH);  		// negate hash bits
              HASH = rot(HASH);  		// rotate hash bits
              four = 0;  			// reset "4 bytes" counter
           }  
           destptr++  			// move forward;
           srcptr++;
  }

用法

如果您想使用和试验此密码,您可以调用

bool Texcrypt::Crypt(BYTE *src, int srclength, BYTE *&dest,
	int &destlength, BYTE *password, int passwordlength,
	TCryptParam param)

关注点

该算法主要基于哈希算法,因此如果哈希算法存在任何弱点,它将对加密可靠性产生直接影响。这里的弱点是碰撞产生和不完全的位“压倒”,我们需要每个位与每一个(其他的)位进行哈希。

令人惊讶的简单算法,怀疑存在弱点,虽然,它不再是传统的密码。

还有一件事是,该密码算法计算验证码,即每个字节中1的总和。这不是CRC32,只是伪CRC,以提高安全性。

它还包括伪密码块链接。基本上,CBC的设计是将每个密码块与之前的明文链接起来,因此明文中的任何小变化(例如解密中的错误)都会使所有下一个块都不可读,并且外观上像“垃圾”。

也许会有人发现这种概念有用。

附加信息

  1. Rijndael,在Wiki上 http://en.wikipedia.org/wiki/Rijndael
  2. RSA,在Wiki上 http://en.wikipedia.org/wiki/Rsa
  3. PGP,在Wiki上 http://en.wikipedia.org/wiki/Pretty_Good_Privacy

历史

  • 更新于2010年8月30日 - 添加了一些参考资料和关于密码可靠性的警告
  • 更新于2010年8月30日 - 重命名为Excrypt,代表实验性加密
  • 更新于2010年8月29日 - 添加了密码块链接(两行代码)和一些注释,以使某些事情更清楚
  • 更新于2010年8月29日 - 移除了一个额外的 rot(),它在那里干什么??
  • 更新于2010年8月29日 - 评论
© . All rights reserved.