2014-01-09 61 views
1

我們有一個Windows Phone 8應用程序需要與使用Chilkat加密某些數據的Web服務進行通信。據我所知,奇爾卡特不支持Windows Phone平臺。我有關於如何加密數據的密鑰和其他信息(例如加密算法名稱,密鑰長度等),但是如果沒有此庫,我能夠在Windows Phone上進行加密/解密嗎? (我們已經有使用相同服務的Android/iOS應用和他們所使用的奇爾卡特庫隱窩中的數據)如何在不使用Chilcat庫的情況下解密使用Chilkat進行加密的數據

class Program 
{ 
    static readonly string keyString = "MyKey"; 
    static readonly string iv = "MyIV"; 
    static Encoding TheEncoding = Encoding.UTF8; 

    static void Main(string[] args) 
    { 
     //I got Chilkat and BouncyCastle via NuGet 
     //https://www.nuget.org/packages/WinRTBouncyCastle/0.1.1.1 
     //chilcat-win32 

     var original = "clear text"; 

     var chilkatCrypt = GetChilkat3Des(); 

     //this is equalent to an encrypted text I get from the service 
     var ckEncrypted = chilkatCrypt.EncryptStringENC(original); 

     var ckDecrypted = chilkatCrypt.DecryptStringENC(ckEncrypted); 

     if (!string.Equals(original, ckDecrypted)) throw new ArgumentException("chilkat encrypt/decrypt failure..."); 

     //now comes the challenge, to decrypt the Chilkat encryption with BouncyCastle (or what ever crypto lib that runs on WP8) 
     //this is where i need help :) 
     byte[] chilkatEncBytes = System.Text.Encoding.UTF8.GetBytes(ckEncrypted); 
     var bouncyDecrypted = BouncyCastleDecrypt(chilkatEncBytes); 
    } 

    public static Chilkat.Crypt2 GetChilkat3Des() 
    { 
     var crypt = new Chilkat.Crypt2(); 

     if (!crypt.UnlockComponent("Start my 30-day Trial")) 
     { 
      throw new Exception("Unlock Chilkat failed"); 
     } 

     crypt.CryptAlgorithm = "3des"; 
     crypt.CipherMode = "cbc"; 
     crypt.KeyLength = 192; 
     crypt.PaddingScheme = 0; 
     // It may be "hex", "url", "base64", or "quoted-printable". 
     crypt.EncodingMode = "hex"; 
     crypt.SetEncodedIV(iv, crypt.EncodingMode); 
     crypt.SetEncodedKey(keyString, crypt.EncodingMode); 
     return crypt; 
    } 

    //this code is more or less copied from here: 
    //http://nicksnettravels.builttoroam.com/post/2012/03/27/TripleDes-Encryption-with-Key-and-IV-for-Windows-Phone.aspx 
    public static byte[] RunBouncyCastleTripleDes(byte[] input, bool encrypt) 
    { 
     byte[] byteKey = new byte[24]; 

     Buffer.BlockCopy(TheEncoding.GetBytes(keyString), 0, byteKey, 0, TheEncoding.GetBytes(keyString).Length); 

     var IV = new byte[8]; 

     Buffer.BlockCopy(TheEncoding.GetBytes(iv), 0, IV, 0, TheEncoding.GetBytes(iv).Length); 

     var keyParam = new Org.BouncyCastle.Crypto.Parameters.DesEdeParameters(byteKey); 

     var ivParam = new Org.BouncyCastle.Crypto.Parameters.ParametersWithIV(keyParam, IV); 
     var engine = Org.BouncyCastle.Security.CipherUtilities.GetCipher("DESede/CBC/PKCS5Padding"); 

     engine.Init(encrypt, ivParam); 
     var output = engine.DoFinal(input); 

     return output; 
    } 

    public static byte[] BouncyCastleEncrypt(byte[] input) 
    { 
     return RunBouncyCastleTripleDes(input, true); 
    } 

    public static byte[] BouncyCastleDecrypt(byte[] input) 
    { 
     return RunBouncyCastleTripleDes(input, false); 
    } 
} 
+0

如果您知道數據是如何加密並擁有密鑰的,那麼您應該反過來執行該過程。 – RacerNerd

回答

0

我有關於數據是如何加密的密鑰和其他信息(如加密算法名稱,密鑰長度等),但是我能否在Windows Phone上進行加密/解密而無需使用此庫?

這取決於,但答案可能是肯定的。

如果他們有一個衆所周知的算法的本土實施,那麼他們可能有一個錯誤,答案可能是NO。

如果他們使用已知的算法形式,經過良好的審查庫已經完全指定了算法和參數,答案可能是YES。

+1

我有服務器上的奇爾卡特,它使用3des,並且我試圖在windows phone客戶端上使用bounchecastle進行解密,但是我還沒有成功。也許我會在這裏發佈一些代碼,看看我能否得到一些建議。 – Aegir

+0

「也許我會在這裏發佈一些代碼,看看我能否得到一些建議。」 - 是的,發佈他們的規範和一些示例代碼。 – jww

+0

我發佈了一個代碼示例的更新 – Aegir

相關問題