Esempio n. 1
0
        // 传入MemoryMappedViewAccessor,加密,解密
        private int EncryptDecrypt0(object arg0)// MemoryMappedViewAccessor accessor, Int64 size)
        {
            MemoryMappedViewAccessor accessor;
            Int64         size;
            exe_to_thread et = (exe_to_thread)arg0;

            accessor = et.accessor;
            size     = et.size;
            //写入文件源数据
            int readCount = 0;

            //每次读16个字节为一次加密分组
            byte[] buffer = new byte[1024];
            Int64  pos    = 0;

            Console.WriteLine("文件线程编号:" + et.ft_threadnum.ToString() + "。处理线程编号:" + et.et_threadnum.ToString());
            while ((size - pos) > 0)
            {
                byte[] temp = null;
                // 剩余的字节数大于1024
                if ((size - pos) > buffer.Length)
                {
                    for (int index = 0; index < buffer.Length; index++)
                    {
                        buffer[index] = accessor.ReadByte(pos + index);
                    }
                    temp = buffer;
                }
                else
                {
                    for (int index = 0; index < size - pos; index++)
                    {
                        buffer[index] = accessor.ReadByte(pos + index);
                        readCount++;
                    }
                    temp = new byte[readCount];
                    Buffer.BlockCopy(buffer, 0, temp, 0, readCount);
                }
                if (flag) // 加密
                {
                    temp = sM4Utils.encryptECB(secruitKey, false, temp);
                }
                else // 解密
                {
                    temp = sM4Utils.decryptECB(secruitKey, false, temp);
                }

                for (int i = 0; i < temp.Length; i++)
                {
                    accessor.Write(pos + i, ref temp[i]);
                }

                pos += temp.Length;
            }
            Console.WriteLine("文件线程编号:" + et.ft_threadnum.ToString() + "。处理线程编号:" + et.et_threadnum.ToString() + "处理完成");
            //accessor.Dispose();
            et.accessor.Dispose();

            return(et.et_threadnum);
        }
Esempio n. 2
0
        public static void main()
        {
            byte[] sourcePath = new byte[] {
                0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
            };
            SM4Utils sm4 = new SM4Utils();

            sm4.secretKey = "0123456789abcdeffedcba9876543210";
            sm4.hexString = true;
            byte[] ciphertext = sm4.encryptECB(sm4.secretKey, false, sourcePath);
            Console.WriteLine(BitConverter.ToString(ciphertext));
            byte[] plaintext = sm4.decryptECB(sm4.secretKey, false, ciphertext);
            Console.WriteLine(BitConverter.ToString(plaintext));
        }