我的 TripleDES 包装器不起作用

发布于 2024-11-25 10:38:32 字数 2160 浏览 0 评论 0原文

快速跟进我最后一个与 TripleDES 相关的问题,这是我的 TripleDES包装器代码,用于加密和解密整数:

public static class Crypto {

    private static Byte[]    _fixedIv = new Byte[] { /* 8 random bytes, const */ };

    private static TripleDES _tripleDes;
    private static Byte[]    _key;

    static Crypto() {

        _tripleDes = TripleDES.Create();
        _tripleDes.Mode = CipherMode.CFB;

        String key = ConfigurationManager.AppSettings["cryptoKeyId"];
        _key = Convert.FromBase64String( key );
    }

    /// <summary>Encrypts the specified integer using the configuration-stored key.</summary>
    public static String EncryptID(Int32 id) {

        Byte[] input = new Byte[8]; // 64-bit block size
        Byte[] inputLo = BitConverter.GetBytes( id );
        for(int i=0;i<inputLo.Length;i++) input[i] = inputLo[i];

        ICryptoTransform tr = _tripleDes.CreateEncryptor( _key, _fixedIv );

        Byte[] output = new Byte[8];
        tr.TransformBlock( input, 0, input.Length, output, 0 );

        return Convert.ToBase64String( output );
    }

    /// <summary>Decrypts the specified string (storing an integer) using the configuration-stored key.</summary>
    public static Int32 DecryptID(String s) {

        Byte[] ciphertext = Convert.FromBase64String(s);

        ICryptoTransform tr = _tripleDes.CreateDecryptor( _key, _fixedIv );

        Byte[] output = new Byte[8];
        tr.TransformBlock( ciphertext, 0, ciphertext.Length, output, 0 );

        Byte[] outputLo = new Byte[4] { output[0], output[1], output[2], output[3] };
        return BitConverter.ToInt32( outputLo, 0 );
    }

}

当我运行它时,我会得到 EncryptID 的每个输入的确定性结果,但每次调用DecryptID 返回零。我进入代码并说“输出”数组的内容全部为零(并且 tr.TransformBlock 返回零)。有谁知道我做错了什么?

我尝试调用 tr.TransformFinalBlock ,但出现异常:

'tr.TransformFinalBlock( ciphertext, 0, ciphertext.Length )' threw an exception of type System.Security.Cryptography.CryptographicException' base {System.SystemException}: {"Bad Data.\r\n"}

Swiftly following on from my last TripleDES-related question, here's my TripleDES wrapper code, which is used to encrypt and decrypt integers:

public static class Crypto {

    private static Byte[]    _fixedIv = new Byte[] { /* 8 random bytes, const */ };

    private static TripleDES _tripleDes;
    private static Byte[]    _key;

    static Crypto() {

        _tripleDes = TripleDES.Create();
        _tripleDes.Mode = CipherMode.CFB;

        String key = ConfigurationManager.AppSettings["cryptoKeyId"];
        _key = Convert.FromBase64String( key );
    }

    /// <summary>Encrypts the specified integer using the configuration-stored key.</summary>
    public static String EncryptID(Int32 id) {

        Byte[] input = new Byte[8]; // 64-bit block size
        Byte[] inputLo = BitConverter.GetBytes( id );
        for(int i=0;i<inputLo.Length;i++) input[i] = inputLo[i];

        ICryptoTransform tr = _tripleDes.CreateEncryptor( _key, _fixedIv );

        Byte[] output = new Byte[8];
        tr.TransformBlock( input, 0, input.Length, output, 0 );

        return Convert.ToBase64String( output );
    }

    /// <summary>Decrypts the specified string (storing an integer) using the configuration-stored key.</summary>
    public static Int32 DecryptID(String s) {

        Byte[] ciphertext = Convert.FromBase64String(s);

        ICryptoTransform tr = _tripleDes.CreateDecryptor( _key, _fixedIv );

        Byte[] output = new Byte[8];
        tr.TransformBlock( ciphertext, 0, ciphertext.Length, output, 0 );

        Byte[] outputLo = new Byte[4] { output[0], output[1], output[2], output[3] };
        return BitConverter.ToInt32( outputLo, 0 );
    }

}

When I run it, I get deterministic results for each input to EncryptID, but every call to DecryptID returns zero. I stepped into the code and say that the contents of the 'output' array are all zero (and tr.TransformBlock returns zero). Does anyone know what I'm doing wrong?

I tried calling tr.TransformFinalBlock but I got an exception:

'tr.TransformFinalBlock( ciphertext, 0, ciphertext.Length )' threw an exception of type System.Security.Cryptography.CryptographicException' base {System.SystemException}: {"Bad Data.\r\n"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2024-12-02 10:38:32

事实证明,对于单个块,我应该使用 TransformFinalBlock 进行加密和解密。

我还必须将 algo.Padding 设置为 None 以确保 8 字节的明文转换为 8 字节的密文。

It turns out that for single blocks I should be using TransformFinalBlock for both encryption and decryption.

I also had to set algo.Padding to None to ensure that 8 bytes of clear is transformed into 8 bytes of ciphertext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文