Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Encrypt bitmap image for C# Windows application [modified]

Encrypt bitmap image for C# Windows application [modified]

Scheduled Pinned Locked Moved C#
helpcsharpdatabasegraphics
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nudma
    wrote on last edited by
    #1

    Hi All!! In code below, i'm encrypting bitmap(24-bit) image, accessed from ms-access database. Here is my code:

    private void button1_Click(object sender, System.EventArgs e)
    {
    string connString = "provider=Microsoft.Jet.OLEDB.4.0;" + @"data
    source=D:\\practiceproject\practice.mdb";
    OleDbConnection connection = new OleDbConnection(connString);
    string commandString = "Select Image from readImage where ID=1";
    OleDbCommand cmd = new OleDbCommand(commandString,connection);
    byte[] toEncrypt = new byte[120154];
    connection.Open();
    toEncrypt = (Byte[]) cmd.ExecuteScalar();
    connection.Close();

    		RSAParameters rsaPubParams;
    		RSAParameters rsaPrivateParams;
    		RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
            
    		//Generate public and private key data.
    		rsaPrivateParams = rsaCSP.ExportParameters(true);
    		rsaPubParams = rsaCSP.ExportParameters(false);
    		rsaCSP.ImportParameters(rsaPubParams);
    
    		byte\[\] encrypted = new byte\[130000\];
    		encrypted =  rsaCSP.Encrypt(toEncrypt, false);
    
    		//MemoryStream ms = new MemoryStream();
    		//ms.Write(encrypted, 78, encrypted.Length - 78);	
    	}
    

    But i get

    Bad Length

    error at line

    encrypted = rsaCSP.Encrypt(toEncrypt, false);

    Please help me to resolve this problem!!!

    modified on Thursday, March 19, 2009 11:35 AM

    M H 2 Replies Last reply
    0
    • N nudma

      Hi All!! In code below, i'm encrypting bitmap(24-bit) image, accessed from ms-access database. Here is my code:

      private void button1_Click(object sender, System.EventArgs e)
      {
      string connString = "provider=Microsoft.Jet.OLEDB.4.0;" + @"data
      source=D:\\practiceproject\practice.mdb";
      OleDbConnection connection = new OleDbConnection(connString);
      string commandString = "Select Image from readImage where ID=1";
      OleDbCommand cmd = new OleDbCommand(commandString,connection);
      byte[] toEncrypt = new byte[120154];
      connection.Open();
      toEncrypt = (Byte[]) cmd.ExecuteScalar();
      connection.Close();

      		RSAParameters rsaPubParams;
      		RSAParameters rsaPrivateParams;
      		RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
              
      		//Generate public and private key data.
      		rsaPrivateParams = rsaCSP.ExportParameters(true);
      		rsaPubParams = rsaCSP.ExportParameters(false);
      		rsaCSP.ImportParameters(rsaPubParams);
      
      		byte\[\] encrypted = new byte\[130000\];
      		encrypted =  rsaCSP.Encrypt(toEncrypt, false);
      
      		//MemoryStream ms = new MemoryStream();
      		//ms.Write(encrypted, 78, encrypted.Length - 78);	
      	}
      

      But i get

      Bad Length

      error at line

      encrypted = rsaCSP.Encrypt(toEncrypt, false);

      Please help me to resolve this problem!!!

      modified on Thursday, March 19, 2009 11:35 AM

      M Offline
      M Offline
      Michael Bookatz
      wrote on last edited by
      #2

      the way you've posted your code makes it very hard to read... please unbold and use a code block then people will look at it more and not jump it.

      1 Reply Last reply
      0
      • N nudma

        Hi All!! In code below, i'm encrypting bitmap(24-bit) image, accessed from ms-access database. Here is my code:

        private void button1_Click(object sender, System.EventArgs e)
        {
        string connString = "provider=Microsoft.Jet.OLEDB.4.0;" + @"data
        source=D:\\practiceproject\practice.mdb";
        OleDbConnection connection = new OleDbConnection(connString);
        string commandString = "Select Image from readImage where ID=1";
        OleDbCommand cmd = new OleDbCommand(commandString,connection);
        byte[] toEncrypt = new byte[120154];
        connection.Open();
        toEncrypt = (Byte[]) cmd.ExecuteScalar();
        connection.Close();

        		RSAParameters rsaPubParams;
        		RSAParameters rsaPrivateParams;
        		RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
                
        		//Generate public and private key data.
        		rsaPrivateParams = rsaCSP.ExportParameters(true);
        		rsaPubParams = rsaCSP.ExportParameters(false);
        		rsaCSP.ImportParameters(rsaPubParams);
        
        		byte\[\] encrypted = new byte\[130000\];
        		encrypted =  rsaCSP.Encrypt(toEncrypt, false);
        
        		//MemoryStream ms = new MemoryStream();
        		//ms.Write(encrypted, 78, encrypted.Length - 78);	
        	}
        

        But i get

        Bad Length

        error at line

        encrypted = rsaCSP.Encrypt(toEncrypt, false);

        Please help me to resolve this problem!!!

        modified on Thursday, March 19, 2009 11:35 AM

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #3

        I have not done anything with encryption for a long time. So this is not guaranteed to work. I think your problem is in the declaration of encrypted. Instead of:

            byte\[\] encrypted = new byte\[130000\];
        encrypted =  rsaCSP.Encrypt(toEncrypt, false);
        

        try:

            byte\[\] encrypted = rsaCSP.Encrypt(toEncrypt, false);
        

        After all you do not know in advance the length of the byte-array thet encrypt will return, so why set a length in the declaration. Good luck!

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        N 1 Reply Last reply
        0
        • H Henry Minute

          I have not done anything with encryption for a long time. So this is not guaranteed to work. I think your problem is in the declaration of encrypted. Instead of:

              byte\[\] encrypted = new byte\[130000\];
          encrypted =  rsaCSP.Encrypt(toEncrypt, false);
          

          try:

              byte\[\] encrypted = rsaCSP.Encrypt(toEncrypt, false);
          

          After all you do not know in advance the length of the byte-array thet encrypt will return, so why set a length in the declaration. Good luck!

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          N Offline
          N Offline
          nudma
          wrote on last edited by
          #4

          I tried that but again

          Bad Length

          error is at:

          byte[] encrypted = rsaCSP.Encrypt(toEncrypt, false);

          Please help me out... :(

          H 1 Reply Last reply
          0
          • N nudma

            I tried that but again

            Bad Length

            error is at:

            byte[] encrypted = rsaCSP.Encrypt(toEncrypt, false);

            Please help me out... :(

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            OK. After a little research. I think the problem might lie in the length of the input. From a little reading it seems that RSACryptoServiceProvider was designed for encrypting cryptographic keys, it gets a bit upset if you ask it to do too much work. A bit like me. There is quite a bit on the web about this problem, and I would suggest that you do a little more research. I Googled "RSACryptoServiceProvider Bad Length", and if you do the same, you might find a solution, but I doubt it. Choose another CryptoServiceProvider, if you are allowed to. I think that will make your life a whole lot easier.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            N 1 Reply Last reply
            0
            • H Henry Minute

              OK. After a little research. I think the problem might lie in the length of the input. From a little reading it seems that RSACryptoServiceProvider was designed for encrypting cryptographic keys, it gets a bit upset if you ask it to do too much work. A bit like me. There is quite a bit on the web about this problem, and I would suggest that you do a little more research. I Googled "RSACryptoServiceProvider Bad Length", and if you do the same, you might find a solution, but I doubt it. Choose another CryptoServiceProvider, if you are allowed to. I think that will make your life a whole lot easier.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              N Offline
              N Offline
              nudma
              wrote on last edited by
              #6

              Hi Henry!! Thanks for your help...I searched that too...None of the article contains image encryption through RSACryptoserviceprovider. other CryptoServiceProviders are used...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups