Encryption error in c#

Asked

Viewed 105 times

3

Hello! I’ve tried to analyze the code but I can’t find the answer

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.");
        }

    }
}

When Run this error comes

Server Error in '/' Application.

Length of the data to Decrypt is invalid.

Description: An unhandled Exception occurred During the Execution of the Current web request. Please review the stack trace for more information about the error and Where it originated in the code.

Exception Details: System.Security.Cryptography.Cryptographicexception: Length of the data to Decrypt is invalid.

Source Error:

Line 142: //write encrypted data to memorystream Line 143: _cryptoStream.Write(plainByte, 0, plainByte.Length); Line 144:
_cryptoStream.Flushfinalblock(); Line 145: //searches as much as encrypted data Line 146: byte[] cryptoByte = _memoryStream.Toarray();

  • Joany, make your lines 142~144 available for us, please.

  • That was the mistake!

1 answer

4


In the Encryption method you are using the Createdecryptor method instead of Createencryptor.

Change:

CryptoStream encryptionStream = new CryptoStream(tempStream, encryptor.CreateDecryptor(key, iv),
            CryptoStreamMode.Write);

for

CryptoStream encryptionStream = new CryptoStream(tempStream, encryptor.CreateEncryptor(key, iv),
            CryptoStreamMode.Write);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.