public Message()
 {
     msg          = "";
     externalType = ExternalMsgTypes.Transfer;
     internalType = InternalMsgTypes.MessageTransfer;
 }
        public bool Decrypt(string MyPrivateKey, string SourcePublicKey)
        {
            //Decrypt Process

            try
            {
                //Message Signing
                RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(1024);
                rsaCryptoServiceProvider.FromXmlString(SourcePublicKey);
                SHA1Managed sha1         = new SHA1Managed();
                byte[]      data         = Convert.FromBase64String(msg);
                byte[]      signature    = new byte[128];
                byte[]      messagebytes = new byte[data.Length - 128];
                Array.Copy(data, 0, signature, 0, 128);
                Array.Copy(data, 128, messagebytes, 0, messagebytes.Length);

                byte[] hash = sha1.ComputeHash(messagebytes);
                if (!rsaCryptoServiceProvider.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), signature))
                {
                    //MessageBox.Show("Couldn't verify the message, please verify your certificate.");
                    return(false);
                }

                //House-Keeping
                int    keyLen     = BitConverter.ToInt32(messagebytes, 0);
                int    messageLen = BitConverter.ToInt32(messagebytes, 4);
                byte[] keybytes   = new byte[keyLen];
                byte[] msgbytes   = new byte[messageLen];

                Array.Copy(messagebytes, 8, keybytes, 0, keyLen);
                Array.Copy(messagebytes, 8 + keyLen, msgbytes, 0, messageLen);

                //Key Decryption
                RSACryptoServiceProvider rsaCryptoServiceProvider2 = new RSACryptoServiceProvider(1024);
                rsaCryptoServiceProvider2.FromXmlString(MyPrivateKey);
                int       base64BlockSize = ((1024 / 8) % 3 != 0) ? (((1024 / 8) / 3) * 4) + 4 : ((1024 / 8) / 3) * 4;
                string    TempStr         = Convert.ToBase64String(keybytes);
                int       iterations      = TempStr.Length / base64BlockSize;
                ArrayList arrayList       = new ArrayList();
                for (int i = 0; i < iterations; i++)
                {
                    byte[] encryptedBytes = Convert.FromBase64String(TempStr.Substring(base64BlockSize * i, base64BlockSize));
                    Array.Reverse(encryptedBytes);
                    arrayList.AddRange(rsaCryptoServiceProvider2.Decrypt(encryptedBytes, false));
                }
                string AES_KEY = Convert.ToBase64String(arrayList.ToArray(Type.GetType("System.Byte")) as byte[]);

                //Decrypt AES 128
                byte[] DecryptedKey = Convert.FromBase64String(AES_KEY);
                SharpPrivacy.Cipher.SymmetricAlgorithm SA = null;
                SA     = CipherHelper.CreateSymAlgorithm(SymAlgorithms.AES128);
                SA.Key = DecryptedKey;
                SA.IV  = SA.Key;

                SharpPrivacy.Cipher.ICryptoTransform Decryptor = SA.CreateDecryptor();
                byte[] Result = new byte[msgbytes.Length];
                Decryptor.TransformBlock(msgbytes, 0, msgbytes.Length, ref Result, 0);

                UnicodeEncoding UE = new UnicodeEncoding();
                msg = UnicodeEncoding.ASCII.GetString(Result);
                string Temp  = Convert.ToBase64String(Result);
                string temp2 = UnicodeEncoding.ASCII.GetString(Result);
                string temp3 = UnicodeEncoding.Unicode.GetString(Result);
                string temp4 = UnicodeEncoding.UTF32.GetString(Result);
                string temp5 = UnicodeEncoding.UTF8.GetString(Result);

                if (msg.Length > 0)
                {
                    internalType = (InternalMsgTypes)Convert.ToInt32(msg[0].ToString());
                    int source_Lenght = Convert.ToInt32(msg[1].ToString());
                    source_name = msg.Substring(2, source_Lenght);
                    msg         = msg.Substring(source_Lenght + 2, msg.Length - source_Lenght - 2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }
            return(true);
        }