Error in encryption program c#

Asked

Viewed 71 times

0

Hello, I am trying to run my program done in c# with Windows application, but it is giving a build error that I am not able to solve. I thank anyone who can help me ! Thanks ! ( The error is happening in the void method Decode )

using System;
using System.Linq;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace CriptografiaTrabalho
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

        }
        void Button1Click(object sender, EventArgs e)
        {
            textBox2.Text = Codificar(textBox1.Text);
        }
        void Button2Click(object sender, EventArgs e)
        {
            textBox3.Text = Decodificar(textBox2.Text);
        }

        public static string Codificar(string entrada) 
        {
            TripleDESCryptoServiceProvider cript = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            try
            {
                if (entrada.Trim() !="")
                {
                    string chave = "cx3f2e4ct4d2";
                    cript.Key = md5.ComputeHash(Encoding.Default.GetBytes(chave));
                    cript.Mode = CipherMode.ECB;
                    ICryptoTransform dencrypt = cript.CreateEncryptor();
                    byte[] buff = Encoding.Default.GetBytes(entrada);

                    return Convert.ToBase64String(dencrypt.TransformFinalBlock(buff, 0, buff.Length));
                }
                else 
                {
                    return "";
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally 
            {
                cript = null;
                md5 = null;
            }
        }

        public static string Decodificar(string entrada) 
        {
            TripleDESCryptoServiceProvider decript = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            try 
            {
                if (entrada.Trim() != "")
                {
                    string chave = "cx3f2e4ct4d2";
                    decript.Key = md5.ComputeHash(Encoding.Default.GetBytes(chave));
                    decript.Mode = CipherMode.ECB;
                    ICryptoTransform dencrypt = decript.CreateEncryptor();
                    byte[] buff = Convert.FromBase64String(entrada);

                    return Encoding.Default.GetString(dencrypt.TransformFinalBlock(buff, 0, buff.Length));
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally 
            {
                decript = null;
                md5 = null;
            }
        }
    }
}
  • 2

    What is the build error? What is the message?

  • Sorry about the mistake in public Static void Decode (input string) the message is (Cryptographic.MainForm.Decode(string): not all code paths return a value (CS0161)

1 answer

0


There is no Return in the method, there is a Return inside the if but only it does not help, because if the condition is not met, there is no "where to go".

Try it this way:

 public static string Decodificar(string entrada) 
    {
        TripleDESCryptoServiceProvider decript = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

        try 
        {
            if (entrada.Trim() != "")
            {
                string chave = "cx3f2e4ct4d2";
                decript.Key = md5.ComputeHash(Encoding.Default.GetBytes(chave));
                decript.Mode = CipherMode.ECB;
                ICryptoTransform dencrypt = decript.CreateEncryptor();
                byte[] buff = Convert.FromBase64String(entrada);

                return Encoding.Default.GetString(dencrypt.TransformFinalBlock(buff, 0, buff.Length));
            }
    return "";  
        }
        catch (Exception exception)
        {
            throw exception;
        }
        finally 
        {
            decript = null;
            md5 = null;
        }
    }

Notice that after the if I put an empty return..

  • Thank you Igor really didn’t even notice q had the Else giving a feedback. Vlw man ! Thank you !

Browser other questions tagged

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