2016-05-03 9 views
2

JavaからC#にPortable.BouncyCastleを使用していくつかの関数を変換しようとしていますが、多くの例がありますが、ほとんどの例が特定の暗号化/復号化方法を説明しているように見えるので、この機能がより一般的であるように見えるので、私の要件に一致するものを見つけてください。私はこれに完全に初心者で、BouncyCastle、Java、または暗号化のいずれの経験も持っていないので間違いかもしれません。bouncycastleを使用してJavaからC#に暗号化/復号化を変換します

Javaの機能は次のとおりです。

byte[] K = Hex.Decode("404142434445464748494a4b4c4d4e4f"); 
byte[] N = Hex.Decode("10111213141516"); 
byte[] P = Hex.Decode("68656c6c6f20776f726c642121"); 
byte[] C = Hex.Decode("39264f148b54c456035de0a531c8344f46db12b388"); 

KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", K); 

IBufferedCipher inCipher = CipherUtilities. 
    GetCipher("AES/CCM/NoPadding"); 

inCipher.Init(true, new ParametersWithIV(key, N)); 

byte[] enc = inCipher.DoFinal(P); 

1. SecretKeySpec:

public static byte[] Cipher(int mode, byte[] key, 
     byte[] data, string algorithm, AlgorithmParameterSpec spec) 
{ 
    Cipher cipher = Cipher.getInstance(algorithm); 
    SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); 

    if (spec != null) 
    cipher.init(mode, keySpec, spec); 
    else 
    cipher.init(mode, keySpec); 

    return cipher.doFinal(data); 
} 

は、私は私が見ることができるものから、ほとんどの機能を一致させることができますBouncyCasleからいくつかのコードを見つけました

SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); 

How do I create this using BC? Is that the equivalent of the SecretKeySpec: 

    KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", K); 

If it is, can I pass the "AES/CCM/NoPadding" instead of AES as it is done in Java? 

2 。スペックパラメータ:

It passes parameters of type IvParameterSpec to the Cypher function when called from `Java` via the `AlgorithmParameterSpec spec` parameter: 

Cipher(ENCRYPT_MODE, key, clearBytes, 
       algorithm, 
       new IvParameterSpec(iv)) 

`BouncyCastle` does not have an overloaded function for `.Init` to allow me to pass the spec parameter as it does in `Java`, so how do I mimic this behaviour? 

3. IvParameterSpec:あなたはCYPHERはJavaから呼び出されたとき、それはnew IvParameterSpec(iv)としてではなくはBouncyCastleでAlgorithmParameterSpec specを渡すことを見ることができ、それがキーを期待しているようですか?

ParametersWithIV(key, N) 

その違いは、暗号化/復号化への影響はありますか?

これは、現在でこの機能を「変換」で私の試み:

public static byte[] Cipher(bool isEncrypt, byte[] key, byte[] data, 
          string algorithm, ICipherParameters spec) 
    { 
     IBufferedCipher cipher = CipherUtilities.GetCipher(algorithm); 
     KeyParameter keySpec = ParameterUtilities. 
      CreateKeyParameter(algorithm, key); 

     cipher.Init(isEncrypt, new ParametersWithIV(keySpec, 
      keySpec.GetKey())); 

     return cipher.DoFinal(data); 
    } 

を私はICipherParameters specにスペックのパラメータを変更したが、これは弾むを使用したときのように動作する場合、私は知りません見ることができるようにnew ParametersWithIVを作成すると、キーが必要で、上記のテストサンプルから、KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", K);を使用してそのキーが作成されるように見えます。この関数を呼び出すときにCipher関数を呼び出すときに技術的には機能しませんまだ。 specパラメータをivに変更し、代わりにbyte[]を渡す必要がありますか?

混乱がある場合や事柄が正しく説明されていない場合は申し訳ありませんが、私が言ったように、私はこれに慣れていないと変換しながらそれをよりよく理解しようとしています。私はそれのほとんどが意味をなさないとあなたが手伝ってくれることを願っています。

多くのありがとうございます。

PS:私はまだJavaでこれらをテストする立場にはいませんが、うまくいけば、新しい数日で正しく設定された環境をうまくいけば、.net & javaの値をテストするのに役立ちます。

UPDATE 1

にを渡す:

KeyParameter key = ParameterUtilities.CreateKeyParameter 

でエラーが発生しますので、これは部分的に私の質問の答え1。 が渡されたときに、には、必要な正しい値、つまりAESを決定する関数がありますか?

答えて

1

期待通りに動作しているように見えるが、まだ悩まされているので、以下のコードを使用して終了しました。AESをIVパラメータ部分のキーとしてハードコードする必要がありました。理想的には、私はこれが私のアルゴリズムに基づいているのが好きでした。だから、今私は暗号化と復号化のための単一の機能を持っています:

public static byte[] Cipher(bool forEncryption, byte[] key, 
byte[] data, string algorithm, byte[] iv) 
{ 
    IBufferedCipher cipher = CipherUtilities.GetCipher(algorithm); 
    KeyParameter keySpec = ParameterUtilities.CreateKeyParameter("AES", key); 
    cipher.Init(forEncryption, new ParametersWithIV(keySpec, iv)); 
    return cipher.DoFinal(data); 
} 

これは役に立ちます。

関連する問題