コード例 #1
0
ファイル: SM4.cs プロジェクト: guanwu/guanwu-toolkit
        public byte[] EncryptCBC(Sm4Context ctx, byte[] iv, byte[] input)
        {
            if (ctx.IsPadding)
            {
                input = Padding(input);
            }

            int length = input.Length;

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

            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 (int i = 0; i < 16; i++)
                {
                    outBytes[i] = ((byte)(inBytes[i] ^ iv[i]));
                }
                Sm4OneRound(ctx.SubKeys, outBytes, out1);
                Array.Copy(out1, 0, iv, 0, 16);
                for (int k = 0; k < 16; k++)
                {
                    bousList.Add(out1[k]);
                }
            }

            var output = bousList.ToArray();

            for (int i = 0; i < output.Length; i++)
            {
                if (output[i] < 0)
                {
                    output[i] = (byte)(output[i] + 256);
                }
            }

            return(output);
        }
コード例 #2
0
ファイル: SM4.cs プロジェクト: guanwu/guanwu-toolkit
 public void SetKeyEnc(Sm4Context ctx, byte[] key)
 {
     SetKey(ctx.SubKeys, key);
 }