Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 768235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:09:01+00:00 2026-05-14T18:09:01+00:00

public class TrippleENCRSPDESCSP { public TrippleENCRSPDESCSP() { } public void EncryptIt(string sData,ref byte[] sEncData,ref

  • 0
public class TrippleENCRSPDESCSP
{


    public TrippleENCRSPDESCSP()
    {

    }

    public void EncryptIt(string sData,ref byte[] sEncData,ref byte[] Key1,ref byte[] Key2)
    {
        try
        {
            // Create a new TripleDESCryptoServiceProvider object
            // to generate a key and initialization vector (IV).
            TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();

            // Create a string to encrypt.

            // Encrypt the string to an in-memory buffer.
            byte[] Data = EncryptTextToMemory(sData,tDESalg.Key,tDESalg.IV);
            sEncData = Data;
            Key1 = tDESalg.Key;
            Key2 = tDESalg.IV;


        }
        catch (Exception)
        {

            throw;
        }


    }

    public string DecryptIt(byte[] sEncData)
    {

        //byte[] toEncrypt = new ASCIIEncoding().GetBytes(sEncData);

        //XElement xParser = null;
        //XmlDocument xDoc = new XmlDocument();

        try
        {
            //string Final = "";
            string sPwd = null;
            string sKey1 = null;
            string sKey2 = null;
            //System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            string soutxml = "";

            //soutxml = encoding.GetString(sEncData);
            soutxml = ASCIIEncoding.ASCII.GetString(sEncData);
            sPwd = soutxml.Substring(18, soutxml.LastIndexOf("</EncPwd>") - 18);
            sKey1 = soutxml.Substring(18 + sPwd.Length + 15, soutxml.LastIndexOf("</Key1>") - (18 + sPwd.Length + 15));
            sKey2 = soutxml.Substring(18 + sPwd.Length + 15 + sKey1.Length + 13, soutxml.LastIndexOf("</Key2>") - (18 + sPwd.Length + 15 + sKey1.Length + 13));



            //xDoc.LoadXml(soutxml);

           //xParser = XElement.Parse(soutxml);

        //IEnumerable<XElement> elemsValidations =
        //      from el in xParser.Elements("EmailPwd")
        //     select el;


            #region OldCode
            //XmlNodeList objXmlNode = xDoc.SelectNodes("EmailPwd");

            //foreach (XmlNode xmllist in objXmlNode)
            //{
            //    XmlNode xmlsubnode;
            //    xmlsubnode = xmllist.SelectSingleNode("EncPwd");
            //    xmlsubnode = xmllist.SelectSingleNode("Key1");
            //    xmlsubnode = xmllist.SelectSingleNode("Key2");
            //}

            #endregion

        //foreach (XElement elemValidation in elemsValidations)
        //{
        //    sPwd = elemValidation.Element("EncPwd").Value;
        //    sKey1 = elemValidation.Element("Key1").Value;
        //    sKey2 = elemValidation.Element("Key2").Value;
        //}

            //byte[] Key1 = encoding.GetBytes(sKey1);
            //byte[] Key2 = encoding.GetBytes(sKey2);
            //byte[] Data = encoding.GetBytes(sPwd);


            byte[] Key1 = ASCIIEncoding.ASCII.GetBytes(sKey1);
            byte[] Key2 = ASCIIEncoding.ASCII.GetBytes(sKey2);
            byte[] Data = ASCIIEncoding.ASCII.GetBytes(sPwd);

            // Decrypt the buffer back to a string.
            string Final = DecryptTextFromMemory(Data, Key1, Key2);

            return Final;
        }
        catch (Exception)
        {

            throw;
        }

    }

    public static byte[] EncryptTextToMemory(string Data,byte[] Key,byte[] IV)
    {
        try
        {
            // Create a MemoryStream.
            MemoryStream mStream = new MemoryStream();

            // Create a CryptoStream using the MemoryStream 
            // and the passed key and initialization vector (IV).
            CryptoStream cStream = new CryptoStream(mStream,
                new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
                CryptoStreamMode.Write);

            // Convert the passed string to a byte array.
            //byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);
            byte[] toEncrypt = ASCIIEncoding.ASCII.GetBytes(Data);
            // Write the byte array to the crypto stream and flush it.
            cStream.Write(toEncrypt, 0, toEncrypt.Length);
            cStream.FlushFinalBlock();

            // Get an array of bytes from the 
            // MemoryStream that holds the 
            // encrypted data.
            byte[] ret = mStream.ToArray();

            // Close the streams.
            cStream.Close();
            mStream.Close();

            // Return the encrypted buffer.
            return ret;
        }
        catch (CryptographicException e)
        {
            MessageBox.Show("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
    }

    public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
    {
        try
        {
            // Create a new MemoryStream using the passed 
            // array of encrypted data. 
            MemoryStream msDecrypt = new MemoryStream(Data);

            // Create a CryptoStream using the MemoryStream 
            // and the passed key and initialization vector (IV). 
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
            new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
            CryptoStreamMode.Write);

            csDecrypt.Write(Data, 0, Data.Length);
            //csDecrypt.FlushFinalBlock();
            msDecrypt.Position = 0;

            // Create buffer to hold the decrypted data. 
            byte[] fromEncrypt = new byte[msDecrypt.Length];

            // Read the decrypted data out of the crypto stream 
            // and place it into the temporary buffer. 
            msDecrypt.Read(fromEncrypt, 0, msDecrypt.ToArray().Length);
            //csDecrypt.Close();
            MessageBox.Show(ASCIIEncoding.ASCII.GetString(fromEncrypt));
            //Convert the buffer into a string and return it. 
            return new ASCIIEncoding().GetString(fromEncrypt);

        }
        catch (CryptographicException e)
        {
            MessageBox.Show("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:09:02+00:00Added an answer on May 14, 2026 at 6:09 pm

    The same key (Key) and initialization vector (IV) used to encrypt the file must be used to decrypt it. I can’t fully check this code currently so there may be one or two small problems but give it a try and let me know if it works, hopefully you get the idea:

    public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
    {
        try
        {
            // Create a new MemoryStream using the passed
            // array of encrypted data.
            MemoryStream msDecrypt = new MemoryStream();
    
            // Create a CryptoStream using the MemoryStream
            // and the passed key and initialization vector (IV).
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
            new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
            CryptoStreamMode.Write);
    
            csDecrypt.Write(Data, 0, Data.Length);
            csDecrypt.FlushFinalBlock();
            msDecrypt.Position = 0;
    
            // Create buffer to hold the decrypted data.
            byte[] fromEncrypt = new byte[msDecrypt.Length];
    
            // Read the decrypted data out of the crypto stream
            // and place it into the temporary buffer.
            msDecrypt.Read(fromEncrypt, 0, msDecrypt.ToArray().Length);
            csDecrypt.Close();
    
            //Convert the buffer into a string and return it.
            return new UTF8Encoding().GetString(fromEncrypt);
        }
        catch (CryptographicException e)
        {
            MessageBox.Show("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker
public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String
public class Main { public static void main(String []ar) { A m = new
public class Test{ public void Test(String name){}; public static void main() { Test t=new
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public class MyEncrypt { public void saveToFile(String fileName, BigInteger mod, BigInteger exp) throws IOException

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.