Example #1
0
        public static string VerifySignedHash(string str_DataToVerify, string str_SignedData, string str_publicKeyFilePath)
        {
            byte[] SignedData = Convert.FromBase64String(str_SignedData);

            UTF8Encoding ByteConverter = new UTF8Encoding();
            byte[] DataToVerify = ByteConverter.GetBytes(str_DataToVerify);
            try
            {
                string sPublicKeyPEM = File.ReadAllText(str_publicKeyFilePath);
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

                rsa.PersistKeyInCsp = false;
                rsa.LoadPublicKeyPEM(sPublicKeyPEM);

                if (rsa.VerifyData(DataToVerify, "SHA256", SignedData))
                {
                    return "verify success";
                }
                else
                {
                    return "verify fail";
                }

            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);

                return "verify error";
            }
        }
        private RsaSecurityKey RSASecurityKey()
        {
            var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();

            rsa.LoadPublicKeyPEM(pubKey);
            return(new RsaSecurityKey(rsa));
        }
Example #3
0
        static int Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            oldpass = File.ReadAllText(@"oldPass.txt");
            newpass = NewPass(oldpass);

            WebRequest request;

            {
                string encrypted;

                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024))
                {
                    rsa.PersistKeyInCsp = false;
                    rsa.LoadPublicKeyPEM(File.ReadAllText(@"pubkey.pem", Encoding.UTF8));

                    encrypted = Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes(newpass), false));
                }

                request = WebRequest.Create(
                    // Zebra Crossing site was unavailable on October, 23rd
                    //@"http://zxing.org/w/chart?cht=qr&chs=350x350&chld=L&choe=UTF-8&chl=" +
                    @"http://api.qrserver.com/v1/create-qr-code/?data=" +
                    Uri.EscapeDataString(encrypted));
            }

            using (var qrcode = new MemoryStream())
            {
                using (var response = request.GetResponse())
                    response.GetResponseStream().CopyTo(qrcode);

                var uploadTo = File.ReadAllText(@"uploadTo.url", Encoding.UTF8);

                var match = new Regex(@"^\w://(?<Credentials>(?<User>[^:@/]+):(?<Pass>[^@/]+)@)").Match(uploadTo);
                if (match.Success)
                {
                    uploadTo = uploadTo.Remove(match.Groups[@"Credentials"].Index, match.Groups[@"Credentials"].Length);

                    request = WebRequest.Create(uploadTo);
                    request.Credentials = new NetworkCredential(match.Groups[@"User"].Value, match.Groups[@"Pass"].Value);
                }
                else
                    request = WebRequest.Create(uploadTo);

                try
                {
                    request.Timeout = 20000;
                }
                catch { }

                try
                {
                    ((FtpWebRequest)request).Method = WebRequestMethods.Ftp.UploadFile;
                    ((FtpWebRequest)request).UsePassive = true;
                }
                catch
                {
                    try
                    {
                        ((HttpWebRequest)request).Method = WebRequestMethods.Http.Put;
                    }
                    catch
                    {
                        try
                        {
                            ((FileWebRequest)request).Method = WebRequestMethods.File.UploadFile;
                        }
                        catch
                        {
                            throw new Exception(@"Unknown protocol in your URL, please check file: uploadTo.url");
                        }
                    }
                }

                try
                {
                    request.ContentLength = qrcode.Length;
                }
                catch { }

                try
                {
                    request.ContentType = @"image/png";
                }
                catch { }

                using (var stream = request.GetRequestStream())
                    qrcode.WriteTo(stream);

                request.GetResponse();
            }

            return 0;
        }
Example #4
0
        public static bool VerifyRSASignature(string data, string expectedSignature)
        {
            string sPublicKeyPEM = File.ReadAllText(System.Windows.Forms.Application.StartupPath + "\\T8Pub.pem");
            var dataToSign = Encoding.UTF8.GetBytes(data);
            var signature = Convert.FromBase64String(expectedSignature);

            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.PersistKeyInCsp = false;
                rsa.LoadPublicKeyPEM(sPublicKeyPEM);
                using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
                    return rsa.VerifyData(dataToSign, sha1, signature);
            }
        }