2012-03-13 17 views
0

私は暗号とjs暗号の新機能です。私はこのような.net コードを以下の暗号化/復号化のために使用しています。 とJSのために、私は、ユーザー名を暗号化する必要がある、と私はhttp://code.google.com/p/crypto-js/AES暗号化.NetとJs

Crypto.AES.encrypt( "Q"、 "テスト"、{モードこのLIBを使用します。新しいCrypto.mode.CBC(Crypto.pad.pkcs7) })

しかし、私は値を解読しようとしました。「パディングエラー」が発生しました 助けてください、ありがとうございます。

/// <summary> 
    /// Method which does the encryption using Rijndeal algorithm 
    /// </summary> 
    /// <param name="InputText">Data to be encrypted</param> 
    /// <param name="Password">The string to used for making the key.The same string 
    /// should be used for making the decrpt key</param> 
    /// <returns>Encrypted Data</returns> 
    public static string EncryptString(string InputText, string Password) 
    { 
     RijndaelManaged RijndaelCipher = new RijndaelManaged(); 


     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); 

    } 

    /// <summary> 
    /// Method which does the encryption using Rijndeal algorithm.This is for decrypting the data 
    /// which has orginally being encrypted using the above method 
    /// </summary> 
    /// <param name="InputText">The encrypted data which has to be decrypted</param> 
    /// <param name="Password">The string which has been used for encrypting.The same string 
    /// should be used for making the decrypt key</param> 
    /// <returns>Decrypted Data</returns> 
    public static string DecryptString(string InputText, string Password) 
    { 
     RijndaelManaged RijndaelCipher = new RijndaelManaged(); 


     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); 

    } 
+0

どのようにお手伝いしますか?答えに示されているようにRfc2898DeriveBytesを使用してキーバイトを複製できますか?あなたの質問にフォローアップすることを忘れないでください。 –

答えて

2

なぜ、JS内のパスワードを暗号化していますか?登録/ログインページが安全な接続で確実に実行されるようにしてください。戻るあなたの質問に

、この質問はすでにGoogleグループに尋ねたと回答されている参照してください。http://groups.google.com/group/crypto-js/browse_thread/thread/b4f32cc2fc59ec2c#

編集:

ます。また、CBCのデフォルトのパディング方式を試みることができます

var crypted = Crypto.AES.encrypt("Message", "Secret Passphrase", { mode: new Crypto.mode.CBC }); 

.NetでデフォルトのCBCパディングを使用していると見なされます。

+0

お返事ありがとうございます。文字列を変換しましたが、引き続きトリガーします "埋め込みが無効で、削除できません。" .net解読コード:( 私のコード例 Crypto.AES.encrypt(stringToUnicodeBytes( "q")、 stringToUnicodeBytes( "Yosef!")、{mode:new Crypto.mode.CBC(Crypto.pad.pkcs7) )}) この行から "fCbxmcwBl5yfgoRm1LgoSRZlUlwTxmVbI9oS/mMrIuE =" しかし、.netコードは私の "hJelPY4RCl7H –

1

PasswordDeriveBytesはPBKDF2を実行せず、PBKDF1のみを実行し、最初の20バイトだけを実行します。それを超えて、それはPBKDF1に基づいている、独自の、記述されていない、ひどく実装された馬鹿な計画です。

あなたが指しているGoogleライブラリのように、PBKDF2を実装するRfc2898DeriveBytesを使用する必要があります。さらに処理する前に計算されたキーバイトが正しいかどうかを確認してください。

パディング例外は、暗号テキストまたはキー値のいずれかが正しくない場合にスローされる可能性があることに注意してください。基本的にそれは何かが間違っていると伝えますが、それ以外はあまりありません。