Simple Question about the Buffers in cryptPopData

View: New views
4 Messages — Rating Filter:   Alert me  

Simple Question about the Buffers in cryptPopData

by BenjaminF :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey Guys,

I got a real dumn question but I can't figure out how it works and the manual is not really helping at that point. I wrote a simple example to see how the system works:

                string message = "hallo Welt!";
                int bytescopied;
                crypt.Init();
                int cryptEnvelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_CRYPTLIB);
                crypt.SetAttribute(cryptEnvelope, crypt.ENVINFO_DATASIZE, message.Length);
                bytescopied = crypt.PushData(cryptEnvelope, message);
                crypt.FlushData(cryptEnvelope);
                bytescopied = crypt.PopData(cryptEnvelope, envelopedData, message.Length);
               
                crypt.DestroyEnvelope(cryptEnvelope);
                Console.WriteLine(envelopedData);
                Console.WriteLine("fertig");
                crypt.End();

The thing is that I have no clue how to initialize the envelopedData. If I initialize it with
 byte[] envelopedData = ... then I have to set all dimensions and that stuff. Do I really have to do this? Also how can I get from there my String back? I was looking for Stringbuffers but found nothing.

Could you help me with this? :)

Thanks!

Benjamin

Re: Simple Question about the Buffers in cryptPopData

by BenjaminF :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I forgot to mention that I develope with C#.


BenjaminF wrote:
Hey Guys,

I got a real dumn question but I can't figure out how it works and the manual is not really helping at that point. I wrote a simple example to see how the system works:

                string message = "hallo Welt!";
                int bytescopied;
                crypt.Init();
                int cryptEnvelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_CRYPTLIB);
                crypt.SetAttribute(cryptEnvelope, crypt.ENVINFO_DATASIZE, message.Length);
                bytescopied = crypt.PushData(cryptEnvelope, message);
                crypt.FlushData(cryptEnvelope);
                bytescopied = crypt.PopData(cryptEnvelope, envelopedData, message.Length);
               
                crypt.DestroyEnvelope(cryptEnvelope);
                Console.WriteLine(envelopedData);
                Console.WriteLine("fertig");
                crypt.End();

The thing is that I have no clue how to initialize the envelopedData. If I initialize it with
 byte[] envelopedData = ... then I have to set all dimensions and that stuff. Do I really have to do this? Also how can I get from there my String back? I was looking for Stringbuffers but found nothing.

Could you help me with this? :)

Thanks!

Benjamin

Re: Simple Question about the Buffers in cryptPopData

by BenjaminF :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

ok, I think I figured out now that I need two envelopes. One for encryption and one for decryption. But my decryption still doesn't work. I wrote:

       
                string message = "hallo Welt!";
                string result ="";


                byte[] mes = new byte[message.Length];
                try
                {

                    mes = encrypt(message);
                }
                catch (Exception e) {
                    Console.WriteLine("######### ENCRYPT: " + e.Message + "    ############");
                      }

                try
                {
                    result = decrypt(mes);
                }
                catch (Exception e) {
               
                Console.WriteLine("######### DECRYPT: " + e.Message + "    ############");
                }

                Console.WriteLine(result);



               
           
            }
            catch (Exception e)
            {
              Console.WriteLine("test");
              Console.WriteLine(e.Message);
            }
           
        }


        private static byte[] encrypt(String message) {
            int bytescopied;
            byte[] sd = new Byte[message.Length];
           
            crypt.Init();
            int cryptEnvelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_CRYPTLIB);
            crypt.SetAttribute(cryptEnvelope, crypt.ENVINFO_DATASIZE, message.Length);
            bytescopied = crypt.PushData(cryptEnvelope, message);
            crypt.FlushData(cryptEnvelope);
            char[] env = new char[bytescopied];
            bytescopied = crypt.PopData(cryptEnvelope, sd, message.Length);
           
            crypt.DestroyEnvelope(cryptEnvelope);
            crypt.End();
           
            return sd;
       
        }

        private static String decrypt(byte[] decmessage)
        {
            int bytescopied;
            byte[] x2 = new Byte[decmessage.Length];
            crypt.Init();
            int envelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_AUTO);
            //crypt.SetAttribute(envelope, crypt.ENVINFO_DATASIZE, decmessage.Length + 1);
            bytescopied = crypt.PushData(envelope, decmessage,0,decmessage.Length);
            crypt.FlushData(envelope);
            bytescopied = crypt.PopData(envelope, x2,decmessage.Length,bytescopied);


            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            Console.WriteLine(Convert.ToBase64String(x2));

            Console.WriteLine(enc.GetString(x2));
            Console.WriteLine("fertig");
            crypt.End();
            return enc.GetString(x2);
        }


If I run this it always says in the Exception that it is not enough data available. How can that be? What do I do wrong?

Best Regards,

Benjamin



I forgot to mention that I develope with C#.


BenjaminF wrote:
Hey Guys,

I got a real dumn question but I can't figure out how it works and the manual is not really helping at that point. I wrote a simple example to see how the system works:

                string message = "hallo Welt!";
                int bytescopied;
                crypt.Init();
                int cryptEnvelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_CRYPTLIB);
                crypt.SetAttribute(cryptEnvelope, crypt.ENVINFO_DATASIZE, message.Length);
                bytescopied = crypt.PushData(cryptEnvelope, message);
                crypt.FlushData(cryptEnvelope);
                bytescopied = crypt.PopData(cryptEnvelope, envelopedData, message.Length);
               
                crypt.DestroyEnvelope(cryptEnvelope);
                Console.WriteLine(envelopedData);
                Console.WriteLine("fertig");
                crypt.End();

The thing is that I have no clue how to initialize the envelopedData. If I initialize it with
 byte[] envelopedData = ... then I have to set all dimensions and that stuff. Do I really have to do this? Also how can I get from there my String back? I was looking for Stringbuffers but found nothing.

Could you help me with this? :)

Thanks!

Benjamin


Re: Simple Question about the Buffers in cryptPopData

by BenjaminF :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

ok, figured it out. For those who are interested:


        private static String decrypt(byte[] decmessage)
        {
            int bytescopied;
            string label = "Private_Key";
           
            int cryptContext = crypt.CreateContext(crypt.UNUSED, crypt.ALGO_AES);
            byte[] messagebuffer = new Byte[decmessage.Length];
           
            int envelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_AUTO);
            cryptContext = crypt.CreateContext(crypt.UNUSED, crypt.ALGO_AES);
            bytescopied = crypt.PushData(envelope, decmessage);
            crypt.SetAttributeString(cryptContext, crypt.CTXINFO_LABEL, label);
            crypt.DestroyContext(cryptContext);
            crypt.FlushData(envelope);
            bytescopied = crypt.PopData(envelope, messagebuffer, messagebuffer.Length);


            Console.WriteLine(ByteArrayToString(messagebuffer));    
           
           
            crypt.DestroyEnvelope(envelope);
                   
            return System.Text.Encoding.UTF8.GetString(messagebuffer);
        }







ok, I think I figured out now that I need two envelopes. One for encryption and one for decryption. But my decryption still doesn't work. I wrote:

       
                string message = "hallo Welt!";
                string result ="";


                byte[] mes = new byte[message.Length];
                try
                {

                    mes = encrypt(message);
                }
                catch (Exception e) {
                    Console.WriteLine("######### ENCRYPT: " + e.Message + "    ############");
                      }

                try
                {
                    result = decrypt(mes);
                }
                catch (Exception e) {
               
                Console.WriteLine("######### DECRYPT: " + e.Message + "    ############");
                }

                Console.WriteLine(result);



               
           
            }
            catch (Exception e)
            {
              Console.WriteLine("test");
              Console.WriteLine(e.Message);
            }
           
        }


        private static byte[] encrypt(String message) {
            int bytescopied;
            byte[] sd = new Byte[message.Length];
           
            crypt.Init();
            int cryptEnvelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_CRYPTLIB);
            crypt.SetAttribute(cryptEnvelope, crypt.ENVINFO_DATASIZE, message.Length);
            bytescopied = crypt.PushData(cryptEnvelope, message);
            crypt.FlushData(cryptEnvelope);
            char[] env = new char[bytescopied];
            bytescopied = crypt.PopData(cryptEnvelope, sd, message.Length);
           
            crypt.DestroyEnvelope(cryptEnvelope);
            crypt.End();
           
            return sd;
       
        }

        private static String decrypt(byte[] decmessage)
        {
            int bytescopied;
            byte[] x2 = new Byte[decmessage.Length];
            crypt.Init();
            int envelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_AUTO);
            //crypt.SetAttribute(envelope, crypt.ENVINFO_DATASIZE, decmessage.Length + 1);
            bytescopied = crypt.PushData(envelope, decmessage,0,decmessage.Length);
            crypt.FlushData(envelope);
            bytescopied = crypt.PopData(envelope, x2,decmessage.Length,bytescopied);


            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            Console.WriteLine(Convert.ToBase64String(x2));

            Console.WriteLine(enc.GetString(x2));
            Console.WriteLine("fertig");
            crypt.End();
            return enc.GetString(x2);
        }


If I run this it always says in the Exception that it is not enough data available. How can that be? What do I do wrong?

Best Regards,

Benjamin


BenjaminF wrote:
I forgot to mention that I develope with C#.


BenjaminF wrote:
Hey Guys,

I got a real dumn question but I can't figure out how it works and the manual is not really helping at that point. I wrote a simple example to see how the system works:

                string message = "hallo Welt!";
                int bytescopied;
                crypt.Init();
                int cryptEnvelope = crypt.CreateEnvelope(crypt.UNUSED, crypt.FORMAT_CRYPTLIB);
                crypt.SetAttribute(cryptEnvelope, crypt.ENVINFO_DATASIZE, message.Length);
                bytescopied = crypt.PushData(cryptEnvelope, message);
                crypt.FlushData(cryptEnvelope);
                bytescopied = crypt.PopData(cryptEnvelope, envelopedData, message.Length);
               
                crypt.DestroyEnvelope(cryptEnvelope);
                Console.WriteLine(envelopedData);
                Console.WriteLine("fertig");
                crypt.End();

The thing is that I have no clue how to initialize the envelopedData. If I initialize it with
 byte[] envelopedData = ... then I have to set all dimensions and that stuff. Do I really have to do this? Also how can I get from there my String back? I was looking for Stringbuffers but found nothing.

Could you help me with this? :)

Thanks!

Benjamin