public static byte[] AsymmetricEncrypt(string publicKeyAsPem, byte[] payload)
        {
            CryptoKey d   = CryptoKey.FromPublicKey(publicKeyAsPem, null);
            RSA       rsa = d.GetRSA();

            byte[] result = rsa.PublicEncrypt(payload, RSA.Padding.PKCS1);
            rsa.Dispose();
            return(result);
        }
예제 #2
0
        static void WriteDll(string dllpath, string pempath)
        {
            //Load public key
            StreamReader sr       = new StreamReader(pempath);
            string       keyaspem = sr.ReadToEnd();

            sr.Close();

            CryptoKey d;

            //Is private key
            if (keyaspem.IndexOf("-----BEGIN RSA PRIVATE KEY-----") == 0)
            {
                d = CryptoKey.FromPrivateKey(keyaspem, null);
            }
            //Is public key
            else if (keyaspem.IndexOf("-----BEGIN PUBLIC KEY-----") == 0)
            {
                d = CryptoKey.FromPublicKey(keyaspem, null);
            }
            else
            {
                Console.WriteLine("Key is not vaild.");
                return;
            }

            //Init openssl rsa component.
            RSA r = d.GetRSA();

            Console.WriteLine("Key File:");
            Console.WriteLine(pempath);
            Console.WriteLine();
            Console.WriteLine("Public Moludus: (reversed)");
            byte[] moludus = new byte[256];
            r.PublicModulus.ToBytes(moludus);
            Array.Reverse(moludus);
            Console.WriteLine(BitConverter.ToString(moludus));
            Console.WriteLine();
            Console.Write("Begin writing \"");
            Console.Write(dllpath);
            Console.Write("\" at ");
            Console.WriteLine("{0:x8}", SIGNATURE_OFFEST);  //hex output
            Console.WriteLine();

            //write game.dll
            FileStream fs = new FileStream(dllpath, FileMode.Open, FileAccess.ReadWrite);

            fs.Seek(SIGNATURE_OFFEST, SeekOrigin.Begin);
            fs.Write(moludus, 0, moludus.Length);
            fs.Close();
            Console.WriteLine("Writing successful");
            Console.WriteLine();
            Console.WriteLine("======================");
        }
예제 #3
0
    public static byte[] EncryptRSA(string publicKeyAsPem, string payload, string passphrase = null)
    {
        var encoder = new UTF8Encoding();

        byte[]    byte_payload = encoder.GetBytes(payload);
        CryptoKey d            = CryptoKey.FromPublicKey(publicKeyAsPem, passphrase);

        OpenSSL.Crypto.RSA rsa    = d.GetRSA();
        byte[]             result = rsa.PublicEncrypt(byte_payload, OpenSSL.Crypto.RSA.Padding.PKCS1);
        rsa.Dispose();
        return(result);
    }
예제 #4
0
        public bool Verify(string publicKeyAsPem, string data, string sign)
        {
            CryptoKey cryptoKey = CryptoKey.FromPublicKey(publicKeyAsPem, null);
            Key       ecKey     = cryptoKey.GetEC();

            var digest = Encoding.ASCII.GetBytes(data);

            var ha = System.Security.Cryptography.SHA256Managed.Create();

            var hash = ha.ComputeHash(digest);

            return(ecKey.Verify(0, hash, Convert.FromBase64String(sign)));
        }
예제 #5
0
 /// <summary>
 /// 公钥验签
 /// </summary>
 public static bool Verify(string publicKey, string text, string sign, Encoding encoding)
 {
     using (BIO bio = new BIO(publicKey))
     {
         using (CryptoKey cryptoKey = CryptoKey.FromPublicKey(bio, null))
         {
             using (MessageDigestContext sha256 = new MessageDigestContext(MessageDigest.SHA256))
             {
                 byte[] msgByte  = encoding.GetBytes(text);
                 byte[] signByte = Convert.FromBase64String(sign);
                 return(sha256.Verify(msgByte, signByte, cryptoKey));
             }
         }
     }
 }
예제 #6
0
        //=====================================================
        // Verify map signature data with public key
        //=====================================================
        public static byte[] VerifyData(byte[] BytesToVerify, string keypath)
        {
            //Load public key
            StreamReader sr          = new StreamReader(keypath);
            string       pubkeyaspem = sr.ReadToEnd();

            sr.Close();

            //Init openssl rsa component.
            CryptoKey d = CryptoKey.FromPublicKey(pubkeyaspem, null);
            RSA       r = d.GetRSA();

            byte[] result = r.PublicDecrypt(BytesToVerify, OpenSSL.Crypto.RSA.Padding.None);
            r.Dispose();
            return(result);
        }
예제 #7
0
        private License Decrypt(string payload)
        {
            CryptoKey cryptoKey = CryptoKey.FromPublicKey("-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqYO2D1DzVA3Q4JXtt0GK\nPKzrA1uu8LKU48f/IRI2vH+I81IwIPGQErDT4SNTjSyLIzYAcJnSUpLbUxj2OhFc\nrSRK/9e7PffJtus9h/ph8Tma5G4e2f7prNxSBTRqiUghB4ZCPJs2NzTA9Q4Rqqe+\n38/2ESGU1G/vOHW71XXKoQkLK8PbU8eRWlCybn5ZElsrCJOlZRdvcmoc/n7IyeBc\n2KJyl3BWhkRLcBDV2YAM1VtU0+jw1aCXltTOoVKFamIqcblYt1a9oJbv4+mPcBnJ\ny4XwuWSTgdfcLu3e1hLPFpBh9aQsxmqPn2OuxQqfggvLeStwazwNCiXJbSgE6XEr\npQIDAQAB\n-----END PUBLIC KEY-----", "2QJmLPD5ktxIrFkr");
            RSA       rSA       = cryptoKey.GetRSA();

            byte[]        msg          = System.Convert.FromBase64String(payload);
            byte[]        bytes        = rSA.PublicDecrypt(msg, RSA.Padding.PKCS1);
            string        @string      = System.Text.Encoding.Default.GetString(bytes);
            IRestResponse restResponse = new RestResponse();

            restResponse.Content = @string;
            JsonDeserializer jsonDeserializer = new JsonDeserializer();
            License          result           = jsonDeserializer.Deserialize <License>(restResponse);

            rSA.Dispose();
            return(result);
        }