2015-01-08 38 views
1

我們想在windows phone 7中使用密碼加密/解密。我們已經爲使用java的android完成了。但是當我們嘗試在C#中開發時,我們正在掙扎。windows phone 7中的密碼加密/解密

我們的Java代碼:

public AES() 
    { 
     try 
     { 
      Security.addProvider(new BouncyCastleProvider()); 
      cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); 

     } catch (NoSuchProviderException e) { 
      e.printStackTrace(); 
     } catch (NoSuchPaddingException e) { 
      e.printStackTrace(); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } 
    } 


    public String doDecrypt(String key, String cipherText) 
    { 
     try 
     { 
      byte[] raw = key.getBytes(Charset.forName("UTF-8")); 
      SecretKeySpec skey = new SecretKeySpec(raw, "AES"); 
      cipher.init(Cipher.DECRYPT_MODE, skey); 
      return new String(cipher.doFinal(Base64.decode(cipherText,Base64.DEFAULT)), Charset.forName("UTF-8")); 

     } catch (BadPaddingException e) { 
      e.printStackTrace(); 
     } catch (IllegalBlockSizeException e) { 
      e.printStackTrace(); 
     } catch (InvalidKeyException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public String doEncrypt(String key, String plainText) 
    { 
     try 
     { 
      byte[] raw = key.getBytes(Charset.forName("UTF-8")); 
      SecretKeySpec skey = new SecretKeySpec(raw, "AES"); 
      cipher.init(Cipher.ENCRYPT_MODE, skey); 
      return Base64.encodeToString(cipher.doFinal(plainText.getBytes(Charset.forName("UTF-8"))),Base64.DEFAULT); 

     } catch (BadPaddingException e) { 
      e.printStackTrace(); 
     } catch (IllegalBlockSizeException e) { 
      e.printStackTrace(); 
     } catch (InvalidKeyException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

在這裏我們可以進行加密和解密。

我們的C#代碼:

public static byte[] EncryptWithAES(string dataToEncrypt, String Key) 
     { 

      byte[] encryptedData; 

      byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(Key); 


      using (AesManaged aesEnc = new AesManaged()) 
      { 
       aesEnc.Key = keyBytes; 
       aesEnc.IV = new byte[16]; 

       //Create encryptor for converting 
       ICryptoTransform encryptor = aesEnc.CreateEncryptor(aesEnc.Key, aesEnc.IV); 


       using (MemoryStream memStream = new MemoryStream()) 
       { 
        using (CryptoStream crypStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write)) 
        { 
         using (StreamWriter srmWriter = new StreamWriter(crypStream)) 
         { 

          srmWriter.Write(dataToEncrypt); 
         } 
         encryptedData = memStream.ToArray(); 
        } 
       } 
      } 
      return encryptedData; 
     } 

但在這裏我們得到不同的輸出。

的Java OP: -

OYbW6pI8mgqU5xOcfG8N92e28T9GUObtcea4XWqU0yQyJRULSLV/yjAzDh8gq9Hgj5K5OubZfdm/ /ts66eQMJYH4TBX0/hN5zPwQbdTWmfVU3dDyU2SyQek5zYcWW + OgnppL9jcMcJZg4pv2 + q6x8w ==

C#OP: -

OYbW6pI8mgqU5xOcfG8N9wXs2/gWMc6dcUSEoLXm3L5v9Ih9eN63xO31mXmEDLprIzusXaOS1rNNtBPi5I8FG3IukVgicagrk Lul1vfa142z + XDULJXFmg5rxPa6iJzXqeZ6x3wxbfI3T/ZqGwxqbg ==

我們不能讓像Java確切的加密數據。請建議或提供密碼加密/解密的任何鏈接中的Windows Phone 7

+0

亞歷克斯嗨..它的工作。但它顯示不同的答案與android代碼比較。 – Vijay

+0

[在C#中使用Bouncy Castle加密/解密]的可能重複(http:// stackoverflow。com/questions/5910454/encrypt-decrypt-using-bouncy-castle-in-c-sharp) - 根據下面的OP評論。 – Javier

+0

@Javier ..是的。我只從這個鏈接獲得解決方案。我在答案中提供了這個鏈接。 – Vijay

回答

0

它的工作原理我使用LINQPad爲:

Console.Write(String.Join(" ", EncryptWithAES("hello", "AAECAwQFBgcICQoLDA0ODw=="))); 

產量:

91 209 208 157 151 41 81 76 99 8 248 231 34 62 204 1 

也許是特定的窗口電話東西7導致它不起作用?從https://stackoverflow.com/a/2919565/1185053

+0

是的。它正在工作。但加密值不同。我們的Android/Java代碼產生不同的出放.. – Vijay

+0

的Android op是** VoJ5wW5GdGiM0ceMzmbz7Y + zGFExbm + XdSfnFvqBOkQWGaEGeMIn0ime4zXGTI97Ne imCbQa7tyPY/YKMJ6JBlPBZKan/FFII = ** – Vijay

+0

看看http://stackoverflow.com/a/8126022/1185053,他們似乎也有類似的問題。 –

0

關鍵最後,我從計算器解決方案。 I found the solution from here ..它工作正常..

的解決方案是: -

地穴類: -

public class BCEngine 
    { 
     private Encoding _encoding; 
     private IBlockCipher _blockCipher; 
     private PaddedBufferedBlockCipher _cipher; 
     private IBlockCipherPadding _padding; 

     Pkcs7Padding pkcs = new Pkcs7Padding(); 

     public BCEngine(IBlockCipher blockCipher, Encoding encoding) 
     { 
      _blockCipher = blockCipher; 
      _encoding = encoding; 
     } 

     public string Encrypt(string plain, string key) 
     { 
      byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key); 
      return Convert.ToBase64String(result); 
     } 

     public string Decrypt(string cipher, string key) 
     { 
      byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key); 
      return _encoding.GetString(result, 0, result.Length); 
     } 

     private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key) 
     { 
      try 
      {                     
       _cipher = _padding == null ? new PaddedBufferedBlockCipher(_blockCipher) : new PaddedBufferedBlockCipher(_blockCipher, _padding); 
       byte[] keyByte = _encoding.GetBytes(key); 
       _cipher.Init(forEncrypt, new KeyParameter(keyByte)); 
       return _cipher.DoFinal(input); 
      } 
      catch (Org.BouncyCastle.Crypto.CryptoException ex) 
      { 
       throw new CryptoException(ex.Message); 
      } 
     } 

     public string AESEncryption(string plain, string key) 
     { 

      return Encrypt(plain, key); 
     } 

     public string AESDecryption(string cipher, string key) 
     { 
      return Decrypt(cipher, key); 
     } 

     public BCEngine() 
     { 
      _blockCipher = new AesEngine(); 
      _encoding = Encoding.UTF8; 
      pkcs = new Pkcs7Padding(); 
      _padding = pkcs; 
     } 
    } 

現在我可以從任何地方呼叫加密/解密文本。

例子: -

public partial class AesExample : PhoneApplicationPage 
    { 
     public AesExample() 
     { 
      InitializeComponent(); 
      string key = "b09f72a0lkb1lktb"; 

      string plainText = "Text To Encrypt"; 

      BCEngine bcEngine = new BCEngine(); 
      string encryptedString= bcEngine.Encrypt(plainText, key); 
      Console.WriteLine("\n\nEncrypted String==> " + encryptedString); 

      BCEngine bcEnginenew = new BCEngine(); 
      string decryptedString = bcEnginenew.Decrypt(encryptedString, key); 
      Console.WriteLine("\n\nDecrypted String==> " + decryptedString); 
     } 
    } 
+0

現在我編輯了答案。現在可以嗎?感謝您的建議。 – Vijay