首页 > 其他分享 >生成DKIM的公钥和私钥

生成DKIM的公钥和私钥

时间:2024-02-18 15:22:06浏览次数:54  
标签:公钥 私钥 证书 rsa private RSA key DKIM

生成一个公钥和私钥

可以使用下述网站生成公钥和私钥(当然,使用外部网站来生成公钥和私钥是不安全的,建议仅作为测试时使用)
https://easydmarc.com/tools/dkim-record-generator

通过证书生成一个公钥和私钥

          使用OpenSSL创建证书

                    1.安装 OpenSSL:首先,在计算机上安装 OpenSSL。可以从 OpenSSL 官方网站(https://www.openssl.org/)下载适用于操作系统的适当版本。
                    2. openssl genpkey -algorithm RSA -out private.key 这将生成一个名为 private.key 的私钥文件。
                    3. 生成证书签名请求(CSR):使用私钥生成证书签名请求(CSR),该请求包含有关您的证书的信息。使用以下命令生成 CSR 文件:
                    openssl req -new -key private.key -out csr.csr
                    4.在生成 CSR 过程中,您将需要提供一些证书的相关信息,例如组织名称、常用名称 (CN) 等。
                    自签名证书:如果您只是需要自签名证书,您可以使用以下命令生成自签名证书:
                    openssl x509 -req -days 365 -in csr.csr -signkey private.key -out certificate.crt
                    这将生成一个名为 certificate.crt 的自签名证书文件。
                    ※以上方法来自于Chatgpt,未经验证,不保证是否正确

 

          使用C# code创建证书

                    .Net创建证书存在限制,出于防止私钥被意外泄露的安全考虑,证书的私钥正常是无法导出的(RSACng 类的 AllowPlaintextExport 属性不再可用)
                    当然,我们也可以通过其他方式绕过这种限制,具体可以参考下面的code

                 创建证书

 1         /// <summary>
 2         /// Use this method to create a certificate
 3         /// </summary>
 4         /// <returns></returns>
 5         public static X509Certificate2 CreateCertificateWithPrivateKey()
 6         {
 7             // 创建 RSA 密钥对
 8             RSA rsa = RSA.Create();
 9 
10             // 创建证书请求
11             CertificateRequest request = new CertificateRequest("CN=My Certificate", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
12 
13             // 添加扩展名,可选
14             request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));
15             request.CertificateExtensions.Add(new X509KeyUsageExtension(System.Security.Cryptography.X509Certificates.X509KeyUsageFlags.DigitalSignature, false));
16 
17             // 创建自签名证书
18             X509Certificate2 certificate = request.CreateSelfSigned(DateTimeOffset.Now.AddDays(-1), DateTimeOffset.Now.AddYears(1));
19 
20             //var content = certificate.Export(X509ContentType.Pfx, password);
21             //File.WriteAllBytes(exportPath, content);
22             return certificate;
23         }

               导出证书的私钥

/// <summary>
        /// Use this method to export private key
        /// certificate not support export private key if created by C# code
        /// use this method to support export private key
        /// refer path:https://stackoverflow.com/questions/65242917/unable-to-export-rsa-private-parameters-when-running-as-administrator
        /// for now, not find a method to support .Net Framwork to export private key.
        /// </summary>
        /// <param name="cert"></param>
        /// <returns></returns>
        public static RSA GetExportableRSAPrivateKey(X509Certificate2 cert)
        {
            const CngExportPolicies exportability = CngExportPolicies.AllowExport | CngExportPolicies.AllowPlaintextExport;

            var rsa = cert.GetRSAPrivateKey();

            // Thankfully we don't have to deal with all this shit on Linux
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                return rsa;

            // We always expect an RSACng on Windows these days, but that could change
            if (!(rsa is RSACng rsaCng))
                return rsa;

            // Is the AllowPlaintextExport policy flag already set?
            if ((rsaCng.Key.ExportPolicy & exportability) != CngExportPolicies.AllowExport)
                return rsa;

            try
            {
                // Export the original RSA private key to an encrypted blob - note you will get "The requested operation
                // is not supported" if trying to export without encryption, so we export with encryption!
                var exported = rsa.ExportEncryptedPkcs8PrivateKey(nameof(GetExportableRSAPrivateKey),
                    new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, 2048));

                // Load the exported blob into a fresh RSA object, which will have the AllowPlaintextExport policy without
                // having to do anything else
                RSA copy = RSA.Create();
                copy.ImportEncryptedPkcs8PrivateKey(nameof(GetExportableRSAPrivateKey), exported, out _);

                return copy;
            }
            finally
            {
                rsa.Dispose();
            }
        }

 

标签:公钥,私钥,证书,rsa,private,RSA,key,DKIM
From: https://www.cnblogs.com/xluoblog/p/18019369

相关文章

  • 如何在一台新电脑上添加github的ssh公钥
    大概原理使用git在自己电脑上生成一个id_rsa和id_rsa.pub两个文件然后复制id_rsa.pub中的到github中生成一个ssh公钥具体步骤及代码可以看链接操作链接1.设置用户名gitconfig--globaluser.name‘用户名’2.设置用户名邮箱gitconfig--globaluser.email‘你的......
  • 【小记】Docker容器间SSH公钥自动交换实现免密登录的一次尝试
    咋想到这茬了最近开始忙毕设的事儿了,想部署个伪分布式的Spark+Hadoop集群来进行测试。思来考去,最终咱把目光放在了Docker上。盘了两天,发现这玩意意外的有趣,镜像构建好后开箱即用,省去了些配置环境的成本。不过呢,在配置Hadoop的时候我发现了一个问题——Hadoop分布式搭建要求各......
  • 关于“公钥加密私钥解密,私钥加签公钥验签”的一些理解
    看了网上的很多资料,发现有些点没有说到,也比较复杂,这里根据个人的理解,简单描述,方便记忆。 先理解公/私钥(yue)的意思:私钥,即 私人 的钥匙,是唯一的,所以可以用来证明来源是特定的人公钥,即 公用 的钥匙,我可以将它给很多人(公众)。所以既然那么多人都知道,所以公钥并不能证明来源......
  • MPC 是下一代私钥安全的7大原因
    PrimiHub一款由密码学专家团队打造的开源隐私计算平台,专注于分享数据安全、密码学、联邦学习、同态加密等隐私计算领域的技术和内容。多重签名钱包与单一密钥钱包相比,因其提升了资产安全性,如今已成为机构管理加密货币的标准做法。然而,最近在多方计算(MPC)领域的密码学突破正引领......
  • linux生成ssh的一对公钥和私钥
    1.首先进入.SSH目录中Linux中,每个用户的根目录下都有一个.ssh目录,保存了ssh相关的key和一些记录文件。例如:cd~/ll-a 2. 使用ssh-keygen生成keyssh-keygen可以生成ssh协议所需要的公钥和私钥,例如:ssh-keygen-trsa然后回提示让你输入一些文件名啥的,别管那些,一路按E......
  • 一键在线获取APP公钥、包名、签名及备案信息方法介绍
    ​目录一键在线获取APP公钥、包名、签名及备案信息方法介绍摘要引言一键获取APP包信息操作步骤​编辑解析报告总结致谢关键词参考资料声明 摘要本文介绍了一款在线APP解析工具,可以一键获取APP的公钥、包名、签名等基础信息,同时提供了详细的操作步骤和使用示例,帮......
  • Flutter开发之安卓打包,和获取签名相关信息,公钥值相关操作
    我们Flutter开发中,难免有些小朋友因需求要获取打包签名的一些相关信息,下面我们来讲一下怎么获取一生成签名文件我们打包安卓APK包需要先生成签名文件,需运行以下指令,生成签名文件keytool-genkey-v-keystoreGMT\_keystore.keystore-aliasGMT\_keystore-keyalgRSA-keysi......
  • App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
    iOS备案查看信息iOS平台BundleID公钥证书SHA-1指纹IOS平台服务器域名获取BundleID:或者https://developer.apple.com/account/resources/identifiers/list获取公钥与签名SHA1值:https://developer.apple.com/account/resources/certificates/list......
  • 使用 openssl 从cer公钥证书中导出公钥pem
    使用openssl从cer公钥证书中导出公钥pem ---------- "der公钥证书"转"base64公钥证书"opensslx509-informder-inz11.cer-outz11.pem从"DER公钥证书"中导出"公钥"opensslx509-informder-inz11.cer-pubkey-noout>zpublickey.pem从"......
  • 同时为github和gitee配置公钥
    同时为github和gitee配置公钥1.下载githttps://git-scm.com/download/win2.配置公钥参考https://help.gitee.com/base/account/SSH公钥设置通过命令ssh-keygen生成SSHKey:ssh-keygen-ted25519-C"[email protected]"输入秘钥文件名,其他回车id_rsa_githubid......