Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code in program.cs:
 static void Main(string[] arg)
        {
            Boolean bCreatedNew;
            Mutex m = new Mutex(false, "schoolProject", out bCreatedNew);
            m.WaitOne();
            GC.Collect();
            if (!bCreatedNew) return;
            m.ReleaseMutex();
           
            System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
            Byte[] myKey = AES.generateKey();
            string result = System.Text.Encoding.UTF8.GetString(myKey);
            RSACryptoServiceProvider RSAObj = new RSACryptoServiceProvider();
            File.WriteAllText("rsa.txt", RSAObj.ToXmlString(true));
            File.WriteAllBytes("aes.txt", RSAObj.Encrypt(myKey, false));
            encryptAll(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), myKey);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form1 = new Form1();
            form1.RSAObj(result);
            Application.Run(form1);

            
            
        }
    }
}


And this is my code in form 1:
public void RSAObj(string value)
       {
           textBox1.Text = value;
       }


As you can see, this program makes two text documents, one with RSA and another with AES key which is encrypted by RSA, and it sends the AES key to a textbox in form 1. The problem is that I don't get the AES key in the textbox, but random rubbish, for example: "6˞Ea���X�)Xl�F ". Why? As you can see I have converted bytes to string to send the AES key to form 1 textbox:
Byte[] myKey = AES.generateKey();
string result = System.Text.Encoding.UTF8.GetString(myKey); 
I tried making the program send the RSA key to a textbox in form 1, and that worked by changing:
form1.RSAObj(result);
to
form1.RSAObj(RSAObj.ToXmlString(true));
How can I make it send the AES key though?

What I have tried:

Posted
Updated 27-Jan-19 4:39am
v15
Comments
Dave Kreskowiak 27-Jan-19 11:20am    
I'm almost afraid to ask, but what's with the Mutex and the call to GC.Collect?

Seriously, it looks like you're copying and pasting code from the web without a clue as to what it does or how it works.

1 solution

Yes, you have got the AES key. It is just that many of the bytes are not printable alphanumeric characters. You need to understand that a byte can have any value between 0 and 255, and only some of those values equate to printable characters. Google will find you the full set.

Given that this appears to be a school project you seem to have missed some basic information in your course.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900