示例#1
0
        public void TestEncryptorDecryptor()
        {
            //FileReadWriteHelper fileReadWriteHelper = new FileReadWriteHelper(privateKeyDirectoryPath, fileName);
            //AESCryptor aESCryptor = new AESCryptor(new JsonSerializationHelper<KeyValuePair<String, String>>(),fileReadWriteHelper);

            ICryptor aESCryptor = Factory.CreateCryptor();

            String encrypted = Convert.ToBase64String(aESCryptor.Encryptor(Encoding.UTF8.GetBytes(input)));

            Assert.IsNotNull(encrypted);
            Console.WriteLine(encrypted);

            String outPut = Encoding.UTF8.GetString(aESCryptor.Decryptor(Convert.FromBase64String(encrypted)));

            Assert.IsNotNull(outPut);
            Console.WriteLine(outPut);
            Console.WriteLine(outPut.Length - input.Length);
            Assert.IsTrue(outPut.Length > 0);
            Assert.IsTrue(outPut.Length == input.Length);
        }
        protected virtual byte[] Decryptor(byte[] bytes)
        {
            /**
             * 验证签名
             * 用密码解密数据
             * 对数据码解
             * */

            if (!Sign.Verify(bytes, GetSignBytes(), out bytes))
            {
                throw new Exception("密文被篡改!");
            }

            try
            {
                bytes = AesCryptoHelper.Decrypt(bytes, GetKeyBytesByPasword(), null);
            }
            catch (Exception ex)
            {
                throw new Exception("密码不正确!", ex.InnerException);
            }
            bytes = Cryptor.Decryptor(bytes);
            return(bytes);
        }