RSAの用途

RSAの用途

ときにこれを解読するRSA秘密鍵で一緒にTCP通信をデバッグ中に私の同僚との今日、ので、私は本当に思考の多くを過ごしました。
そのようなプロセスは、一般に、固定端末ログイン・パスワードの暗号化DES送信データ、ログイン情報を受信した後、プラットフォームを確認した後、文字列を返し、使用時
以降の通信のような文字列が公開鍵暗号の文字列RSAの使用であるが、パスワードのDES使用。JAVAプラットフォームは、公開鍵を使用して開発され、秘密鍵ファイルがされ
、端末に保存されています。しかし、これは、PEMファイル形式で、道の公開鍵証明書を使用するには、デバッグを送ることがプラットフォームに公開鍵暗号を使用して、プラットフォームはすべて暗号化解除することができます。
しかし、プラットフォームは、復号化された公開鍵暗号が正常でない送り返します。最終的に解決し、言っているのではない情報を検索します。ここでは、レコードを行うには、だけでなく、共有すること。
サードパーティ製のライブラリを使用する:。はBouncyCastle、NuGetをインストールするために使用することができます
最初は、システムの変換RSAキーとPEMキーです。

/// <summary>
        /// Pem密钥转RSA密钥
        /// </summary>
        /// <param name="pemKey">Pem密钥</param>
        /// <param name="isPrivateKey">是否是私钥</param>
        /// <returns>RSA密钥</returns>
        public static string PemToRSAKey(string pemKeyFileName, bool isPrivateKey)
        {
            string rsaKey = string.Empty;
            object pemObject = null;
            RSAParameters rsaPara = new RSAParameters();
            using (StreamReader sReader = new StreamReader(pemKeyFileName))
            {
                var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(sReader);
                pemObject = pemReader.ReadObject();

            }
            //RSA私钥
            if (isPrivateKey)
            {
                RsaPrivateCrtKeyParameters key = (RsaPrivateCrtKeyParameters)pemObject;
                rsaPara = new RSAParameters
                {
                    Modulus = key.Modulus.ToByteArrayUnsigned(),
                    Exponent = key.PublicExponent.ToByteArrayUnsigned(),
                    D = key.Exponent.ToByteArrayUnsigned(),
                    P = key.P.ToByteArrayUnsigned(),
                    Q = key.Q.ToByteArrayUnsigned(),
                    DP = key.DP.ToByteArrayUnsigned(),
                    DQ = key.DQ.ToByteArrayUnsigned(),
                    InverseQ = key.QInv.ToByteArrayUnsigned(),
                };
            }
            //RSA公钥
            else
            {
                RsaKeyParameters key = (RsaKeyParameters)pemObject;
                rsaPara = new RSAParameters
                {
                    Modulus = key.Modulus.ToByteArrayUnsigned(),
                    Exponent = key.Exponent.ToByteArrayUnsigned(),
                };
            }
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaPara);
            using (StringWriter sw = new StringWriter())
            {
                sw.Write(rsa.ToXmlString(isPrivateKey ? true : false));
                rsaKey = sw.ToString();
            }
            return rsaKey;
        }
        /// <summary>
        /// RSA密钥转Pem密钥
        /// </summary>
        /// <param name="RSAKey">RSA密钥</param>
        /// <param name="isPrivateKey">是否是私钥</param>
        /// <returns>Pem密钥</returns>
        public static string RSAKeyToPem(string RSAKey, bool isPrivateKey)
        {
            string pemKey = string.Empty;
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(RSAKey);
            RSAParameters rsaPara = new RSAParameters();
            RsaKeyParameters key = null;
            //RSA私钥
            if (isPrivateKey)
            {
                rsaPara = rsa.ExportParameters(true);
                key = new RsaPrivateCrtKeyParameters(
                    new BigInteger(1, rsaPara.Modulus), new BigInteger(1, rsaPara.Exponent), new BigInteger(1, rsaPara.D),
                    new BigInteger(1, rsaPara.P), new BigInteger(1, rsaPara.Q), new BigInteger(1, rsaPara.DP), new BigInteger(1, rsaPara.DQ),
                    new BigInteger(1, rsaPara.InverseQ));
            }
            //RSA公钥
            else
            {
                rsaPara = rsa.ExportParameters(false);
                key = new RsaKeyParameters(false,
                    new BigInteger(1, rsaPara.Modulus),
                    new BigInteger(1, rsaPara.Exponent));
            }
            using (TextWriter sw = new StringWriter())
            {
                var pemWriter = new PemWriter(sw);
                pemWriter.WriteObject(key);
                pemWriter.Writer.Flush();
                pemKey = sw.ToString();
            }
            return pemKey;
        }

公開鍵暗号は、証明書がキ​​ーファイルのPEMを開くには

 /// <summary>
/// RSA公钥加密
/// </summary>
/// <param name="data"></param>
/// <param name="publicKey"></param>
/// <returns></returns>
public static byte[] RSAPublicEncrypt(byte[] data, string publicKeyFileName)
{
    X509Certificate2 x509Certificate2 = new X509Certificate2(publicKeyFileName);
    RSACryptoServiceProvider pubKey = (RSACryptoServiceProvider)x509Certificate2.PublicKey.Key;
    byte[] bys = pubKey.Encrypt(data, false);
    //string result = Convert.ToBase64String(bys);
    //return Encoding.UTF8.GetBytes(result);
    return bys;
}

復号化するための秘密鍵

 /// <summary>
/// RSA私钥解密
/// </summary>
/// <param name="data"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static byte[] RSAPrivateDecrypt(byte[] data, string privateKeyFileName)
{
    string xml = PemToRSAKey(privateKeyFileName, true);//将pem密钥转为RSA密钥
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    rsa.FromXmlString(xml);
    return rsa.Decrypt(data, false);
}

おすすめ

転載: www.cnblogs.com/zzr-stdio/p/11240329.html