2016-09-21 6 views
0

次のPythonコードをC#に移行したいと考えています。 エントリーポイントはencrypted_requestのメソッドですPythonからC#へのAES/RSAの実装

私はPythonまたはC#でaes/rsaについて本当の手がかりがありません。 誰かがさまざまなコードセクションを説明し、可能であれば、それをC#でどのように実装するかのヒントを教えてくれるかもしれません。 特にここで使用されている魔法の数字は分かりません。

modulus = ('00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7' 
      'b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280' 
      '104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932' 
      '575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b' 
      '3ece0462db0a22b8e7') 
nonce = '0CoJUm6Qyw8W8jud' 
pubKey = '010001' 

def encrypted_request(text): 
    text = json.dumps(text) 
    secKey = createSecretKey(16) 
    encText = aesEncrypt(aesEncrypt(text, nonce), secKey) 
    encSecKey = rsaEncrypt(secKey, pubKey, modulus) 
    data = {'params': encText, 'encSecKey': encSecKey} 
    return data 


def aesEncrypt(text, secKey): 
    pad = 16 - len(text) % 16 
    text = text + chr(pad) * pad 
    encryptor = AES.new(secKey, 2, '0102030405060708') 
    ciphertext = encryptor.encrypt(text) 
    ciphertext = base64.b64encode(ciphertext).decode('u8') 
    return ciphertext 


def rsaEncrypt(text, pubKey, modulus): 
    text = text[::-1] 
    rs = pow(int(binascii.hexlify(text), 16), int(pubKey, 16)) % int(modulus, 16) 
    return format(rs, 'x').zfill(256) 


def createSecretKey(size): 
    return binascii.hexlify(os.urandom(size))[:16] 

出典:C#でhttps://github.com/darknessomi/musicbox/blob/master/NEMbox/api.py

私の現在の状態:

private byte[] hex2Binary(string hex) { 
    byte[] binaryVal = new byte[hex.Length]; 
    for (int i = 0; i < hex.Length; i++) { 
     string byteString = hex.Substring(i, 1); 
     byte b = Convert.ToByte(byteString, 16); 
     binaryVal[i] = b; 
    } 
    return binaryVal; 
} 
private string aesEncryptBase64(String plainText, string key) { 
    return aesEncryptBase64(plainText, hex2Binary(key)); 
} 
private string aesEncryptBase64(String plainText, byte[] key) { 
    //pad = 16 - len(text) % 16 
    //text = text + chr(pad) * pad 
    int pad = 16 - plainText.Length % 16; 
    for (int i=0; i<pad; i++) { 
     plainText = plainText + ((char)pad); 
    } 

    byte[] plainBytes = null; 
    RijndaelManaged aes = new RijndaelManaged(); 
    //aes.KeySize = 16; 
    aes.Mode = CipherMode.CBC; 

    aes.Key = key; 
    aes.IV = hex2Binary(client.neteaseFix.encryptInfo.iv); 

    MemoryStream ms = new MemoryStream(); 
    CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write); 
    cs.Write(plainBytes, 0, plainBytes.Length); 
    cs.Close(); 
    byte[] encryptedBytes = ms.ToArray(); 
    return Convert.ToBase64String(encryptedBytes); //decode("u8") 
} 
+0

RSA対AESの使用時に、対称鍵と公開鍵の暗号化の違い、RSAによるデータの暗号化/復号化の違いなど、アルゴリズムの仕組みを高レベルで理解していますか? – EJoshuaS

+0

私はAESが対称で高速であることを知っています.RSAは公開鍵であり、素数では遅いです。だから私はrsaを使用してキーを暗号化して、rsaでキーの速度と安全な転送を結合します。 – Schnulli

答えて

0

ここで私は右のバット見送り物事のカップルですが、問題は少しも開いています終了:

  • aesEncryptBase64あなたはmanですパディングを適用する。 .NETのAES実装はあなたのためにそれを行います。あなた自身で設定する必要がある場合は、aes.Padding = PaddingMode.None
  • aesEncryptBase64RijndaelManagedオブジェクトを作成します。それをしないでください。あなたはAESがほしいと思っています。ちょうどAES.Create()を使ってください。これはAESオブジェクト(Rijndaelオブジェクトではありません)を返します。
    • .NETはAESよりも大きなRijndaelアルゴリズムをサポートしていました。 128ビットのブロックサイズのRijndaelはAESとして選択されたものですが、RijndaelはAESがサポートしないモードをサポートしています。
  • aesEncryptBase64であなたのaesms、そしてcsオブジェクトはすべてIDisposableあるので、あなたはステートメントを使用してそれらを持っている必要があります。
  • PythonのrsaEncryptメソッドは、.NETでサポートされていない生のRSAを実行しています(一般的には良い考えではありません)。それがパディングを行うルーチンによってのみ呼び出されない限り(そして、それはサイドチャンネルの脆弱性のちょうど穴です)。 (Pythonで)あなたのrsaEncryptのみ署名または暗号化(あるいはPSSまたはOAEP)パディングを行うルーチンから呼び出されている場合は

は、あなたの.NET同等ではなく、通常の、あなたのメソッドの命名ケーシングを使用して(だろう.NETのもの)

private static rsaEncrypt(string text, string pubKey, string modulus) 
{ 
    RSAParameters rsaParams = new RSAParameters 
    { 
     Exponent = hex2Binary(pubKey), 
     Modulus = hex2Binary(modulus), 
    }; 

    using (RSA rsa = RSA.Create()) 
    { 
     rsa.ImportParameters(rsaParams); 
     return rsa.Encrypt(Encoding.ASCII.GetBytes(text), YOUNEEDTOPICKTHEPADDINGMODE); 
    } 
} 

それはそんなに文字列の再解析を行う必要がないように、しかし、この周りのコードのすべてを改善するために、世界をより良いでしょう。

関連する問題