zoukankan      html  css  js  c++  java
  • Does RSA Private key always contain the Public key, or is it just .NET?

    Does RSA Private key always contain the Public key, or is it just .NET?

    回答1

    The private key always includes the public key.

    What you might really want is Signing. Using the same .NET classes, you can sign data with your private key and verify the signature on the other party's side with the public key (which obviously doesn't contain the private key).

        public static string Sign(string data, string privateAndPublicKey)
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(data);
            RSACryptoServiceProvider provider = CreateProviderFromKey(privateAndPublicKey);
            byte[] signatureBytes = provider.SignData(dataBytes, "SHA1");
            return Convert.ToBase64String(signatureBytes);
        }
    
        public static bool Verify(string data, string signature, string publicKey)
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(data);
            byte[] signatureBytes = Convert.FromBase64String(signature);
            RSACryptoServiceProvider provider = CreateProviderFromKey(publicKey);
            return provider.VerifyData(dataBytes, "SHA1", signatureBytes);
        }
    
        private static RSACryptoServiceProvider CreateProviderFromKey(string key)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(key);
            return provider;
        }
    
  • 相关阅读:
    2019年第二周作业
    2019年pta作业第二题——求最大值及其下标
    2019春第十一周作业
    2019春第十周作业
    2019年寒假作业3
    2019年寒假作业2
    2019年寒假作业1
    我的老师
    自说
    Day16
  • 原文地址:https://www.cnblogs.com/chucklu/p/15664651.html
Copyright © 2011-2022 走看看