当前位置: 首页>>代码示例>>C#>>正文


C# RSACryptoServiceProvider.ImportCspBlob方法代码示例

本文整理汇总了C#中RSACryptoServiceProvider.ImportCspBlob方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.ImportCspBlob方法的具体用法?C# RSACryptoServiceProvider.ImportCspBlob怎么用?C# RSACryptoServiceProvider.ImportCspBlob使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RSACryptoServiceProvider的用法示例。


在下文中一共展示了RSACryptoServiceProvider.ImportCspBlob方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ExportImportPublicOnly

        public static void ExportImportPublicOnly()
        {
            byte[] expectedExport = ByteUtils.HexToByteArray(
                "0602000000a40000525341310004000001000100e19a01644b82962a224781d1f60c2cc373b"
                + "798df541343f63c638f45fa96e11049c8d9e88bd56483ec3c2d56e9460d2b1140191841761c1523840221b0e"
                + "b6401dc4d09c54bf75cea25d9e191572fb2ec92c3559b35b3ef3fa695171bb1fddeb469792e49f0d17c769d0"
                + "a37f6a4a6584af39878eb21f9ba9eae8be9c39eac6ae0");

            byte[] exported;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.CspTestKey);

                exported = rsa.ExportCspBlob(includePrivateParameters: false);
            }

            Assert.Equal(expectedExport, exported);

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportCspBlob(exported);

                byte[] exported2 = rsa.ExportCspBlob(includePrivateParameters: false);

                Assert.Equal(exported, exported2);

                Assert.Throws<CryptographicException>(() => rsa.ExportCspBlob(includePrivateParameters: true));
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:30,代码来源:ImportExportCspBlob.cs

示例2: Main

  public static void Main(string[] args) {
    string path = args[0];
    byte[] blob = null;
    using(FileStream fs = File.Open(path, FileMode.Open)) {
      blob = new byte[fs.Length];
      fs.Read(blob, 0, blob.Length);
    }

    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    rsa.ImportCspBlob(blob);
    RSAParameters PrivateKey = rsa.ExportParameters(true);
    byte[] key = RSAKeyToASN1(PrivateKey);

    using(FileStream fs = File.Open(path + ".out", FileMode.Create)) {
      fs.Write(key, 0, key.Length);
    }

    Console.WriteLine("Your file is ready for you at " + path + ".out.");
  }
开发者ID:kyungyonglee,项目名称:BrunetTutorial,代码行数:19,代码来源:RSAPrivateKeyToDER.cs

示例3: Pkcs1DecodingTest

    public void Pkcs1DecodingTest()
    {
        #pragma warning disable 0436
        // Initialize the "Known Good" RSAParameters.
        byte[] capi1Blob = Convert.FromBase64String(AsymmetricKeyAlgorithmProviderTests.Helper.PrivateKeyFormatsAndBlobs[Tuple.Create(PCLCrypto.AsymmetricAlgorithm.RsaOaepSha1, CryptographicPrivateKeyBlobType.Capi1PrivateKey)]);
        var rsa = new RSACryptoServiceProvider();
        rsa.ImportCspBlob(capi1Blob);
        RSAParameters rsaCapi = rsa.ExportParameters(true);

        // Now load up the tested one.
        byte[] pkcs1KeyBlob = Convert.FromBase64String(AsymmetricKeyAlgorithmProviderTests.Helper.PrivateKeyFormatsAndBlobs[Tuple.Create(PCLCrypto.AsymmetricAlgorithm.RsaOaepSha1, CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey)]);
        RSAParameters homeReadPkcs1 = KeyFormatter.ToPlatformParameters(KeyFormatter.Pkcs1.Read(pkcs1KeyBlob));
        #pragma warning restore 0436
        Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.Modulus), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.Modulus));
        Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.Exponent), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.Exponent));
        Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.D), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.D));
        Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.P), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.P));
        Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.Q), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.Q));
        Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.DP), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.DP));
        Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.DQ), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.DQ));
        Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.InverseQ), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.InverseQ));
    }
开发者ID:Sway0308,项目名称:PCLCrypto,代码行数:22,代码来源:Pkcs1KeyFormatterTests.cs

示例4: ExportImportPublicPrivate

        public static void ExportImportPublicPrivate()
        {
            // This blob contains the private key of TestData.CspTestKey. The guidelines for the TestData class
            // as to key security/sanity apply to this blob as well.
            byte[] expectedExport = ByteUtils.HexToByteArray(
                "0702000000a40000525341320004000001000100e19a01644b82962a224781d1f60c2cc373b"
                + "798df541343f63c638f45fa96e11049c8d9e88bd56483ec3c2d56e9460d2b1140191841761c1523840221b0e"
                + "b6401dc4d09c54bf75cea25d9e191572fb2ec92c3559b35b3ef3fa695171bb1fddeb469792e49f0d17c769d0"
                + "a37f6a4a6584af39878eb21f9ba9eae8be9c39eac6ae07bb43a9f6e29e584b47303f8ac70384ba4f1a4b7d77"
                + "fb4c931c2a194584b9d6060d39ba798e20698221ac615b083bbdaf2b6f39c05c570276945728800b1aae1531"
                + "511b5878dae8820a178f8cc3cca5426ce761ef3247bce9375318a03c3d5779ed339d2f9d04d6265d0a99057c"
                + "c1af86b656541f4f6b062d8407968aaf794fee33273c0fd4735d688e0e8161f5c9f360c2fc1caed9a2b48a53"
                + "3ea4d26b9ac50a0e7e7ca94c6bd6edfd3fe448650b66fa99c57b50e3737fae9d26300fee06649472a664190e"
                + "a603126718f896bbfe0671401f31414678d173d32c486c8fbb6334fe90c77f7c2a04ee9c3e3ab85d948357f7"
                + "15e5d706031e013f0951eeb1e506c5af71cfec07bbc637d5b7c788fdad21ec5f250ef069d00a5c9bb6e2fe06"
                + "01b91f36121885011cd7186093ee25c2a5dd6b3cfea3d8b1627148ab0a47610b8d99743ac008b62f8a054c18"
                + "4b8b9f862beebc70af40408999bead5a09baec588375be03cfa636b018d7d9948f1abae4d5463c5c5d210a0b"
                + "42589a90a2bc01b1bb027f6c859de82ace0c60237d96574a1752e38b56326c7eae33cf7590da6728ff1de184"
                + "c654fccba0866732e576747107cef935d43aa5f477178aafee834a53a3d14");

            byte[] exported;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.CspTestKey);

                exported = rsa.ExportCspBlob(includePrivateParameters: true);
            }

            Assert.Equal(expectedExport, exported);

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportCspBlob(exported);
                byte[] exported2 = rsa.ExportCspBlob(includePrivateParameters: true);
                Assert.Equal<byte>(exported, exported2);
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:38,代码来源:ImportExportCspBlob.cs

示例5: RSAParametersToBlob_PublicOnly

        public static void RSAParametersToBlob_PublicOnly()
        {
            byte[] blob;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.RSA1024Params);
                blob = rsa.ExportCspBlob(false);
            }

            RSAParameters exported;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportCspBlob(blob);

                Assert.True(rsa.PublicOnly);

                exported = rsa.ExportParameters(false);
            }

            RSAParameters expected = new RSAParameters
            {
                Modulus = TestData.RSA1024Params.Modulus,
                Exponent = TestData.RSA1024Params.Exponent,
            };

            ImportExport.AssertKeyEquals(ref expected, ref exported);
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:29,代码来源:ImportExportCspBlob.cs

示例6: RSAParametersToBlob_PublicPrivate

        public static void RSAParametersToBlob_PublicPrivate()
        {
            byte[] blob;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.RSA1024Params);
                blob = rsa.ExportCspBlob(true);
            }

            RSAParameters exported;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportCspBlob(blob);

                Assert.False(rsa.PublicOnly);

                exported = rsa.ExportParameters(true);
            }

            RSAParameters expected = TestData.RSA1024Params;

            ImportExport.AssertKeyEquals(ref expected, ref exported);
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:25,代码来源:ImportExportCspBlob.cs

示例7: CreateKey_LegacyProvider_RoundtripBlob

        public static void CreateKey_LegacyProvider_RoundtripBlob()
        {
            const int KeySize = 512;

            CspParameters cspParameters = new CspParameters(PROV_RSA_FULL);
            byte[] blob;

            using (var rsa = new RSACryptoServiceProvider(KeySize, cspParameters))
            {
                CspKeyContainerInfo containerInfo = rsa.CspKeyContainerInfo;
                Assert.Equal(PROV_RSA_FULL, containerInfo.ProviderType);
                Assert.Equal(KeySize, rsa.KeySize);

                blob = rsa.ExportCspBlob(true);
            }

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportCspBlob(blob);

                CspKeyContainerInfo containerInfo = rsa.CspKeyContainerInfo;

                // The provider information is not persisted in the blob
                Assert.Equal(PROV_RSA_AES, containerInfo.ProviderType);
                Assert.Equal(KeySize, rsa.KeySize);
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:27,代码来源:RSACryptoServiceProviderTests.cs

示例8: GenerateCACert

    public bool GenerateCACert(string group)
    {
        if(!Context.Request.IsLocal) {
          throw new Exception("Call must be made locally!");
        }

        string private_path = GetGroupPrivatePath(group);
        Directory.CreateDirectory(private_path);

        private_path += "private_key";
        RSACryptoServiceProvider private_key = new RSACryptoServiceProvider(2048);
        byte[] private_blob = private_key.ExportCspBlob(true);
        using(FileStream fs = File.Open(private_path, FileMode.Create)) {
          fs.Write(private_blob, 0, private_blob.Length);
        }

        string data_path = GetGroupDataPath(group);
        Directory.CreateDirectory(data_path);

        RSACryptoServiceProvider public_key = new RSACryptoServiceProvider();
        public_key.ImportCspBlob(private_key.ExportCspBlob(false));

        CertificateMaker cm = new CertificateMaker(string.Empty, group,
        string.Empty, "admin", string.Empty, public_key, string.Empty);
        Certificate cert = cm.Sign(cm, private_key);

        string cacert_path = GetGroupDataPath(group) + "cacert";
        byte[] cert_data = cert.X509.RawData;
        using(FileStream fs = File.Open(cacert_path, FileMode.Create)) {
          fs.Write(cert_data, 0, cert_data.Length);
        }

        return true;
    }
开发者ID:davidiw,项目名称:Joomla-Grid-Appliance,代码行数:34,代码来源:GroupVPN.cs

示例9: SignCertificate

    protected bool SignCertificate(string group, string request_id)
    {
        string request_path = GetGroupDataPath(group) + request_id;
        if(!File.Exists(request_path)) {
          throw new Exception("No such request.");
        }

        CertificateMaker cm = null;
        using(FileStream fs = File.Open(request_path, FileMode.Open)) {
          byte[] blob = new byte[fs.Length];
          fs.Read(blob, 0, blob.Length);
          cm = new CertificateMaker(blob);
        }
        // We need to create a new certificate with all the users info!

        string private_path = GetGroupPrivatePath(group) + "private_key";
        if(!File.Exists(private_path)) {
          throw new Exception("No private key.");
        }

        RSACryptoServiceProvider private_key = new RSACryptoServiceProvider();
        using(FileStream fs = File.Open(private_path, FileMode.Open)) {
          byte[] blob = new byte[fs.Length];
          fs.Read(blob, 0, blob.Length);
          private_key.ImportCspBlob(blob);
        }

        string cacert_path = GetGroupDataPath(group) + "cacert";
        if(!File.Exists(cacert_path)) {
          throw new Exception("No CA Certificate.");
        }

        Certificate cacert = null;
        using(FileStream fs = File.Open(cacert_path, FileMode.Open)) {
          byte[] blob = new byte[fs.Length];
          fs.Read(blob, 0, blob.Length);
          cacert = new Certificate(blob);
        }

        Certificate cert = cm.Sign(cacert, private_key);

        request_path += ".signed";
        using(FileStream fs = File.Open(request_path, FileMode.Create)) {
          byte[] blob = cert.X509.RawData;
          fs.Write(blob, 0, blob.Length);
        }

        return true;
    }
开发者ID:davidiw,项目名称:Joomla-Grid-Appliance,代码行数:49,代码来源:GroupVPN.cs

示例10: UpdateRevocationList

    public bool UpdateRevocationList(string group_name)
    {
        if(!Context.Request.IsLocal) {
          throw new Exception("Call must be made locally!");
        }

        IDbConnection dbcon = new MySqlConnection(_connection_string);
        dbcon.Open();
        IDbCommand dbcmd = dbcon.CreateCommand();

        // Get the group_id
        string sql = "SELECT group_id from groupvpn WHERE group_name = \"" + group_name + "\"";
        dbcmd.CommandText = sql;
        IDataReader reader = dbcmd.ExecuteReader();
        if(!reader.Read()) {
          throw new Exception("No such group.");
        }

        int group_id = (int) reader["group_id"];
        reader.Close();

        // get revoked users
        sql = "SELECT user_id FROM groups WHERE group_id = \"" + group_id + "\" and revoked = 1";
        dbcmd.CommandText = sql;
        reader = dbcmd.ExecuteReader();

        // add revoked users by user name to the revocation list
        ArrayList revoked_user_ids = new ArrayList();
        while(reader.Read()) {
          revoked_user_ids.Add((int) reader["user_id"]);
        }

        reader.Close();

        ArrayList revoked_users = new ArrayList();
        foreach(int user_id in revoked_user_ids) {
          sql = "SELECT username FROM " + _db_prefix + "users WHERE id = " + user_id;
          dbcmd.CommandText = sql;
          IDataReader user_reader = dbcmd.ExecuteReader();
          if(!user_reader.Read()) {
        continue;
          }

          revoked_users.Add(user_reader["username"]);
          user_reader.Close();
        }

        reader.Close();
        dbcmd.Dispose();
        dbcon.Close();

        // get private key
        string private_path = GetGroupPrivatePath(group_name) + "private_key";
        if(!File.Exists(private_path)) {
          throw new Exception("No private key for " + private_path + " " + File.Exists(private_path));
        }

        RSACryptoServiceProvider private_key = new RSACryptoServiceProvider();
        using(FileStream fs = File.Open(private_path, FileMode.Open)) {
          byte[] blob = new byte[fs.Length];
          fs.Read(blob, 0, blob.Length);
          private_key.ImportCspBlob(blob);
        }

        // create revocation list
        byte[] to_sign = null;
        using(MemoryStream ms = new MemoryStream()) {
          NumberSerializer.WriteLong(DateTime.UtcNow.Ticks, ms);
          AdrConverter.Serialize(revoked_users, ms);
          to_sign = ms.ToArray();
        }

        // sign revocation list
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        byte[] hash = sha1.ComputeHash(to_sign);
        byte[] signature = private_key.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
        byte[] data = new byte[4 + to_sign.Length + signature.Length];
        NumberSerializer.WriteInt(to_sign.Length, data, 0);
        to_sign.CopyTo(data, 4);
        signature.CopyTo(data, 4 + to_sign.Length);

        // write revocation list
        using(FileStream fs = File.Open(GetGroupDataPath(group_name) + "revocation_list", FileMode.Create)) {
          fs.Write(data, 0, data.Length);
        }

        return true;
    }
开发者ID:davidiw,项目名称:Joomla-Grid-Appliance,代码行数:88,代码来源:GroupVPN.cs


注:本文中的RSACryptoServiceProvider.ImportCspBlob方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。