Exemplo n.º 1
0
        public string Decrypt_ECB(string cipherText)
        {
            SM4_Context ctx = new SM4_Context();

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

            byte[] keyBytes;
            if (hexString)
            {
                keyBytes = Hex.Decode(secretKey);
            }
            else
            {
                keyBytes = Encoding.ASCII.GetBytes(secretKey);
            }

            SM4 sm4 = new SM4();

            sm4.sm4_setkey_dec(ctx, keyBytes);
            byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Hex.Decode(cipherText));
            if (decrypted == null)
            {
                return(string.Empty);
            }
            else
            {
                return(Encoding.Default.GetString(decrypted));
            }
        }
Exemplo n.º 2
0
        public string Encrypt_CBC(string plainData)
        {
            SM4_Context ctx = new SM4_Context();

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

            byte[] keyBytes;
            byte[] ivBytes;
            if (hexString)
            {
                keyBytes = Hex.Decode(secretKey);
                ivBytes  = Hex.Decode(iv);
            }
            else
            {
                keyBytes = Encoding.Default.GetBytes(secretKey);
                ivBytes  = Encoding.Default.GetBytes(iv);
            }

            SM4 sm4 = new SM4();

            sm4.sm4_setkey_enc(ctx, keyBytes);
            byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Encoding.Default.GetBytes(plainData));

            //return Hex.Encode(encrypted);
            //return encrypted;
            string cipherText = Encoding.Default.GetString(Hex.Encode(encrypted));

            return(cipherText);
        }
Exemplo n.º 3
0
        public string Decrypt_CBC(string cipherData)
        {
            SM4_Context ctx = new SM4_Context();

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

            byte[] keyBytes;
            byte[] ivBytes;
            if (hexString)
            {
                keyBytes = Hex.Decode(secretKey);
                ivBytes  = Hex.Decode(iv);
            }
            else
            {
                keyBytes = Encoding.Default.GetBytes(secretKey);
                ivBytes  = Encoding.Default.GetBytes(iv);
            }

            SM4 sm4 = new SM4();

            sm4.sm4_setkey_dec(ctx, keyBytes);
            byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Hex.Decode(cipherData));
            if (decrypted == null)
            {
                return(string.Empty);
            }
            else
            {
                return(Encoding.Default.GetString(decrypted));
            }
            //return decrypted;
        }
Exemplo n.º 4
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.º 5
0
        public byte[] sm4_crypt_ecb(SM4_Context ctx, byte[] input)
        {
            if ((ctx.isPadding) && (ctx.mode == SM4_ENCRYPT))
            {
                byte[] temp = padding(input, SM4_ENCRYPT);
                if (temp == null)
                {
                    return(temp);
                }
                else
                {
                    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)
            {
                byte[] temp = padding(bous, SM4_DECRYPT);
                if (temp == null)
                {
                    return(temp);
                }
                else
                {
                    bous = padding(bous, SM4_DECRYPT);
                    return(bous);
                }
            }
            else
            {
                return(bous);
            }
        }
Exemplo n.º 6
0
        public byte[] sm4_crypt_cbc(SM4_Context ctx, byte[] iv, byte[] input)
        {
            if (ctx.isPadding && ctx.mode == SM4_ENCRYPT)
            {
                byte[] temp = padding(input, SM4_ENCRYPT);
                if (temp == null)
                {
                    return(temp);
                }
                else
                {
                    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);//原代码有错,需将i * 16改为j * 16
                    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);//原代码有错,需将i * 16改为j * 16
                    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)
            {
                byte[] temp = padding(bousList.ToArray(), SM4_DECRYPT);
                if (temp == null)
                {
                    return(temp);
                }
                else
                {
                    bous = padding(bousList.ToArray(), SM4_DECRYPT);
                    return(bous);
                }
            }
            else
            {
                return(bousList.ToArray());
            }
        }
Exemplo n.º 7
0
 public void sm4_setkey_enc(SM4_Context ctx, byte[] key)
 {
     ctx.mode = SM4_ENCRYPT;
     sm4_setkey(ctx.sk, key);
 }