【问题标题】:How to Decrypt data that uses Chilkat for Encryption without using Chilcat library如何在不使用 Chilkat 库的情况下解密使用 Chilkat 进行加密的数据
【发布时间】:2014-01-14 05:05:28
【问题描述】:

我们有一个 Windows Phone 8 应用程序需要与使用 Chilkat 加密某些数据的 Web 服务进行通信。据我所知,Chilkat 不支持 Windows Phone 平台。我有关于如何加密数据的密钥和其他信息(例如加密算法名称、密钥长度等),但是如果没有这个库,我能否在 Windows Phone 上加密/解密? (我们已经有使用相同服务的 android/ios 应用程序,它们使用 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);
    }
}

【问题讨论】:

  • 如果您知道数据是如何加密的并且拥有密钥,那么您应该反过来执行该过程。

标签: c# encryption windows-phone chilkat


【解决方案1】:

我有关于如何加密数据的密钥和其他信息(例如加密算法名称、密钥长度等),但是如果没有这个库,我能否在 Windows Phone 上加密/解密?

这取决于,但答案可能是肯定的。

如果他们有一个众所周知的算法的本土实现,那么他们可能有一个错误,答案可能是否定的。

如果他们使用的是经过严格审查的库中的知名算法并且已经完全指定了算法和参数,那么答案很可能是肯定的。

【讨论】:

  • 我在服务器上有 Chilkat,它使用 3des,我尝试在 windows phone 客户端上使用 bounchecastle 解密,但我还没有成功。也许我会在这里发布一些代码,看看是否可以得到一些建议。
  • "也许我会在这里贴一些代码,看看能不能得到一些建议。" - 是的,发布他们的规范和一些示例代码。
猜你喜欢
  • 1970-01-01
  • 2012-07-27
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多