Results 1 to 4 of 4
  1. #1
    Ryan28's Avatar
    Join Date
    May 2014
    Gender
    male
    Posts
    67
    Reputation
    10
    Thanks
    11
    My Mood
    Cool

    Encryption and Decryption, error

    Hi,
    I am just a started of beginner with writing in C#, so sorry if sounds stupid,

    I want to write a Encrypt and Decrypt, program, (simple it will encrypt a string of text and write in a file, and if i want to decrypt the file later on)

    with the encryption, i have no problem, i can write it in a file.
    but when i try to read it and decrypt it, it will gave me an error (just in time error debugging)

    Here is the code for the class that i used, (I wrote at first my own Rijendeal encryption but that doesn't work ether)
    Code :
    Code:
    using System;
    using System.Text;
    using System.Security.Cryptography;
    using System****;
    using System.Linq;
    
    namespace EncryptStringSample
    {
        public static class StringCipher
        {
            // This constant is used to determine the keysize of the encryption algorithm in bits.
            // We divide this by 8 within the code below to get the equivalent number of bytes.
            private const int Keysize = 256;
    
            // This constant determines the number of iterations for the password bytes generation function.
            private const int DerivationIterations = 1000;
    
            public static string Encrypt(string plainText, string passPhrase)
            {
                // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
                // so that the same Salt and IV values can be used when decrypting.  
                var saltStringBytes = Generate256BitsOfRandomEntropy();
                var ivStringBytes = Generate256BitsOfRandomEntropy();
                var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
                using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
                {
                    var keyBytes = password.GetBytes(Keysize / 8);
                    using (var symmetricKey = new RijndaelManaged())
                    {
                        symmetricKey.BlockSize = 256;
                        symmetricKey.Mode = CipherMode.CBC;
                        symmetricKey.Padding = PaddingMode.PKCS7;
                        using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
                        {
                            using (var memoryStream = new MemoryStream())
                            {
                                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                                {
                                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                                    cryptoStream.FlushFinalBlock();
                                    // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
                                    var cipherTextBytes = saltStringBytes;
                                    cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
                                    cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
                                    memoryStream.Close();
                                    cryptoStream.Close();
                                    return Convert.ToBase64String(cipherTextBytes);
                                }
                            }
                        }
                    }
                }
            }
    
            public static string Decrypt(string cipherText, string passPhrase)
            {
                // Get the complete stream of bytes that represent:
                // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
                var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
                // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
                var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
                // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
                var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
                // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
                var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
    
                using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
                {
                    var keyBytes = password.GetBytes(Keysize / 8);
                    using (var symmetricKey = new RijndaelManaged())
                    {
                        symmetricKey.BlockSize = 256;
                        symmetricKey.Mode = CipherMode.CBC;
                        symmetricKey.Padding = PaddingMode.PKCS7;
                        using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
                        {
                            using (var memoryStream = new MemoryStream(cipherTextBytes))
                            {
                                using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                                {
                                    var plainTextBytes = new byte[cipherTextBytes.Length];
                                    var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                                    memoryStream.Close();
                                    cryptoStream.Close();
                                    return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                                }
                            }
                        }
                    }
                }
            }
    
            private static byte[] Generate256BitsOfRandomEntropy()
            {
                var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
                using (var rngCsp = new RNGCryptoServiceProvider())
                {
                    // Fill the array with cryptographically secure random bytes.
                    rngCsp.GetBytes(randomBytes);
                }
                return randomBytes;
            }
        }
    }


    Here the Encryption Code,
    Code:
    Code:
                StreamWriter writer = new StreamWriter("C:/Users/Ryan/Documents/Password/data.txt", true);
    
                string Code = "2000";
    
                string App = textBox4.Text;
                string Username = textBox3.Text;
                string Password = textBox5.Text;
    
                string AppEncrypted = StringCipher.Encrypt(App, Code);
                string UsernameEncrypted = StringCipher.Encrypt(Username, Code);
                string PasswordEncrypted = StringCipher.Encrypt(Password, Code);
    
    
                writer.WriteLine(AppEncrypted);
                writer.WriteLine(UsernameEncrypted);
                writer.WriteLine(UsernameEncrypted);
                writer.WriteLine(" ");
                writer.Flush();
    
                writer.Close();
                MessageBox.Show("Saved succesfull");


    Code for Decryption
    Code:
    Code:
                // Read the file and display it line by line.
                StreamReader file = new System****.StreamReader(@"C:\Users\Ryan\Documents\Password\Data.txt");
                while (true)
                {
                    string Line = file.ReadLine();
                    if (Line == "" && Line == " ")
                    {
                        break;
                    }
                    else
                    {
                        string LineDecrypted = StringCipher.Decrypt(Line, "2000");
                        richTextBox1.AppendText(LineDecrypted);
                    }
                    
                }
    
                file.Close();
    if i used this code, i get an error,
    But if i only Decode 1 string there isn't an error,

    I hope one of you could help me out,

    attached, Visual Studio Full file,

    https://virustotal.com/en/file/0745a...is/1481229877/
    https://virusscan.jotti.org/en-US/fi...job/x8fa4p3sy5
    <b>Downloadable Files</b> Downloadable Files
    Last edited by Hugo Boss; 12-08-2016 at 01:46 PM. Reason: added vs

  2. The Following User Says Thank You to Ryan28 For This Useful Post:

    marvs2020 (07-01-2020)

  3. #2
    Hugo Boss's Avatar
    Join Date
    Oct 2011
    Gender
    male
    Posts
    28,752
    Reputation
    4790
    Thanks
    5,902
    My Mood
    Angelic
    //Approved your file and added virus scans to it.

     
    Super User since 08-29-2017
    Global Moderator from 10-02-2016 - 08-29-2017
    Premium Seller since 11-16-2016
    Moderator from 09-24-2015 - 01-09-2016
    Alliance of Valiant Arms Minion from 11-12-2015 - 01-09-2016
    Market place Minion from 09-24-2015 - 01-09-2016
    Crossfire Minion from 09-11-2015 - 01-09-2016

    Middleman from 07-07-2015 - 01-09-2016
    Market Place Minion from 03-03-2014 - 08-01-2014
    Middleman from 01-30-2014 - 08-01-2014
    Moderator from 03-29-2013 - 04-04-2013
    Market Place Minion from 03-07-2013 - 04-04-2013
    Premium Member since 01-25-2013
    Middleman from 12-04-2012 - 04-04-2013
    Registered since 10-9-2011

  4. #3
    NotCarlsson's Avatar
    Join Date
    Sep 2016
    Gender
    male
    Posts
    72
    Reputation
    10
    Thanks
    12
    That is the error message you get when you are trying to decrypt it?
    Not Carlsson.
    Steam

  5. #4
    Hugo Boss's Avatar
    Join Date
    Oct 2011
    Gender
    male
    Posts
    28,752
    Reputation
    4790
    Thanks
    5,902
    My Mood
    Angelic
    No response for more than a week.
    Marking it as unresolved.

     
    Super User since 08-29-2017
    Global Moderator from 10-02-2016 - 08-29-2017
    Premium Seller since 11-16-2016
    Moderator from 09-24-2015 - 01-09-2016
    Alliance of Valiant Arms Minion from 11-12-2015 - 01-09-2016
    Market place Minion from 09-24-2015 - 01-09-2016
    Crossfire Minion from 09-11-2015 - 01-09-2016

    Middleman from 07-07-2015 - 01-09-2016
    Market Place Minion from 03-03-2014 - 08-01-2014
    Middleman from 01-30-2014 - 08-01-2014
    Moderator from 03-29-2013 - 04-04-2013
    Market Place Minion from 03-07-2013 - 04-04-2013
    Premium Member since 01-25-2013
    Middleman from 12-04-2012 - 04-04-2013
    Registered since 10-9-2011

Similar Threads

  1. [WTS] Script Encrypting and Decrypting Service
    By ikillindreams in forum DayZ Selling / Trading / Buying
    Replies: 1
    Last Post: 02-01-2013, 11:18 PM
  2. [Source Code] Text Encrypter and Decrypter
    By rileyjstrickland in forum Visual Basic Programming
    Replies: 3
    Last Post: 11-09-2012, 08:32 PM
  3. [Help Request] Error Elite Encrypt.. and no hacks work.
    By 48Aces in forum Combat Arms Help
    Replies: 4
    Last Post: 09-08-2011, 10:23 AM
  4. [Help] Startup and Install Error
    By AirCuddles in forum WarRock Discussions
    Replies: 32
    Last Post: 11-04-2009, 09:40 AM
  5. So whatever happened to the glitches and hitbox errors?
    By wolves4life in forum Combat Arms Discussions
    Replies: 10
    Last Post: 11-04-2009, 08:00 AM

Tags for this Thread