你好!我正在尝试加密数据,但每当我运行此错误时,我的代码出现了什么问题?.ERROR:要解密的数据长度无效。 [英] hello! I'm trying to encrypt data but whenever I run this error appears what's wrong with my code?.ERROR:Length of the data to decrypt is invalid.

查看:116
本文介绍了你好!我正在尝试加密数据,但每当我运行此错误时,我的代码出现了什么问题?.ERROR:要解密的数据长度无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web;
using System.IO;
using System.Security.Cryptography;

/// <summary>
/// Summary description for StringEncritacao
/// </summary>
namespace Seguranca
{
    public static  class StringEncritacao
    {
        public static string  Encritacao(string sourceData)
        {
            //define a chave e inicializa o valor do vetor
            byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            try
            {
                //converte o dado para array
                byte[] sourceDataBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(sourceData);
                //obter fluxo de memoria
                MemoryStream tempStream = new MemoryStream();
                //apanha o codificador e o fluxo de codificao
                DESCryptoServiceProvider encryptor = new DESCryptoServiceProvider();
                CryptoStream encryptionStream = new CryptoStream(tempStream, encryptor.CreateDecryptor(key, iv),
                CryptoStreamMode.Write);

                //dado de encriptacao
                encryptionStream.Write(sourceDataBytes, 0,sourceDataBytes.Length);
                encryptionStream.FlushFinalBlock();

                //poe o byte no array
                byte[] encryptedDataBytes = tempStream.GetBuffer();

                //converte o dado de encriptacao para string
                return Convert.ToBase64String(encryptedDataBytes,0,(int)tempStream.Length);
            }
            catch 
            {
                throw new StringEncritacaoExcepion("Incapaz de encriptrar dados");
            }
               
            //
            // TODO: Add constructor logic here
            //
           
        }
        public static string Decriptacao(string sourceData) 
        {
            //define a chave inicializacao  valores vecto
            byte[] key = new byte[] { 1,2,3,4,5,6,7,8 };
            byte[] iv = new byte []{ 1,2,3,4,5,6,7,8 };
            try
            {
                //convert o dado para array de byte
                byte[] encryptedDataBytes = Convert.FromBase64String(sourceData);
                //apanha o codigo do fluxo memoria e enche 
                MemoryStream tempStream = new MemoryStream(encryptedDataBytes, 0, encryptedDataBytes.Length);

                //apanha o decriptador e decriptar o fluxo
                DESCryptoServiceProvider decryptor = new DESCryptoServiceProvider();
                CryptoStream decryptionStream = new CryptoStream(tempStream, decryptor.CreateDecryptor(key, iv), CryptoStreamMode.Read);

                //desicriptar
                StreamReader allDataReader = new StreamReader(decryptionStream);
                return allDataReader.ReadToEnd();
            }
            catch
            {
                throw new StringEncritacaoExcepion("Impossivel desencriptar dados.");
            }

        }
    }

}

谢谢

推荐答案

在方法中,Encritacao将encryptor.CreateDecryptor(key,iv)更改为encryptor.Create 加密器(密钥,iv)
In method Encritacao change encryptor.CreateDecryptor(key, iv) to encryptor.CreateEncryptor(key, iv)


这篇关于你好!我正在尝试加密数据,但每当我运行此错误时,我的代码出现了什么问题?.ERROR:要解密的数据长度无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆