示例#1
0
        private string EncryptCBC(string plainText)
        {
            if (_iv == null)
            {
                throw new NotSupportedException("对象初始化的时候没有设置iv向量,所以不能使用CBC模式进行加解密");
            }

            var ctx = new SM4Context();

            ctx.isPadding = true;
            ctx.mode      = SM4_ENCRYPT;

            sm4_setkey_enc(ctx, _secretKey);
            var encrypted = sm4_crypt_cbc(ctx, _iv, _encoding.GetBytes(plainText));

            switch (_encryptionResultType)
            {
            case EncryptionResultTypes.Base64String:
                return(Convert.ToBase64String(encrypted));

            case EncryptionResultTypes.HexString:
                return(_encoding.GetString(Hex.Encode(encrypted)));

            default:
                throw new NotSupportedException("不支持的输出类型" + _encryptionResultType.ToString());
            }
        }
示例#2
0
        private static byte[] sm4_crypt_ecb(SM4Context ctx, byte[] input)
        {
            if (ctx.isPadding && (ctx.mode == SM4_ENCRYPT))
            {
                input = padding(input, SM4_ENCRYPT);
            }

            int length = input.Length;

            byte[] bins = new byte[length];
            Array.Copy(input, 0, bins, 0, length);
            byte[] bous = new byte[length];
            for (int i = 0; length > 0; length -= 16, i++)
            {
                byte[] inBytes  = new byte[16];
                byte[] outBytes = new byte[16];
                Array.Copy(bins, i * 16, inBytes, 0, length > 16 ? 16 : length);
                sm4_one_round(ctx.sk, inBytes, outBytes);
                Array.Copy(outBytes, 0, bous, i * 16, length > 16 ? 16 : length);
            }

            if (ctx.isPadding && ctx.mode == SM4_DECRYPT)
            {
                bous = padding(bous, SM4_DECRYPT);
            }
            return(bous);
        }
示例#3
0
        private string DecryptCBC(string cipherText)
        {
            if (_iv == null)
            {
                throw new NotSupportedException("对象初始化的时候没有设置iv向量,所以不能使用CBC模式进行加解密");
            }

            var ctx = new SM4Context();

            ctx.isPadding = true;
            ctx.mode      = SM4_DECRYPT;

            sm4_setkey_dec(ctx, _secretKey);
            byte[] decrypted;
            switch (_encryptionResultType)
            {
            case EncryptionResultTypes.Base64String:
                decrypted = sm4_crypt_cbc(ctx, _iv, Convert.FromBase64String(cipherText));
                break;

            case EncryptionResultTypes.HexString:
                decrypted = sm4_crypt_cbc(ctx, _iv, Hex.Decode(cipherText));
                break;

            default:
                throw new NotSupportedException("不支持的密文类型" + _encryptionResultType.ToString());
            }

            return(_encoding.GetString(decrypted));
        }
示例#4
0
        private string DecryptECB(string cipherText)
        {
            var ctx = new SM4Context();

            ctx.isPadding = true;
            ctx.mode      = SM4_DECRYPT;


            sm4_setkey_dec(ctx, _secretKey);
            byte[] decrypted;
            switch (_encryptionResultType)
            {
            case EncryptionResultTypes.Base64String:
                decrypted = sm4_crypt_ecb(ctx, Convert.FromBase64String(cipherText));
                break;

            case EncryptionResultTypes.HexString:
                decrypted = sm4_crypt_ecb(ctx, Hex.Decode(cipherText));
                break;

            default:
                throw new NotSupportedException("不支持的密文类型" + _encryptionResultType.ToString());
            }

            return(_encoding.GetString(decrypted));
        }
示例#5
0
        private static void sm4_setkey_dec(SM4Context ctx, byte[] key)
        {
            int i = 0;

            ctx.mode = SM4_DECRYPT;
            sm4_setkey(ctx.sk, key);
            for (i = 0; i < 16; i++)
            {
                SWAP(ctx.sk, i);
            }
        }
示例#6
0
        private string EncryptECB(string plainText)
        {
            var ctx = new SM4Context();

            ctx.isPadding = true;
            ctx.mode      = SM4_ENCRYPT;

            sm4_setkey_enc(ctx, _secretKey);
            var encrypted = sm4_crypt_ecb(ctx, _encoding.GetBytes(plainText));

            //Console.WriteLine(Convert.ToBase64String(encrypted));
            switch (_encryptionResultType)
            {
            case EncryptionResultTypes.Base64String:
                return(Convert.ToBase64String(encrypted));

            case EncryptionResultTypes.HexString:
                return(_encoding.GetString(Hex.Encode(encrypted)));

            default:
                throw new NotSupportedException("不支持的输出类型" + _encryptionResultType.ToString());
            }
        }
示例#7
0
        private static byte[] sm4_crypt_cbc(SM4Context ctx, byte[] iv, byte[] input)
        {
            if (ctx.isPadding && ctx.mode == SM4_ENCRYPT)
            {
                input = padding(input, SM4_ENCRYPT);
            }

            int i      = 0;
            int length = input.Length;

            byte[] bins = new byte[length];
            Array.Copy(input, 0, bins, 0, length);
            byte[]      bous     = null;
            List <byte> bousList = new List <byte>();

            if (ctx.mode == SM4_ENCRYPT)
            {
                for (int j = 0; length > 0; length -= 16, j++)
                {
                    byte[] inBytes  = new byte[16];
                    byte[] outBytes = new byte[16];
                    byte[] out1     = new byte[16];

                    Array.Copy(bins, j * 16, inBytes, 0, length > 16 ? 16 : length);
                    for (i = 0; i < 16; i++)
                    {
                        outBytes[i] = ((byte)(inBytes[i] ^ iv[i]));
                    }
                    sm4_one_round(ctx.sk, outBytes, out1);
                    Array.Copy(out1, 0, iv, 0, 16);
                    for (int k = 0; k < 16; k++)
                    {
                        bousList.Add(out1[k]);
                    }
                }
            }
            else
            {
                byte[] temp = new byte[16];
                for (int j = 0; length > 0; length -= 16, j++)
                {
                    byte[] inBytes  = new byte[16];
                    byte[] outBytes = new byte[16];
                    byte[] out1     = new byte[16];

                    Array.Copy(bins, j * 16, inBytes, 0, length > 16 ? 16 : length);
                    Array.Copy(inBytes, 0, temp, 0, 16);
                    sm4_one_round(ctx.sk, inBytes, outBytes);
                    for (i = 0; i < 16; i++)
                    {
                        out1[i] = ((byte)(outBytes[i] ^ iv[i]));
                    }
                    Array.Copy(temp, 0, iv, 0, 16);
                    for (int k = 0; k < 16; k++)
                    {
                        bousList.Add(out1[k]);
                    }
                }
            }

            if (ctx.isPadding && ctx.mode == SM4_DECRYPT)
            {
                bous = padding(bousList.ToArray(), SM4_DECRYPT);
                return(bous);
            }
            else
            {
                return(bousList.ToArray());
            }
        }
示例#8
0
 private static void sm4_setkey_enc(SM4Context ctx, byte[] key)
 {
     ctx.mode = SM4_ENCRYPT;
     sm4_setkey(ctx.sk, key);
 }