2012-02-24 36 views
1

我試圖使用公共和私人指數和模數來加密(實際上簽名)數據,這是在C#.NET中,我不能使用RSACryptoServiceProvider,因爲它也需要素數和其他CRT的東西。RSA - 負指數加密

所以我嘗試做以下:

private Byte[] signData() 
{ 
    BigInteger biPrivEx = new BigInteger(this.privEx); // Those are byte[] 
    BigInteger biPubEx = new BigInteger(this.pubEx); 
    BigInteger biMod = new BigInteger(this.mod);   

    BigInteger cyph = BigInteger.ModPow(new BigInteger(pkcs11), biPrivEx, biMod); // This raise exception 

    return cyph.ToByteArray();; 
} 

但問題是我收到Out Of Range Exception因爲我的私人指數是負數。

我在做什麼錯?或者可以從這個容易恢復CRT?或者,有沒有更好的方法來做到這一點? 在不同的程序中,我可以使用我正在使用的數據,所以我有參考來驗證它。

回答

1

問題是你首先得到一個負的私人指數。這取決於你如何得到這個破指數試:

  1. 添加n
  2. Concating一個00字節數組,使之正確解析。

你還應該小心排序問題。 .net的BigInteger使用little endian,其他二進制格式可能使用big endian。


嘗試:

BigInteger ParseBinaryLE(byte[] raw) 
{ 
    return new BigInteger(raw.Concat(new byte[]{0}).ToArray()); 
} 

BigInteger ParseBinaryBE(byte[] raw) 
{ 
    return new BigInteger(raw.Reverse().Concat(new byte[]{0}).ToArray()); 
} 

據我所知,還可以恢復PQ(並從這些參數的其餘部分),當你知道edn

+0

我的輸入看起來像是'Byte [] PrivateEx = StringToByteArray(「FFAABBCC')'(就是例子),我不認爲它是負面的,任何地方都沒有負號唱歌,這可能是由BigInteger造成的嗎 – Johny 2012-02-24 12:15:58

+0

@Johny BigInteger假定最後一位是符號位,所以你需要追加'00',也可能需要先倒置數組,我添加了兩個候選函數 – CodesInChaos 2012-02-24 12:16:49

+0

好吧,沒有它最終計算,但結果仍然是錯誤的,無論如何,爲什麼它可能需要顛倒數組? – Johny 2012-02-24 12:32:31