Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
AnswerRe: Updating sql table where textbox = ID Pin
Wei Xue2-Apr-24 20:58
Wei Xue2-Apr-24 20:58 
QuestionCreating a shortcut (LNK) Pin
Richard Andrew x6416-Mar-24 3:38
professionalRichard Andrew x6416-Mar-24 3:38 
AnswerRe: Creating a shortcut (LNK) Pin
Dave Kreskowiak16-Mar-24 4:50
mveDave Kreskowiak16-Mar-24 4:50 
GeneralRe: Creating a shortcut (LNK) Pin
Richard Andrew x6416-Mar-24 7:23
professionalRichard Andrew x6416-Mar-24 7:23 
GeneralRe: Creating a shortcut (LNK) Pin
Dave Kreskowiak16-Mar-24 11:44
mveDave Kreskowiak16-Mar-24 11:44 
QuestionHow to read and update existing language MST file (1033.mst) which is part of MSI Package using C# application? Pin
krish8812-Mar-24 20:02
krish8812-Mar-24 20:02 
AnswerRe: How to read and update existing language MST file (1033.mst) which is part of MSI Package using C# application? Pin
OriginalGriff12-Mar-24 20:02
mveOriginalGriff12-Mar-24 20:02 
QuestionSystem.Security.Cryptography.Aes Class Pin
Kevin Marois12-Mar-24 13:40
professionalKevin Marois12-Mar-24 13:40 
I am trying to encrypt and decrypt some text using System.Security.Cryptography.Aes.[^].

Here's my Console app:
var password = "thisIsAReallllllllyLongPasswordForTesting";
var passPhrase = "E546C8DF278CD5931069B522E695D4F2";
var initVector = "HR$2pIjHR$2pIj12";
var encrypted = Cryptography.EncryptString(password, passPhrase, initVector);

Console.WriteLine("Encrypted password");
Console.WriteLine(encrypted.ToString());

string decrypted = Cryptography.DecryptString(encrypted, passPhrase, initVector);

Console.WriteLine("decrypted password");
Console.WriteLine(decrypted);

Here's my crypto class:
public static class Cryptography
{
    public static string EncryptString(string plainText, string passPhrase, string initVector)
    {
        string encrypted = "";

        // Create an Aes object with the specified key and IV.
        using (Aes aes = Aes.Create())
        {
            aes.Key = Encoding.ASCII.GetBytes(passPhrase);
            aes.IV = Encoding.ASCII.GetBytes(initVector);

            // Create a new MemoryStream object to contain the encrypted bytes.
            using (var memoryStream = new MemoryStream())
            {
                // Create a CryptoStream object to perform the encryption.
                using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    // Encrypt the plaintext.
                    using (var streamWriter = new StreamWriter(cryptoStream))
                    {
                        streamWriter.Write(plainText);
                    }

                    encrypted = Encoding.UTF8.GetString(memoryStream.ToArray());
                }
            }
        }

        return encrypted;
    }

    public static string DecryptString(string encryptedText, string passPhrase, string initVector)
    {
        string decrypted;

        // Convert the input string to bytes
        var inputBytes = Encoding.ASCII.GetBytes(encryptedText);

        //PadToMultipleOf(ref inputBytes, 16);

        // Create an Aes object with the specified key and IV.
        using (Aes aes = Aes.Create())
        {
            aes.Key = Encoding.ASCII.GetBytes(passPhrase);
            aes.IV = Encoding.ASCII.GetBytes(initVector);

            // Create a new MemoryStream object to contain the decrypted bytes.
            using (var memoryStream = new MemoryStream(inputBytes))
            {
                // Create a CryptoStream object to perform the decryption.
                using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    // Decrypt the ciphertext.
                    using (var streamReader = new StreamReader(cryptoStream))
                    {
                        decrypted = streamReader.ReadToEnd();
                    }
                }
            }
        }

        return decrypted;
    }

    private static void PadToMultipleOf(ref byte[] src, int pad)
    {
        int len = (src.Length + pad - 1) / pad * pad;
        Array.Resize(ref src, len);
    }
}

I don't get any errors when encrypting, but when I decrypt I get
The input data is not a complete block.'
I thought that maybe I need to pad the input array, but when I uncomment this:
PadToMultipleOf(ref inputBytes, 16);
then I get
Padding is invalid and cannot be removed.'
Anyone know what's wrong, or maybe can point me to a working example?

Thanks
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.

AnswerRe: System.Security.Cryptography.Aes Class Pin
Peter_in_278012-Mar-24 15:50
professionalPeter_in_278012-Mar-24 15:50 
GeneralRe: System.Security.Cryptography.Aes Class Pin
Kevin Marois13-Mar-24 7:44
professionalKevin Marois13-Mar-24 7:44 
AnswerRe: System.Security.Cryptography.Aes Class Pin
JudyL_MD13-Mar-24 2:05
JudyL_MD13-Mar-24 2:05 
GeneralRe: System.Security.Cryptography.Aes Class Pin
Kevin Marois13-Mar-24 7:44
professionalKevin Marois13-Mar-24 7:44 
GeneralRe: System.Security.Cryptography.Aes Class Pin
Kevin Marois13-Mar-24 8:16
professionalKevin Marois13-Mar-24 8:16 
SuggestionRe: System.Security.Cryptography.Aes Class Pin
Richard Deeming17-Mar-24 23:46
mveRichard Deeming17-Mar-24 23:46 
QuestionI want to display both Old and New Values in Form2. Pin
Sonika B S10-Mar-24 4:14
Sonika B S10-Mar-24 4:14 
AnswerRe: I want to display both Old and New Values in Form2. Pin
Dave Kreskowiak10-Mar-24 4:33
mveDave Kreskowiak10-Mar-24 4:33 
AnswerRe: I want to display both Old and New Values in Form2. Pin
OriginalGriff10-Mar-24 5:04
mveOriginalGriff10-Mar-24 5:04 
QuestionVideo Merging with Xabe.FFmpeg. How to track the progress? Pin
Kasun Lee8-Mar-24 20:13
Kasun Lee8-Mar-24 20:13 
AnswerRe: Video Merging with Xabe.FFmpeg. How to track the progress? Pin
OriginalGriff8-Mar-24 20:40
mveOriginalGriff8-Mar-24 20:40 
AnswerRe: Video Merging with Xabe.FFmpeg. How to track the progress? Pin
Andre Oosthuizen9-Mar-24 0:27
mveAndre Oosthuizen9-Mar-24 0:27 
QuestionVideo Merging using Splicer Lib Pin
Kasun Lee5-Mar-24 17:36
Kasun Lee5-Mar-24 17:36 
AnswerRe: Video Merging using Splicer Lib Pin
Dave Kreskowiak5-Mar-24 18:41
mveDave Kreskowiak5-Mar-24 18:41 
AnswerRe: Video Merging using Splicer Lib Pin
OriginalGriff5-Mar-24 20:49
mveOriginalGriff5-Mar-24 20:49 
GeneralRe: Video Merging using Splicer Lib Pin
Kasun Lee6-Mar-24 3:36
Kasun Lee6-Mar-24 3:36 
GeneralRe: Video Merging using Splicer Lib Pin
OriginalGriff6-Mar-24 4:37
mveOriginalGriff6-Mar-24 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.