2012-01-29 39 views
0
using System; 
using System.Linq; 
using System.Text; 
using Org.BouncyCastle.Crypto; 
using Org.BouncyCastle.Crypto.Digests; 
using Org.BouncyCastle.Crypto.Engines; 
using Org.BouncyCastle.Crypto.Modes; 
using Org.BouncyCastle.Crypto.Parameters; 
using Org.BouncyCastle.Math; 
using Org.BouncyCastle.Utilities.Encoders; 

namespace Common.Encryption { 

    public class Cast5Cryptographer { 
     private bool forEncryption; 
     private BufferedBlockCipher cipher; 

     public Cast5Cryptographer(bool forEncryption) { 
      this.forEncryption = forEncryption; 
      cipher = new BufferedBlockCipher(new CfbBlockCipher(new Cast5Engine(), 64)); 
      cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(Encoding.ASCII.GetBytes("BC234xs45nme7HU9")), new byte[8])); 
     } 

     public void ReInit(byte[] IV, BigInteger pubkey) { 
      cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(pubkey.ToByteArrayUnsigned()), IV)); 
     } 

     public int BlockSize { 
      get { 
       return cipher.GetBlockSize(); 
      } 
     } 

     public byte[] DoFinal() { 
      return cipher.DoFinal(); 
     } 

     public byte[] DoFinal(byte[] buffer) { 
      return cipher.DoFinal(buffer); 
     } 

     public byte[] DoFinal(byte[] buffer, int startIndex, int len) { 
      return cipher.DoFinal(buffer, startIndex, len); 
     } 

     public byte[] ProcessBytes(byte[] buffer) { 
      return cipher.ProcessBytes(buffer); 
     } 

     public byte[] ProcessBytes(byte[] buffer, int startIndex, int len) { 
      return cipher.ProcessBytes(buffer, startIndex, len); 
     } 
    } 
} 

它的正常工作上面,它是16長度的關鍵,但是當我試圖ReInit()它與此鍵 byte[] newkey = new byte[] { 0x39, 0x65, 0x38, 0x63, 0x64, 0x32, 0x36, 0x63, 0x37, 0x37, 0x34, 0x31, 0x33, 0x65, 0x61, 0x36, 0x65, 0x35, 0x35, 0x39, 0x61, 0x32, 0x35, 0x32, 0x66, 0x30, 0x31, 0x35, 0x32, 0x38, 0x66, 0x39, 0x34, 0x38, 0x66, 0x33, 0x33, 0x34, 0x32, 0x62, 0x31, 0x38, 0x37, 0x36, 0x34, 0x61, 0x66, 0x35, 0x36, 0x38, 0x62, 0x39, 0x63, 0x39, 0x30, 0x33, 0x63, 0x35, 0x38, 0x38, 0x35, 0x34, 0x65, 0x63 };BouncyCastel CAST5 setkey的

它拋出此異常Index was outside the bounds of the array.

for (int i = 0; i < key.Length; i++) { x[i] = (int)(key[i] & 0xff); }

裏面的SetKey方法在Cast5Engine.cs,所以我更新了這個方法,而不是固定長度爲x這是16,我把它

int[] x = new int[key.Length]; for (int i = 0; i < 16; i++) x[i] = 0;

 /* copy the key into x */ 
     for (int i = 0; i < key.Length; i++) { 
      x[i] = (int)(key[i] & 0xff); 
     }` 

但現在通過比較其正在逐漸從BouncyCastelCast5OpenSSLCast5結果,好像Bouncycastel Cast5未用正確的更新密鑰,所以它會產生錯誤的加密/解密。

有任何建議來解決Setkey方法?

回答

0

通過觀察對OpenSSL的CAST_setKey方法的源代碼...

void CAST_set_key(CAST_KEY *key, int len, const unsigned char *data) 
#ifdef OPENSSL_FIPS 
    { 
    fips_cipher_abort(CAST); 
    private_CAST_set_key(key, len, data); 
    } 
void private_CAST_set_key(CAST_KEY *key, int len, const unsigned char *data) 
#endif 
    { 
    CAST_LONG x[16]; 
    CAST_LONG z[16]; 
    CAST_LONG k[32]; 
    CAST_LONG X[4],Z[4]; 
    CAST_LONG l,*K; 
    int i; 

    for (i=0; i<16; i++) x[i]=0; 
    if (len > 16) len=16; 

見線 「如果(LEN> 16)LEN = 16;」,他們只保留前16個字節的關鍵的。這不會發生在征服在線2.0會嗎?,我承認「BC234xs45nme7HU9」。

+0

是的,我很早就想出了:P – Abanoub 2012-04-17 17:08:06

相關問題