如何将文件中Base64进制数据生成X509证书

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_2328/article/details/81973304

byte[] ciphertext1 = new BASE64Decoder()
                .decodeBuffer("MIIFeTCCBGGgAwIBAgIBATANBgkqhkiG9w0BAQUFADCBxjELMAkGA1UEBhMCTk8xDTALBgNVBAgTBE9zbG8xDTALBgNVBAcTBE9zbG8xIjAgBgNVBAoTGVdvcmxkIFdpZGUgV2ViIENvbnNvcnRpdW0xNjA0BgNVBAsTLXczYy13aWRnZXRzLWRpZ3NpZy10ZXN0c3VpdGUgcm9vdCBjZXJ0aWZpY2F0ZTEbMBkGA1UEAxMSaHR0cDovL3d3dy53My5vcmcvMSAwHgYJKoZIhvcNAQkBFhFzdHVhcnRrQG9wZXJhLmNvbTAeFw0xMDA5MjQwODQ2MTFaFw0zMDA5MTkwODQ2MTFaMIG/MQswCQYDVQQGEwJOTzENMAsGA1UECBMET3NsbzEiMCAGA1UEChMZV29ybGQgV2lkZSBXZWIgQ29uc29ydGl1bTE+MDwGA1UECxM1dzNjLXdpZGdldHMtZGlnc2lnLXRlc3RzdWl0ZSBzZWNvbmQgbGV2ZWwgY2VydGlmaWNhdGUxGzAZBgNVBAMTEmh0dHA6Ly93d3cudzMub3JnLzEgMB4GCSqGSIb3DQEJARYRc3R1YXJ0a0BvcGVyYS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCz8QGhzBdMDXd0qLXY6jEtWudeAAQ5JRsFgV3ddHeU4oeMCnRTDBWtTiwu9NdaZ+nQPwW9j80iG+CQY53HnNjQXeR++xirBREGM4vIvCz/j9qmJ0acoZ6bS4DhOAMVwlk9Ay3UcZlMVjdEHErFSCQ12SXtaX01O60Zh7CEU+Pq6zKYru6/Cdojp+dD+LKZKxAkfsM3aZTw8CVZm/QI8kaNDFfx7OKGVfcATARq6WU2HT39icrNgC3Kfxvx2vfv6TA7v+jNcdvRu68mIqWFRGT6RKhHA6HLId26BeXoaTNk+st+cj7nTmCdumhS5eV2nB77G5P207HIYVn4U3ME9xN/AgMBAAGjggF1MIIBcTAMBgNVHRMEBTADAQH/MB0GA1UdDgQWBBQN0Gu0xguc9/fNpplu6LSSir/MdjCB+wYDVR0jBIHzMIHwgBTy0/Kw6ZgHtZ6oV9kRqvRXuOpHuKGBzKSByTCBxjELMAkGA1UEBhMCTk8xDTALBgNVBAgTBE9zbG8xDTALBgNVBAcTBE9zbG8xIjAgBgNVBAoTGVdvcmxkIFdpZGUgV2ViIENvbnNvcnRpdW0xNjA0BgNVBAsTLXczYy13aWRnZXRzLWRpZ3NpZy10ZXN0c3VpdGUgcm9vdCBjZXJ0aWZpY2F0ZTEbMBkGA1UEAxMSaHR0cDovL3d3dy53My5vcmcvMSAwHgYJKoZIhvcNAQkBFhFzdHVhcnRrQG9wZXJhLmNvbYIJAMTqFP/Wa8uAMEQGCWCGSAGG+EIBDQQ3FjV3M2Mtd2lkZ2V0cy1kaWdzaWctdGVzdHN1aXRlIHNlY29uZCBsZXZlbCBjZXJ0aWZpY2F0ZTANBgkqhkiG9w0BAQUFAAOCAQEAiXxW/08hOf42PasOPSkDbAaR91Dn1JwFSCEvordp7RM9HN0iogjmkkPLXJn2aLgolWQoh4C227JFA9S+dHO8QjiyaVMHcxziIDnfr3+bpG3URpJm1W5T6PJoj0vsB51iUNpwGB75fr2Yt8uGxufsFJDn/Rs78kGkeXmAEKLnoGb7QCrDGcmiEKoqsWvkg3WiYfoK75cgs5bG7xVks7GgSobCohHqmJE96v2EYouMarHnNUVIvn3w2HGxtOiQj4JP9K2nFFx3gxbdgMGXsNyEMh5Kls9H0tQt6QKLMZd127K571k+fJV6mQUtTwOb8jcecFq8PPh/VvNf5cUi1m5P/w==");

        File f1 = new File("C:\\test2.csr");
        FileOutputStream fos1 = null;

        try
        {
            f1.createNewFile();

            fos1 = new FileOutputStream(f1);
            fos1.write(ciphertext1);
            fos1.flush();
            fos1.close();
        }
        catch (IOException e)
        {
            System.out.println(e);
        }

        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        FileInputStream in = new FileInputStream("C:\\test1.csr");
        Certificate c = cf.generateCertificate(in);
        PublicKey pk = c.getPublicKey();

红色的为:文件中定义的Base64(二进制)数据证书!

展开阅读全文

关于X509证书

07-14

代码如下:在我的机器上WIn7 32位旗舰版 一起运行正常 可以获取证书信息,在XP的机器上也运行正确rnrn但是在客户的机器上 WIn7 64位旗舰版, StoreName.My和AddressBook都没有得到证书信息,也没有发生异常rnrn现在想知道原因? 是缺少权限还是其他什么原因? 非常感谢 不吝赐教。rn------------------------------------------------------------------------------------------rnusing System;rnusing System.Collections.Generic;rnusing System.Linq;rnusing System.Text;rnusing System.Security.Cryptography.X509Certificates;rnusing System.IO;rnrnnamespace ConsoleApplication1rnrn class Programrn rnrn static void Log(string logMessage)rn rn using (StreamWriter w = File.AppendText("运行结果.txt"))rn rnrn w.WriteLine(" -- :0", logMessage);rnrn rn rnrn static void ShowMsg(X509Certificate2 cer)rn rnrn Log(string.Format("-----------------------------------------------------------"));rn Log(string.Format("主题: 0", cer.Subject));rn Log(string.Format("颁发者: 0", cer.Issuer));rn Log(string.Format("版本: 0", cer.Version));rn Log(string.Format("有效日期: 0", cer.NotBefore));rn Log(string.Format("过期日期: 0", cer.NotAfter));rn Log(string.Format("指纹: 0", cer.Thumbprint));rn Log(string.Format("序列号: 0", cer.SerialNumber));rn Log(string.Format("友好名: 0", cer.PublicKey.Oid.FriendlyName));rn Log(string.Format("公钥格式: 0", cer.PublicKey.EncodedKeyValue.Format(true)));rn Log(string.Format("原始数据长度: 0", cer.RawData.Length));rnrnrn rnrn static void Main(string[] args)rn rnrn Log("#################################################################################");rn Log("#################################################################################");rnrn tryrn rnrnrn X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);rn store.Open(OpenFlags.MaxAllowed);rn X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;rn Log("证书个数:"+storecollection.Count.ToString());rn foreach (X509Certificate2 x509 in storecollection) ShowMsg(x509);rnrnrnrn rn catch(Exception e)rn rn Log(e.Message);rn rnrn Log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");rn Log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");rnrn rn tryrn rnrn X509Store store = new X509Store(StoreName.AddressBook, StoreLocation.LocalMachine);rn store.Open(OpenFlags.ReadOnly);rn X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;rn Log("证书个数:" + storecollection.Count.ToString());rn foreach (X509Certificate2 x509 in storecollection) ShowMsg(x509); rn rn rn catch(Exception e)rn rn Log(e.Message);rn rnrnrn System.Console.WriteLine("已经运行完成,按任意键退出,结果请看[运行结果.txt]");rn System.Console.ReadLine();rnrn rnrn rn rnrn 论坛

没有更多推荐了,返回首页