Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
please help me change this to cbc

C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Security.Cryptography;
using System.Text;



public class SSTCryptographer
{
    private static string _key;
        
	public SSTCryptographer()
	{ 
	}

    
    public static string Key
    {
        set
        {
            _key = value;
        }
    }

    
    public static string Encrypt(string strToEncrypt)
    {
        try
        {
            return Encrypt(strToEncrypt, _key);
        }
        catch (Exception ex)
        {
            return "Wrong Input. " + ex.Message;
        }
           
    }
    
    public static string Decrypt(string strEncrypted)
    {
        try
        {
            return Decrypt(strEncrypted, _key);
        }
        catch (Exception ex)
        {
            return "Wrong Input. " + ex.Message;
        }
    }

   
    public static string Encrypt(string strToEncrypt, string strKey)
    {
        try
        {
            TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();

            byte[] byteHash, byteBuff;
            string strTempKey = strKey;

            byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
            objHashMD5 = null;
            objDESCrypto.Key = byteHash;
            objDESCrypto.Mode = CipherMode.EBC; 

            byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
            return Convert.ToBase64String(objDESCrypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
        }
        catch (Exception ex)
        {
            return "Wrong Input. " + ex.Message;
        }
    }


    
    public static string Decrypt(string strEncrypted, string strKey)
    {
        try
        {
            TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();

            byte[] byteHash, byteBuff;
            string strTempKey = strKey;

            byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
            objHashMD5 = null;
            objDESCrypto.Key = byteHash;
            objDESCrypto.Mode = CipherMode.ECB; 

            byteBuff = Convert.FromBase64String(strEncrypted);
            string strDecrypted = ASCIIEncoding.ASCII.GetString(objDESCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            objDESCrypto = null;

            return strDecrypted;
        }
        catch (Exception ex)
        {
            return "Wrong Input. " + ex.Message;
        }
    }
}


What I have tried:

many ivs and RNG lots of get bytes i feel im close i just need someone to edit the code for me
Posted
Updated 8-Feb-18 19:37pm
v2
Comments
Graeme_Grant 9-Feb-18 0:28am    
Fixtheformattingofyourcodeifyouwanthelp. Next, you don't explain where you are stuck, only you want someone to finish your code for you. We are not here to fix your code for you just because you are close.
Member 13664778 9-Feb-18 1:18am    
Maybe you should stick to answering the html w3school forms I think this might be too big for you mate and my code runs perfectly I just want to change it to cbc Thanks for nothing noob
Graeme_Grant 9-Feb-18 1:34am    
No need for the offensive response. I'm no noob, but a CP MVP. If your code runs perfectly and you want someone to convert it for you, then you are asking in the wrong place! Please take the time to read the CP Quick Answers FAQ[^]. Instead, you need to ask here: Hire Freelancers & Find Freelance Jobs Online - Freelancer[^]
Graeme_Grant 9-Feb-18 1:45am    
A quick Google search finds lots of examples: C# TripleDES CipherMode cbc example[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900