Exemplo n.º 1
0
        public static byte[] Encrypt_ECB(byte[] plainBytes, byte[] keyBytes)
        {
            SM4_Context ctx = new SM4_Context();

            SM4Base sm4 = new SM4Base();

            sm4.sm4_setkey_enc(ctx, keyBytes);
            byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainBytes);
            return(encrypted);
        }
Exemplo n.º 2
0
        public static byte[] Decrypt_CBC(byte[] plainBytes, byte[] keyBytes, byte[] ivBytes)
        {
            SM4_Context ctx = new SM4_Context();

            SM4Base sm4 = new SM4Base();

            sm4.sm4_setkey_dec(ctx, keyBytes);
            byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainBytes);
            return(encrypted);
        }
Exemplo n.º 3
0
        public void sm4_setkey_dec(SM4_Context 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.º 4
0
        public byte[] sm4_crypt_ecb(SM4_Context ctx, byte[] input)
        {
            int length = input.Length;

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

            return(bous);
        }
Exemplo n.º 5
0
        public byte[] sm4_crypt_cbc(SM4_Context ctx, byte[] iv, byte[] input)
        {
            int i      = 0;
            int length = input.Length;

            byte[] bins = new byte[length];
            Array.Copy(input, 0, bins, 0, length);
            byte[] bous     = new byte[length];
            byte[] inBytes  = new byte[16];
            byte[] xorBytes = new byte[16];
            byte[] resBytes = new byte[16];

            if (ctx.mode == SM4_ENCRYPT)
            {
                for (int j = 0; length > 0; length -= 16, j++)
                {
                    Array.Copy(bins, j * 16, inBytes, 0, 16);
                    for (i = 0; i < 16; i++)
                    {
                        xorBytes[i] = ((byte)(inBytes[i] ^ iv[i]));
                    }
                    sm4_one_round(ctx.sk, xorBytes, resBytes);
                    Array.Copy(resBytes, 0, iv, 0, 16);
                    Array.Copy(resBytes, 0, bous, j * 16, 16);
                }
            }
            else
            {
                byte[] temp = new byte[16];
                for (int j = 0; length > 0; length -= 16, j++)
                {
                    Array.Copy(bins, j * 16, inBytes, 0, 16);
                    sm4_one_round(ctx.sk, inBytes, resBytes);
                    for (i = 0; i < 16; i++)
                    {
                        xorBytes[i] = ((byte)(resBytes[i] ^ iv[i]));
                    }
                    Array.Copy(inBytes, 0, iv, 0, 16);
                    Array.Copy(xorBytes, 0, bous, j * 16, 16);
                }
            }

            return(bous);
        }
Exemplo n.º 6
0
 public void sm4_setkey_enc(SM4_Context ctx, byte[] key)
 {
     ctx.mode = SM4_ENCRYPT;
     sm4_setkey(ctx.sk, key);
 }