2012-06-29 27 views
0

Bouncy城​​堡已经使用XTEAEngine类提供了XTEA加密。 类属性是这样的:我为BC的XTEA实现不起作用,为什么?

private static final int rounds  = 32, 
         block_size = 8, 
         key_size = 16, 
         delta  = 0x9E3779B9, 
         d_sum  = 0xC6EF3720; // sum on decrypt 

但封闭源代码的C++应用程序使用以下设置(与32轮):

uint32 delta = 0x61C88647; uint32 sum = 0xC6EF3720;

因此,要改变它,我已经复制的整个代码BC XTEAEngine类,在更改的delta处将其命名为XTEAEngine2。但现在预期加密不起作用:

arr given : [11, 0, 10, 8, 0, 72, 105, 32, 116, 104, 101, 114, 101, 0, 0, 0] 
arr encrypted: [-128, -26, -32, 17, 7, 98, 80, -112, 26, -83, -11, 47, -124, -50, -80, 59] 
arr decrypted: [-106, 62, 110, -40, -56, -58, 18, -101, -38, -73, -83, 95, 18, -99, -84, -37] 

我改变一切之前罚款:

arr given : [11, 0, 10, 8, 0, 72, 105, 32, 116, 104, 101, 114, 101, 0, 0, 0] 
arr encrypted: [89, -95, -88, 120, -117, 39, 57, -126, 23, -74, 35, 105, -23, -7, -109, -27] 
arr decrypted: [11, 0, 10, 8, 0, 72, 105, 32, 116, 104, 101, 114, 101, 0, 0, 0] 

而且我使用BouncyCastle的XTEA这样的:

BlockCipher engine = new XTEAEngine[2](); 
BufferedBlockCipher cipher = new BufferedBlockCipher(engine); 
KeyParameter kp = new KeyParameter(keyArr); 
private byte[] callCipher(byte[] data) 
    throws CryptoException { 
     int size = 
       cipher.getOutputSize(data.length); 
     byte[] result = new byte[ size ]; 
     int olen = cipher.processBytes(data, 0, 
       data.length, result, 0); 
     olen += cipher.doFinal(result, olen); 

     if(olen < size){ 
      byte[] tmp = new byte[ olen ]; 
      System.arraycopy(
        result, 0, tmp, 0, olen); 
      result = tmp; 
     } 

     return result; 
    } 


public synchronized byte[] encrypt(byte[] data) 
    throws CryptoException { 
     if(data == null || data.length == 0){ 
      return new byte[0]; 
     } 

     cipher.init(true, kp); 
     return callCipher(data); 
    } 

    public synchronized byte[] decrypt(byte[] data) 
    throws CryptoException { 
     if(data == null || data.length == 0){ 
      return new byte[0]; 
     } 

     cipher.init(false, kp); 
     return callCipher(data); 
    } 

是任何人都能够指出我的错误?因为我相信我在某个地方创造了一个。 我甚至用维基百科的算法规范完全实现了XTEA,但同样的事情发生了。

+1

你确定它不是封闭的源应用程序有问题吗? –

+0

我100%确定。已有开源应用程序使用该封闭源应用程序的反向引擎协议,并且封闭源客户端与开放源代码服务器协同工作。 –

回答

3

0x9E3779B90x61C88647的两个补码阴性;这向我暗示,有一个增加与减法交换的地方。

+0

哦,一如既往,U2。这解释了一切!在我之前的问题中,我已经提到我跳过了所有有关二进制补码的类:< –