当前位置: 首页 > 知识库问答 >
问题:

如何使用BouncyCastle Blowfish实现解密数据?

汪坚
2023-03-14

我正在消耗一个外部API,它返回给我一个河豚加密的JSON数组。首先,我试图使用BountyCastle包实现Blowfish加密/解密方法,该包基于这篇文章c#Bouncy Castle Blowfish Decryption-Pad块损坏。

internal class Program
{
    private static void Main(string[] args)
    {
        string key = "KgKnVRujrgAv4XjD4bKCqdQVN5De0DCw8zpu1URnPw8="; // random
        string content = "[{'id':1},{'id':2}]";

        string encryptedContent = Encrypt(content, key);
        string decryptedContent = Decrypt(encryptedContent, key);

        /*
         
            decryptedContent returns 
        
                [{'id':1},{'id':2}]\0\0\0\0\0

            so I think this should be fine
         
         */
    }

    private static string Encrypt(string content, string encryptionKey)
    {
        byte[] contentBytes = Encoding.UTF8.GetBytes(content);

        return SharedCode(
            contentBytes,
            encryptionKey,
            true,
            encryptedContentBytes => BitConverter
                .ToString(encryptedContentBytes)
                .Replace("-", ""));
    }

    private static string Decrypt(string encryptedContent, string decryptionKey)
    {
        byte[] contentBytes = Hex.Decode(encryptedContent);

        return SharedCode(contentBytes, decryptionKey, false, decryptedContentBytes =>
        {
            string decryptedContentString = BitConverter
                .ToString(decryptedContentBytes)
                .Replace("-", "");

            byte[] hexBytes = new byte[decryptedContentString.Length / 2];
            
            for (int i = 0; i < hexBytes.Length; i++)
            {
                string currentHexString = decryptedContentString.Substring(i * 2, 2);
                hexBytes[i] = Convert.ToByte(currentHexString, 16);
            }

            return Encoding.UTF8.GetString(hexBytes);
        });
    }

    private static string SharedCode(byte[] contentBytes, string key, bool forceEncryption, Func<byte[], string> processor)
    {
        BlowfishEngine blowfishEngine = new BlowfishEngine();
        PaddedBufferedBlockCipher paddedBufferedBlockCipher = new PaddedBufferedBlockCipher(blowfishEngine);
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);
        KeyParameter keyParameter = new KeyParameter(keyBytes);
        paddedBufferedBlockCipher.Init(forceEncryption, keyParameter);
        int outputLength = paddedBufferedBlockCipher.GetOutputSize(contentBytes.Length);
        byte[] outputBytes = new byte[outputLength];
        int processedBytes = paddedBufferedBlockCipher.ProcessBytes(contentBytes, 0, contentBytes.Length, outputBytes, 0);
        paddedBufferedBlockCipher.DoFinal(outputBytes, processedBytes);

        return processor(outputBytes);
    }
}

现在我想解密API响应。Api返回给我以下Blowfish加密的JSON正文内容

$-1$cb8ba9e30b19ff2a$d1157421764fe503d1fa9810fb9e6c3b424a1e8d014a321f5a2fb47ec6ebc8287d4d6236448d3623be42cf927fb883ca48810037c1a62bd229f937727c272c76420eb1f630bb2856c27d10c955220a1539f64e07c5708db90787ac470cad8372ea086501981c7a53ca69740c7cfced856e234a6801efcf1f71178e756441ba2716ea75ff3e6e002ba08ad18efeef95a909c9a5c68087cc63ed138a63c6788b9bb

我收到了一个解密密钥和这个PHP代码示例作为“文档”。响应本身包含三个组

  • switch语句的字符串(例如-1)

.

<?php
function decryptData(string $data, string $key): string{
    $matches = [];
    if(!preg_match('|^\$([^$]{2,4})\$([a-f0-9]{16,64})\$|i', $data, $matches)){
        return '';
    }
    $data = (string) substr($data, strlen($matches[0]));
    switch($matches[1]){
        default:
            return '';
        case '-1':
            $data = (string) hex2bin($data);
        case '-1a':
            $algo = 'blowfish';
            return (string) openssl_decrypt($data, $algo, (string) base64_decode($key), OPENSSL_RAW_DATA, (string) hex2bin($matches[2]));
    }
}

我个人不知道salt的用途是什么,因为我的实现不需要它,但我尝试将代码更新到这一点

internal class Program
{
    private static void Main(string[] args)
    {
        string key = "KgKnVRujrgAv4XjD4bKCqdQVN5De0DCw8zpu1URnPw8"; // this decryption key is not the correct one to use
        string apiResponse = "$-1$cb8ba9e30b19ff2a$d1157421764fe503d1fa9810fb9e6c3b424a1e8d014a321f5a2fb47ec6ebc8287d4d6236448d3623be42cf927fb883ca48810037c1a62bd229f937727c272c76420eb1f630bb2856c27d10c955220a1539f64e07c5708db90787ac470cad8372ea086501981c7a53ca69740c7ccfced856e234a6801efcf1f71178e75646441ba2716ea75a75ff3e6e002ba08ad18efeef95a909c9a5c68087cc63ed138a63c6788b9bbc43f3c04d2a496660f84ac98f011d3930c61ce9d5565131d2cba65db7c9bef824dd9a6594";

        Match matches = Regex.Match(apiResponse, @"^\$([^$]{2,4})\$([a-f0-9]{16,64})\$([a-f0-9]*)");
        Group algorithm = matches.Groups[1];
        Group salt = matches.Groups[2];
        Group content = matches.Groups[3];
        string encryptedContent = content.ToString();
        string decryptedContent = Decrypt(encryptedContent, key);
    }

    private static string Decrypt(string encryptedContent, string decryptionKey)
    {
        byte[] contentBytes = Hex.Decode(encryptedContent);
        BlowfishEngine blowfishEngine = new BlowfishEngine();
        PaddedBufferedBlockCipher paddedBufferedBlockCipher = new PaddedBufferedBlockCipher(blowfishEngine);
        byte[] keyBytes = Encoding.UTF8.GetBytes(decryptionKey);
        KeyParameter keyParameter = new KeyParameter(keyBytes);
        paddedBufferedBlockCipher.Init(false, keyParameter);
        int outputLength = paddedBufferedBlockCipher.GetOutputSize(contentBytes.Length);
        byte[] outputBytes = new byte[outputLength];
        int processedBytes = paddedBufferedBlockCipher.ProcessBytes(contentBytes, 0, contentBytes.Length, outputBytes, 0);
        paddedBufferedBlockCipher.DoFinal(outputBytes, processedBytes); // throws Org.BouncyCastle.Crypto.InvalidCipherTextException: 'pad block corrupted'

        string decryptedContentString = BitConverter
            .ToString(outputBytes)
            .Replace("-", "");

        byte[] hexBytes = new byte[decryptedContentString.Length / 2];

        for (int i = 0; i < hexBytes.Length; i++)
        {
            string currentHexString = decryptedContentString.Substring(i * 2, 2);
            hexBytes[i] = Convert.ToByte(currentHexString, 16);
        }

        return Encoding.UTF8.GetString(hexBytes);
    }
}

不幸的是,代码在

paddedBufferedBlockCipher.DoFinal(outputBytes, processedBytes);

抛出一个

组织。弹跳船舱。加密。InvalidCipherTextException:“填充块已损坏”

例外有人知道如何解密这个Api响应吗?

共有1个答案

巫马玉堂
2023-03-14

PHP代码首先将这三部分分开。第二部分是IV,它是十六进制解码的,因此大小为8字节。第三部分是数据,由于第一部分中的-1,首先对数据进行十六进制解码,然后通过在CBC模式下使用Blowfish和PKCS7填充,使用密钥和IV进行解密。

为了检查C#实现,测试数据非常有用:

  • 作为20字节密钥,应用以下Base64解码密钥:
  MDEyMzQ1Njc4OTAxMjM0NTY3ODk=
  • 使用密文时:
  $-1$cb8ba9e30b19ff2a$1cb8430f6b9c109e4334874408dbd26be36b0c9600383d63afd70669efcec38bea1290dfbe6b71519b1f48b514957845

如果在PHP代码中使用此数据,decryptData()返回:

The quick brown fox jumps over the lazy dog

必须对C代码进行以下更改:

  • 审议第四次会议
  • 使用CBC模式和PKCS7衬垫
  • 密钥的Base64解码

这将导致以下实施:

private static void Main(string[] args)
{
    //string key = "KgKnVRujrgAv4XjD4bKCqdQVN5De0DCw8zpu1URnPw8"; // this decryption key is not the correct one to use
    //string apiResponse = "$-1$cb8ba9e30b19ff2a$d1157421764fe503d1fa9810fb9e6c3b424a1e8d014a321f5a2fb47ec6ebc8287d4d6236448d3623be42cf927fb883ca48810037c1a62bd229f937727c272c76420eb1f630bb2856c27d10c955220a1539f64e07c5708db90787ac470cad8372ea086501981c7a53ca69740c7ccfced856e234a6801efcf1f71178e75646441ba2716ea75a75ff3e6e002ba08ad18efeef95a909c9a5c68087cc63ed138a63c6788b9bbc43f3c04d2a496660f84ac98f011d3930c61ce9d5565131d2cba65db7c9bef824dd9a6594";

    string key = "MDEyMzQ1Njc4OTAxMjM0NTY3ODk=";
    string apiResponse = "$-1$cb8ba9e30b19ff2a$1cb8430f6b9c109e4334874408dbd26be36b0c9600383d63afd70669efcec38bea1290dfbe6b71519b1f48b514957845";

    Match matches = Regex.Match(apiResponse, @"^\$([^$]{2,4})\$([a-f0-9]{16,64})\$([a-f0-9]*)");
    Group algorithm = matches.Groups[1];
    Group iv = matches.Groups[2];                                                               // 2nd part is the IV
    Group content = matches.Groups[3];      

    string decryptedContent = Decrypt(content.ToString(), key, iv.ToString());                  // Pass additionally the IV
    Console.WriteLine(decryptedContent);
}

private static string Decrypt(string encryptedContent, string decryptionKey, string iv)
{
    byte[] contentBytes = Hex.Decode(encryptedContent);

    byte[] keyBytes = Convert.FromBase64String(decryptionKey);                                  // Base64 decode the key
    KeyParameter keyParameter = new KeyParameter(keyBytes);
    ParametersWithIV keyParamWithIv = new ParametersWithIV(keyParameter, Hex.Decode(iv));       // Consider the IV

    BlowfishEngine blowfishEngine = new BlowfishEngine(); 
    PaddedBufferedBlockCipher paddedBufferedBlockCipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(blowfishEngine), new Pkcs7Padding()); // Consider CBC mode and PKCS7 padding
    paddedBufferedBlockCipher.Init(false, keyParamWithIv);
    int outputLength = paddedBufferedBlockCipher.GetOutputSize(contentBytes.Length);
    byte[] outputBytes = new byte[outputLength];
    int processedBytes = paddedBufferedBlockCipher.ProcessBytes(contentBytes, 0, contentBytes.Length, outputBytes, 0);
    processedBytes += paddedBufferedBlockCipher.DoFinal(outputBytes, processedBytes); 
    
    return Encoding.UTF8.GetString(outputBytes, 0, processedBytes); // The quick brown fox jumps over the lazy dog
}

如果执行此代码,则测试结果为明文。因此,使用此代码,您的密文应该可以使用密钥进行解密。

 类似资料:
  • 我正在做一个简单的程序来加密/解密使用RSA算法在Java。我创建一个密码对象如下: 我通过调用加密函数进行加密: 解密过程如下: 但是,当我将创建密码对象的代码编辑为://Create a Cipher object Cipher rsapier=Cipher时。getInstance(“RSA”); 问题出在哪里。在第一种情况下(当空格出现时),我指定了NoPadd?为什么空格出现在解密的消息

  • 下面是我到现在为止所尝试的 下面是适用于我的node.js代码。在CryptoJ中,我没有成功地实现类似的功能。根据我的理解,crypto是内置库,其中的节点在其上有自己的包装器。

  • 本文向大家介绍C#实现数据包加密与解密实例详解,包括了C#实现数据包加密与解密实例详解的使用技巧和注意事项,需要的朋友参考一下 在很多项目中,为了安全安全考虑,需要对数据包进行加密处理,本文实例所述的即为C#加密代码,在应用开发中有很大的实用价值。说起数据包加密,其实对C#编程者来说,应该是一个基础的技巧,是进行C#程序设计人员必须要掌握的技能。 C#实现加密功能的核心代码如下: 本例备有详细的注

  • 本文向大家介绍java使用RSA加密方式实现数据加密解密的代码,包括了java使用RSA加密方式实现数据加密解密的代码的使用技巧和注意事项,需要的朋友参考一下 RSA的应用 RSA是一种非对称加密算法。现在,很多登陆表单的密码的都采用RSA加密,例如京东中的登陆使用公钥对密码进行加密 java使用RSA加密方式实现数据加密解密,需要首先产生私钥和公钥 测试代码 RSA工具类的实现 总结 以上所述是

  • 问题内容: 我正在连接一个旧的Java应用程序(无法更改该应用程序),该应用程序正在使用AES加密数据。这是原始Java代码如何实例化AES密码: 我是C / C ++开发人员,而不是Java,但是从我可以看出来的传统Java代码中,既没有指定模式,也没有指定初始化向量。有人偶然知道默认情况下将使用什么Java,因为未指定它? 我们需要新的C / C ++应用程序来解密Java加密的数据。但是我不

  • 我正在尝试用两个不同的服务提供商解密加密的数据。 我使用的加密算法是:AES/CTR/PKCS5Padding(AESCounter Mode with PKCS5Padding) 在从另一端进行加密的同时,他们使用SunJCE作为使用Python的服务提供者。 我正在尝试使用JAVA中的Bouncy Castle作为提供程序来解密加密的数据(来自python)。 我使用了相同的密钥和用于加密的I