예제 #1
0
        /// <summary>
        /// 接收数据时,将字节数组解密
        /// </summary>
        /// <param name="_bytes">[in] 字节数组</param>
        /// <param name="_key">[in] DES加密用的64位秘钥</param>
        /// <returns>解密后的Byte数组</returns>
        public static byte[] DecryptBytes(byte[] _bytes, string _key)
        {
            Debug.Assert(_key.Length == 8, "<DecryptBytes> key length must be 8.");
            Debug.Assert(_bytes.Length % 8 == 0, "<DecryptBytes> input byte array length must mod 8 == 0.");

            int batch_count = _bytes.Length / 8;

            byte[]       ret = new byte[_bytes.Length];
            MemoryStream ms  = new MemoryStream();
            Des          obj = new Des(_key);

            for (int i = 0; i < batch_count; i++)
            {
                byte[] arr_encrypted_bytes = new byte[8];
                byte[] arr_decrypted_bytes = new byte[8];

                Array.Copy(_bytes, i * 8, arr_encrypted_bytes, 0, 8);
                BitArray bit_arr_encrypted = new BitArray(arr_encrypted_bytes);
                BitArray bit_arr_decrypted = obj.decrypt(bit_arr_encrypted);
                bit_arr_decrypted.CopyTo(arr_decrypted_bytes, 0);
                ms.Write(arr_decrypted_bytes, 0, 8);
            }

            ret = ms.ToArray();

            ms.Close();

            return(ret);
        }
예제 #2
0
파일: Program.cs 프로젝트: pigmon/des_test
        static void Main(string[] args)
        {
            BinaryWriter bw = new BinaryWriter(new FileStream(@"D:\as.txt", FileMode.OpenOrCreate));

            Des      obj    = new Des("T&^9c=A`");
            string   s      = "To be or";
            BitArray cipher = obj.encrypt(s);

            //PrintBitString(cipher);
            byte[] arr = new byte[cipher.Length / 8];
            cipher.CopyTo(arr, 0);
            bw.Write(arr);
            bw.Close();

            BitArray plain = obj.decrypt(cipher);
            //PrintBitString(plain);
        }