2024年05月08日
C#和PHP的国密SM3、SM4_ecb的加密解密
分类:技术

PHP

依赖于openssl扩展

sm3

openssl_digest($data, 'sm3');

sm4

class Sm4
{
    public static function encrypt($data, $key)
    {
        return bin2hex(openssl_encrypt($data, 'sm4-ecb', hex2bin($key), OPENSSL_RAW_DATA));
    }
    public static function decrypt($data, $key)
    {
        return openssl_decrypt(hex2bin($data), 'sm4-ecb', hex2bin($key), OPENSSL_RAW_DATA);
    }
}

C#

依赖于BouncyCastle.Crypto

sm3

static string Sm3(string data)
{
    byte[] msg1 = Encoding.UTF8.GetBytes(data);
    Org.BouncyCastle.Crypto.Digests.SM3Digest sm3 = new Org.BouncyCastle.Crypto.Digests.SM3Digest();
    sm3.BlockUpdate(msg1, 0, msg1.Length);
    byte[] result = new byte[sm3.GetDigestSize()];

    sm3.DoFinal(result, 0);
    return new UTF8Encoding().GetString(Org.BouncyCastle.Utilities.Encoders.Hex.Encode(result));
}

sm4

using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using Sw.ChinaEncryptSM;
using System.Text;
using System.Text.RegularExpressions;

namespace Test.SM4;

public class SM4
{
    public static byte[] ECBEncrypt(byte[] input, string key)
    {
        var engine = new SM4Engine();
        var cipher = new PaddedBufferedBlockCipher(engine, new Pkcs7Padding());
        var keyParam = new KeyParameter(Decode(key));
        cipher.Init(true, keyParam);

        var output = new byte[cipher.GetOutputSize(input.Length)];
        var length = cipher.ProcessBytes(input, 0, input.Length, output, 0);
        cipher.DoFinal(output, length);

        return output;
    }
    public static string ECBEncryptToString(string input, string key)
    {
        return BitConverter.ToString(ECBEncrypt(Encoding.Default.GetBytes(input), key), 0).Replace("-", string.Empty).ToLower();
    }
    public static byte[] ECBDecrypt(byte[] input, string key)
    {
        var engine = new SM4Engine();
        var cipher = new PaddedBufferedBlockCipher(engine, new Pkcs7Padding());
        var keyParam = new KeyParameter(Decode(key));
        cipher.Init(false, keyParam);

        var output = new byte[cipher.GetOutputSize(input.Length)];
        var length = cipher.ProcessBytes(input, 0, input.Length, output, 0);
        cipher.DoFinal(output, length);
        return RemovePadding(output);
    }
    public static string ECBDecryptToString(string input, string key)
    {
        return Encoding.Default.GetString(ECBDecrypt(Enumerable.Range(0, input.Length)
                                 .Where(x => x % 2 == 0)
                                 .Select(x => Convert.ToByte(input.Substring(x, 2), 16))
                                 .ToArray(), key));
    }

    public static byte[] RemovePadding(byte[] data)
    {
        var lastNonZeroByte = data.Length - 1;
        while (data[lastNonZeroByte] == 0)
        {
            lastNonZeroByte--;
        }

        var result = new byte[lastNonZeroByte + 1];
        Array.Copy(data, result, lastNonZeroByte + 1);
        return result;
    }
    public static byte[] Decode(string key)
    {
        return Regex.IsMatch(key, "^[0-9a-f]+$", RegexOptions.IgnoreCase) ? Hex.Decode(key) : Convert.FromBase64String(key);
    }
}


(文章为本站原创或AI生成,配图部分来自于网络,不用于商业活动或盈利,如有侵权请及时联系删除)

关键词: PHP C# 国密 SM3 SM4,加密