Exemplo n.º 1
0
        /// <summary>
        /// SM4 Crypt ECB
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// SM4 SetKey DEC
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="key"></param>
        public 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);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// SM4 Crypt CBC
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="iv"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public 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, i * 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, i * 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());
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// SM4 SetKey ENC
 /// </summary>
 /// <param name="ctx"></param>
 /// <param name="key"></param>
 public void sm4_setkey_enc(SM4Context ctx, byte[] key)
 {
     ctx.Mode = SM4_ENCRYPT;
     sm4_setkey(ctx.SK, key);
 }