Binary encryption

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TomB

    Binary encryption

    Anyone know of an example/tutorial for encrypting a binary file?

    I'm able to encrypt/decrypt simple text files, but anything more complicated
    craps out.

    Thanks
    TomB


  • James

    #2
    Re: Binary encryption

    I think there is a class for this in the .Net frame work. I haven't used it
    but look in MSDN.


    "TomB" <shuckle@hotmai lXXX.com> wrote in message
    news:%233Joj4Bn EHA.952@TK2MSFT NGP10.phx.gbl.. .[color=blue]
    > Anyone know of an example/tutorial for encrypting a binary file?
    >
    > I'm able to encrypt/decrypt simple text files, but anything more[/color]
    complicated[color=blue]
    > craps out.
    >
    > Thanks
    > TomB
    >
    >[/color]


    Comment

    • Daniel O'Connell [C# MVP]

      #3
      Re: Binary encryption


      "TomB" <shuckle@hotmai lXXX.com> wrote in message
      news:%233Joj4Bn EHA.952@TK2MSFT NGP10.phx.gbl.. .[color=blue]
      > Anyone know of an example/tutorial for encrypting a binary file?
      >
      > I'm able to encrypt/decrypt simple text files, but anything more
      > complicated
      > craps out.
      >[/color]

      It shouldn't matter what the contents are. What encryption method are you
      using? Can you post a short, but complete program that shows what your
      problem is?


      Comment

      • TomB

        #4
        Re: Binary encryption

        Encrypt and Decrypt are my button names.



        RijndaelEnhance d is from



        Thanks

        TomB



        private void Encrypt_Click(o bject sender, System.EventArg s e)

        {

        Cursor.Current= Cursors.WaitCur sor;

        Encrypt.Text="E ncrypting...";

        Encrypt.Refresh ();

        string IV;



        IV=(Password.Te xt+"01234567890 123456").Substr ing(0,16);


        RijndaelEnhance d re=new RijndaelEnhance d(Password.Text ,IV);


        StreamReader _sr=new StreamReader(Fi leName.Text);

        byte[] byteArray=re.En cryptToBytes(_s r.ReadToEnd());

        string encryptedText=C onvert.ToBase64 String(byteArra y);

        //string encryptedText=r e.Encrypt(_sr.R eadToEnd());

        StreamWriter _sw;

        try

        {

        _sw=new
        StreamWriter(En cryptedFileLoca tion.Text,false ,System.Text.En coding.UTF8);


        _sw.Write( encryptedText);

        //_sw.Write(Conve rt.ToBase64Stri ng(byteArray));

        _sw.Flush();

        _sw.Close();

        MessageBox.Show ("Encrypted File saved as " +
        EncryptedFileLo cation.Text,"Fi le
        Encrypted",Mess ageBoxButtons.O K,MessageBoxIco n.Information);

        }

        catch (Exception ex)

        {

        MessageBox.Show (ex.Message);

        }

        finally

        {

        Encrypt.Text="& Encrypt";

        Cursor.Current= Cursors.Default ;

        }

        }

        private void Decrypt_Click(o bject sender, System.EventArg s e)

        {


        try

        {

        Decrypt.Text="D ecrypting...";

        Cursor.Current= Cursors.WaitCur sor;

        string IV;

        IV=(Password.Te xt+"01234567890 123456").Substr ing(0,16);


        RijndaelEnhance d re=new RijndaelEnhance d(Password.Text ,IV);


        StreamReader _sr=new
        StreamReader(En cryptedFileLoca tion.Text,Syste m.Text.Encoding .UTF8);

        string encryptedText=_ sr.ReadToEnd();

        string DecryptedText=r e.Decrypt(encry ptedText);

        StreamWriter _sw=new
        StreamWriter(De cryptedFileLoca tion.Text,false ,System.Text.En coding.UTF8);

        _sw.Write(Conve rt.FromBase64St ring(DecryptedT ext));

        //_sw.Write(Decry ptedText);

        _sw.Flush();

        _sw.Close();

        MessageBox.Show ("Decrypted File saved as '" + DecryptedFileLo cation.Text +
        "'.","File Decrypted",Mess ageBoxButtons.O K,MessageBoxIco n.Information);

        }

        catch (Exception ex)

        {

        // I imagine an exception would occur if the password is incorrect

        //MessageBox.Show (ex.Message);

        MessageBox.Show ("The file '" + EncryptedFileLo cation.Text + "' could not be
        decrypted. Please ensure you entered the correct password and filename",

        "Unable to decrypt file",

        MessageBoxButto ns.OK,

        MessageBoxIcon. Warning );

        }

        finally

        {

        Decrypt.Text="D ecrypt";

        Cursor.Current= Cursors.Default ;

        }

        }



        "Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
        message news:%23GxTZMCn EHA.596@TK2MSFT NGP11.phx.gbl.. .[color=blue]
        >
        > "TomB" <shuckle@hotmai lXXX.com> wrote in message
        > news:%233Joj4Bn EHA.952@TK2MSFT NGP10.phx.gbl.. .[color=green]
        > > Anyone know of an example/tutorial for encrypting a binary file?
        > >
        > > I'm able to encrypt/decrypt simple text files, but anything more
        > > complicated
        > > craps out.
        > >[/color]
        >
        > It shouldn't matter what the contents are. What encryption method are you
        > using? Can you post a short, but complete program that shows what your
        > problem is?
        >
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Binary encryption

          TomB <shuckle@hotmai lXXX.com> wrote:[color=blue]
          > Encrypt and Decrypt are my button names.[/color]

          <snip>

          The problem is that you're treating the binary files as if they're text
          files. Don't use a StreamReader - just read straight from the stream.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • TomB

            #6
            Re: Binary encryption

            Ah. OK.

            Thanks Jon, I'll give that a shot.

            Thanks for your help.

            TomB

            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1bb410 b9a822c85098b43 e@msnews.micros oft.com...[color=blue]
            > TomB <shuckle@hotmai lXXX.com> wrote:[color=green]
            > > Encrypt and Decrypt are my button names.[/color]
            >
            > <snip>
            >
            > The problem is that you're treating the binary files as if they're text
            > files. Don't use a StreamReader - just read straight from the stream.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            Working...