예제 #1
0
        public bool DecryptData(string signed, string msg = "")
        {
            if (Init())
            {
                if (_type == 1)
                {
                    //SHA256 sha256Hash = SHA256.Create();
                    //byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(msg));

                    byte[] bsigned = StringToByteArray(signed);
                    byte[] bytes   = Encoding.UTF8.GetBytes(msg);
                    var    rsa     = RSA.Create();
                    rsa.KeySize = _keySize;
                    rsa.ImportSubjectPublicKeyInfo(Convert.FromBase64String(_publicKey), out int byteReads);
                    var result = rsa.VerifyData(bytes, bsigned, HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1);
                    return(result);
                }
                else
                {
                    PGPLib pgp    = new PGPLib();
                    byte[] bytes  = Encoding.UTF8.GetBytes(_publicKey);
                    var    stream = new MemoryStream(bytes);
                    SignatureCheckResult signatureCheck = pgp.VerifyString(signed, stream, out string plainText);
                    var result = signatureCheck == SignatureCheckResult.SignatureVerified;
                    return(result);
                }
            }
            else
            {
                return(false);
            }
        }
예제 #2
0
        public Boolean Decreypt(string encrypt, int ID)
        {
            //Get Public File
            GetKey  objKey     = new GetKey();
            DataRow drKey      = objKey.drSearchStudentKey(ID);
            string  PublicKey1 = drKey[0].ToString();

            // obtain an OpenPGP signed message
            String signedString = encrypt;

            // Extract the message and check the validity of the signature
            String plainText;

            // create an instance of the library
            PGPLib pgp = new PGPLib();
            SignatureCheckResult signatureCheck;

            try
            {
                signatureCheck = pgp.VerifyString(signedString,
                                                  new FileInfo(PublicKey1), out plainText);
            }
            catch
            {
                return(false);
            }



            string strData1 = plainText;

            // Print the results
            Console.WriteLine("Extracted plain text message is " + plainText);
            if (signatureCheck == SignatureCheckResult.SignatureVerified)
            {
                // Console.WriteLine("Signature OK");
                return(true);
            }
            else if (signatureCheck == SignatureCheckResult.SignatureBroken)
            {
                //Console.WriteLine("Signature of the message is either broken or forged");
                return(false);
            }
            else if (signatureCheck == SignatureCheckResult.PublicKeyNotMatching)
            {
                //   Console.WriteLine("The provided public key doesn't match the signature");
                return(false);
            }
            else if (signatureCheck == SignatureCheckResult.NoSignatureFound)
            {
                //  Console.WriteLine("This message is not digitally signed");
                return(false);
            }
            return(false);
        }
예제 #3
0
        public void Decreypt(string encrypt)
        {
            //Get Public File
            GetKey  objKey     = new GetKey();
            int     ID         = Convert.ToInt32(Session["ID"].ToString());
            DataRow drKey      = objKey.drSearchStudentKey(ID);
            string  PublicKey1 = drKey[0].ToString();



            // obtain an OpenPGP signed message
            String signedString = encrypt;

            // Extract the message and check the validity of the signature
            String plainText;

            // create an instance of the library
            PGPLib pgp = new PGPLib();

            //SignatureCheckResult signatureCheck = pgp.VerifyString(signedString,
            //                                        new FileInfo(@"C:\Users\Dua'a-Orcas\Desktop\finalProject\WebApplication1 - Copy (2)\WebApplication1\Sig\public_key_exported.asc"),
            //                                        out plainText);


            SignatureCheckResult signatureCheck = pgp.VerifyString(signedString,
                                                                   new FileInfo(PublicKey1), out plainText);


            string strData1 = plainText;

            // Print the results
            Console.WriteLine("Extracted plain text message is " + plainText);
            if (signatureCheck == SignatureCheckResult.SignatureVerified)
            {
                Console.WriteLine("Signature OK");
                Result = true;
            }
            else if (signatureCheck == SignatureCheckResult.SignatureBroken)
            {
                Console.WriteLine("Signature of the message is either broken or forged");
            }
            else if (signatureCheck == SignatureCheckResult.PublicKeyNotMatching)
            {
                Console.WriteLine("The provided public key doesn't match the signature");
            }
            else if (signatureCheck == SignatureCheckResult.NoSignatureFound)
            {
                Console.WriteLine("This message is not digitally signed");
            }
        }