2012-03-06 8 views
0

見つけることができません。ネットRijndaelManagedは、私はC#のWCFで、次の暗号化/復号化を使用していJavascriptを

public static string EncryptString(string InputText, string Password) 
    { 
     RijndaelManaged RijndaelCipher = new RijndaelManaged(); 
     RijndaelCipher.Padding = PaddingMode.ISO10126; 
     if (string.IsNullOrEmpty(Password) == true) 
     { 
      Password = "Test"; 
     } 
     byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(InputText); 
     byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString()); 

     //This class uses an extension of the PBKDF1 algorithm defined in the PKCS#5 v2.0 
     //standard to derive bytes suitable for use as key material from a password. 
     //The standard is documented in IETF RRC 2898. 

     PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt); 
     //Creates a symmetric encryptor object. 
     ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); 
     MemoryStream memoryStream = new MemoryStream(); 
     //Defines a stream that links data streams to cryptographic transformations 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write); 
     cryptoStream.Write(PlainText, 0, PlainText.Length); 
     //Writes the final state and clears the buffer 
     cryptoStream.FlushFinalBlock(); 
     byte[] CipherBytes = memoryStream.ToArray(); 
     memoryStream.Close(); 
     memoryStream = null; 
     cryptoStream.Close(); 
     cryptoStream = null; 
     PlainText = null; 
     Salt = null; 
     try 
     { 
      GC.Collect(); 
     } 
     catch { } 
     return Convert.ToBase64String(CipherBytes); 

    } 


    public static string DecryptString(string InputText, string Password) 
    { 

     RijndaelManaged RijndaelCipher = new RijndaelManaged(); 
     RijndaelCipher.Padding = PaddingMode.ISO10126; 
     if (string.IsNullOrEmpty(Password) == true) 
     { 
      Password = "Test"; 
     } 
     byte[] EncryptedData = Convert.FromBase64String(InputText); 
     byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString()); 
     //Making of the key for decryption 
     PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt); 
     //Creates a symmetric Rijndael decryptor object. 
     ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); 
     MemoryStream memoryStream = new MemoryStream(EncryptedData); 
     //Defines the cryptographics stream for decryption.THe stream contains decrpted data 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read); 
     byte[] PlainText = new byte[EncryptedData.Length]; 
     int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length); 
     memoryStream.Close(); 
     memoryStream = null; 
     cryptoStream.Close(); 
     cryptoStream = null; 
     Salt = null; 
     try 
     { 
      GC.Collect(); 
     } 
     catch { } 
     //Converting to string 
     return Encoding.Unicode.GetString(PlainText, 0, DecryptedCount); 
    } 

今は、私が収まるようにするJavaスクリプトを使用しようとしている、私の中にデータを暗号化したいです私のWCFでデータを解読できるようになるまで、this scriptを使用しようとしましたが、JavascriptまたはJS & .Netのサンプルを見つけることができませんでしたか?

は、次のエラーを取得する:{「データの長さが無効である復号化するために。」}

感謝。

+0

「このスクリプト」で[AESManaged](http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aspx)を試しましたか?また、あなたが受け取っているエラーを投稿することはできますか?そして、Base64のデコード/エンコードはjavascriptでですか? –

+0

この例では、どこでjavascriptの復号化を見つけることができますか? – Joseph

+0

あなたの邸宅の例でmyAes.Key、myAes.IVを私のWCFに送信する必要があります。どのように暗号化されたデータでキーとIVを送信できますか? – Joseph

答えて

0

私が正しく理解していれば、安全にデータをWCFサービスに転送するために、ブラウザのJavaScriptでユーザー名とパスワードを暗号化する必要があります。これを実現するには、両側でAES(対称)暗号化を使用しています。

これが正しい場合は、実際にSSLを使用する必要があります。どうして? SSLはこれを行いますが、はるかに優れています。簡単に言えば、SSLはRSA鍵の公開鍵の認証後にAES鍵をネゴシエートします。したがって、クライアントのjavascriptが、正しいサーバーに話しかけていることを確実に知っているという利点が得られます。

私が考えているのは、あなた自身のAESアプローチが間違っていると思うのは、少なくとも公開鍵認証ステップを使わないであなたのキーをクライアントのJavaScriptに公開しなければならないということです。これは、その鍵を持つ誰もが今すぐサーバーにデータを送信できるので、セキュリティを即座に破壊していることを意味します。

私が誤解している場合、おそらくこれを行う適切な時期がありますが、現時点では私は見ていません。

これが役に立ちます。

+0

私はキーの問題を理解していますが、今はSSLなしでやりたいと思っていますが、今のところ鍵を共有したいのですが、コード内の問題は何ですか、なぜエラーが出ますか?すべてのオプションに私のC#のコードで。 – Joseph

関連する問題