Ejemplo n.º 1
0
        public override byte[] DecryptValue(byte[] bytesToDecrypt)
        {
            Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding decryptEngine =
                new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine());

            decryptEngine.Init(false, m_keyPair.Private);

            // string decrypted = System.Text.Encoding.UTF8.GetString(
            return(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
            //);
        }
Ejemplo n.º 2
0
        public override byte[] EncryptValue(byte[] bytesToEncrypt)
        {
            Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding encryptEngine =
                new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine());

            encryptEngine.Init(true, m_keyParameter);

            // string encrypted = System.Convert.ToBase64String(
            return(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
            // );
        }
Ejemplo n.º 3
0
        public override byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
        {
            Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding encryptEngine =
                new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine());

            encryptEngine.Init(true, m_keyParameter);

            // string encrypted = System.Convert.ToBase64String(
            return(encryptEngine.ProcessBlock(data, 0, data.Length));
            // );
        }
Ejemplo n.º 4
0
        public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
        {
            Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding decryptEngine =
                new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine());

            decryptEngine.Init(false, m_keyPair.Private);

            // string decrypted = System.Text.Encoding.UTF8.GetString(
            return(decryptEngine.ProcessBlock(data, 0, data.Length));
            //);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 报文加密解密入口
 /// </summary>
 /// <param name="data">处理报文</param>
 /// <param name="encrypt">true 加密处理  false 解密处理</param>
 /// <param name="usePublicKey">true 使用公钥处理 false使用私钥处理</param>
 /// <returns></returns>
 private byte[] RSAProcess(byte[] data, bool encrypt, bool usePublicKey)
 {
     try
     {
         enc.Init(encrypt, usePublicKey ? publicKey : privateKey);
         return(enc.ProcessBlock(data, 0, data.Length));
     }
     catch (Exception ex)
     {
         LogHelper.Log("RSA" + (usePublicKey ? "公钥" : "私钥") + (encrypt ? "加密" : "解密") + "失败", ex);
         return(null);
     }
 }
Ejemplo n.º 6
0
        public static string EncryptRSA(string unencrypted, string pubKey)
        {
            var bytesToEncrypt = Encoding.UTF8.GetBytes(unencrypted);
            var encryptEngine  = new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine());

            using (var txtreader = new StringReader(pubKey))
            {
                var keyParameter = (AsymmetricKeyParameter) new PemReader(txtreader).ReadObject();
                encryptEngine.Init(true, keyParameter);
            }
            var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));

            return(encrypted);
        }
Ejemplo n.º 7
0
        public static string RsaEncryptWithPrivate(string clearText, string privateKey)
        {
            var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
            var encryptEngine  = new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine());

            using (var txtreader = new StringReader(privateKey))
            {
                var keyPair = (AsymmetricCipherKeyPair) new Org.BouncyCastle.OpenSsl.PemReader(txtreader).ReadObject();
                encryptEngine.Init(true, keyPair.Public);
            }

            var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));

            return(encrypted);
        }
Ejemplo n.º 8
0
        public static string DecryptRSA(string encrypted, string privKey)
        {
            var bytesToDecrypt = Convert.FromBase64String(encrypted);
            AsymmetricCipherKeyPair keyPair;
            var decryptEngine = new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new RsaEngine());

            byte[] result;
            using (var txtreader = new StringReader(privKey))
            {
                keyPair = (AsymmetricCipherKeyPair) new PemReader(txtreader).ReadObject();
                decryptEngine.Init(false, keyPair.Private);
                result = decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length);
            }
            var decrypted = Encoding.UTF8.GetString(result, 0, result.Length);

            return(decrypted);
        }
Ejemplo n.º 9
0
        public string RsaDecryptWithPublic(string base64Input, string publicKey)
        {
            byte[] bytesToDecrypt = System.Convert.FromBase64String(base64Input);
            Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding decryptEngine =
                new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine());

            using (System.IO.StringReader txtreader = new System.IO.StringReader(publicKey))
            {
                Org.BouncyCastle.Crypto.AsymmetricKeyParameter keyParameter =
                    (Org.BouncyCastle.Crypto.AsymmetricKeyParameter) new Org.BouncyCastle.OpenSsl.PemReader(txtreader).ReadObject();

                decryptEngine.Init(false, keyParameter);
            }

            string decrypted = System.Text.Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));

            return(decrypted);
        }
Ejemplo n.º 10
0
        public string RsaEncryptWithPublic(string clearText, string publicKey)
        {
            byte[] bytesToEncrypt = System.Text.Encoding.UTF8.GetBytes(clearText);
            Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding encryptEngine =
                new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine());

            using (System.IO.StringReader txtreader = new System.IO.StringReader(publicKey))
            {
                Org.BouncyCastle.Crypto.AsymmetricKeyParameter keyParameter =
                    (Org.BouncyCastle.Crypto.AsymmetricKeyParameter) new Org.BouncyCastle.OpenSsl.PemReader(txtreader).ReadObject();

                encryptEngine.Init(true, keyParameter);
            }

            string encrypted = System.Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));

            return(encrypted);
        }