数字证书生成--加密密/加签验签

最近一个项目,处于安全上的考虑,前后端需要使用安全证书加密通信,涉及ios/android-后台交互。在测试环境上没有正式的CA证书,使用自签证书开发。

下面把生成4套环境的自签证书过程mark下,如有需要,可参考:

 

以下命令的执行环境均为windows-cmd界面(前提需安装jdk,使用jdk自带的keytool工具)

 

1、生成jks、csr证书(这俩证书暂时没用):

keytool -genkey -alias *.test.com -sigalg SHA1withRSA -keyalg RSA -keysize 2048 -keystore D:/Citificate/testKey/test.jks -dname "C=CN,ST=珠海,L=珠海,O=测试样例公司,OU=研发部,CN=*.test.com" && keytool -certreq -alias *.test.com -file D:/Citificate/testKey/test.csr -keystore D:/Citificate/testKey/test.jks && echo Your certificate signing request file is D:/Citificate/testKey/test.csr.  Your keystore file is D:/Citificate/testKey/test.jks.  Thanks for using the 亚洲诚信TrustAsia keytool CSR helper.

 

2、生成keystore密钥库:

keytool -genkey -alias *.test.com -keypass password -keyalg RSA -keysize 2048 -validity 730 -keystore D:/Citificate/testKey/test.keystore -dname "C=CN,ST=珠海,L=珠海,O=测试样例公司,OU=研发部,CN=*.test.com" -storepass password

 

3、从密钥库导出cer、crt公钥证书:

keytool -export -alias *.test.com -keystore D:/Citificate/testKey/test.keystore -storepass password -rfc -file D:/Citificate/testKey/test.cer

crt公钥证书可以直接把这一步生成的cer证书修改文件后缀名得到

 

4、使用java工具类导出证书key

package test;

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;

import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class SslKey {

    public static KeyStore getKeyStore(String keyStorePath, String password) throws Exception {
        FileInputStream is = new FileInputStream(keyStorePath);
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(is, password.toCharArray());
        is.close();
        return ks;
    }

    public static PrivateKey getPrivateKey() {
        try {

            BASE64Encoder encoder = new BASE64Encoder();
            KeyStore ks = getKeyStore("D:/Citificate/testKey/test.keystore", "password");
            PrivateKey key = (PrivateKey) ks.getKey("*.test.com", "password".toCharArray());
            String encoded = encoder.encode(key.getEncoded());
            System.out.println("-----BEGIN RSA PRIVATE KEY-----");
            System.out.println(encoded);
            System.out.println("-----END RSA PRIVATE KEY-----");
            return key;
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        getPrivateKey();
    }

}

执行方法后,将结果拷入新建的后缀名为:.key的文本文档

 

5、使用openssl将上一步生成的.key文件做.unsecure文件输出(nginx使用,使用.key.unsecure可以避免使用.key文件时每次访问都要输入密码,本套方案前后端加密该文件未使用)-前提是先安装openssl插件,不然cmd会报openssl命令错误

openssl rsa -in D:/Citificate/testKey/test.key -out D:/Citificate/testKey/test.key.unsecure

 

6、使用java工具类从密钥库导出pfx私钥:

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.util.Enumeration;

public class CreatePfx {
    /** 
     * 将keystore转为pfx
     * 
     * @param keyStoreFile 生成的文件名和路径
     * @param pfxPsw 密码
     * @param pfxFile 原文件路径及名称
     */
    public static void coverToPfx() throws Exception {
        String keyStoreFile = "D:/Citificate/testKey/test.keystore";
        String pfxPsw = "password";
        String pfxFile = "D:/Citificate/testKey/test.pfx";

        KeyStore inputKeyStore = null;
        FileInputStream input = null;
        FileOutputStream output = null;
        String keyAlias = "";
        try {
            inputKeyStore = KeyStore.getInstance("JKS");
            input = new FileInputStream(keyStoreFile);
            char[] password = null;
            if ((pfxPsw == null) || pfxPsw.trim().equals("")) {
                password = null;
            } else {
                password = pfxPsw.toCharArray();
            }
            inputKeyStore.load(input, password);
            KeyStore outputKeyStore = KeyStore.getInstance("PKCS12");
            outputKeyStore.load(null, pfxPsw.toCharArray());
            Enumeration enums = inputKeyStore.aliases();
            while (enums.hasMoreElements()) {
                keyAlias = (String) enums.nextElement();
                System.out.println("alias=[" + keyAlias + "]");
                if (inputKeyStore.isKeyEntry(keyAlias)) {
                    Key key = inputKeyStore.getKey(keyAlias, password);
                    Certificate[] certChain = inputKeyStore.getCertificateChain(keyAlias);
                    outputKeyStore.setKeyEntry(keyAlias, key, pfxPsw.toCharArray(), certChain);
                }
            }
            output = new FileOutputStream(pfxFile);
            outputKeyStore.store(output, password);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }

}

 

以上证书已经可以适应安卓端-后台加密通信

方案:

1)安卓端使用cer或crt公钥加密,后台使用keystore解密

2)后台使用keystore签名,安卓端使用使用cer或crt公钥验签

 

ios还需要der和pem证书支持

7、使用openssl将cer类型公钥转换为ios能使用的der类型公钥:

openssl x509 -outform der -in D:/Citificate/testKey/test.cer -out D:/Citificate/testKey/test.der

 

8、使用openssl生成ios端验签所需pem文件:

openssl rsa -in D:/Citificate/testKey/test.key -pubout -out D:/Citificate/testKey/test.pem

 

详细的加解密及加签验签方案见:http://www.cnblogs.com/shindo/p/6349070.html

 

转载于:https://www.cnblogs.com/shindo/p/6346971.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: .NET证书签验是指使用.NET框架的相关类库来进行数字证书加密和验证操作。在过程中,首先需要获取证书的私钥,然后使用私钥对要加密的数据进行名,生成名后的数据。生成名数据可以与原始数据一起传输给接收方。 在验签过程中,接收方需要获取证书的公钥,并使用公钥对接收到的名数据进行验证。验证过程会对名数据和原始数据进行比对,如果两者一致,则验证通过,否则验证失败。 .NET提供了一些类库来处理证书的签验操作,主要包括X509Certificate2类和RSACryptoServiceProvider类。通过X509Certificate2类可以获得证书的私钥和公钥,RSACryptoServiceProvider类可以使用私钥进行名操作,使用公钥进行验证操作。 在进行操作时,可以使用RSACryptoServiceProvider的SignData方法对数据进行名,并指定名算法和哈希算法。生成名数据可以存储为字节数组或Base64字符串,用于传输。 在进行验签操作时,可以使用RSACryptoServiceProvider的VerifyData方法对接收到的名数据和原始数据进行验证。验证结果会返回一个布尔值,表示验证成功或失败。 总之,使用.NET进行证书的签验操作,可以确保数据的完整性和安全性,提供一种可靠的数据传输和验证机制。 ### 回答2: .NET证书签验是指使用.NET框架提供的相关功能对证书进行操作,包括验签两个过程。 是指使用私钥对某个消息进行加密生成数字名,以证明该消息的来源和完整性。首先,我们需要获取私钥所在的证书,并通过.NET中的CryptoAPI类库获取相关的私钥信息。然后,使用私钥对消息进行加密操作,生成数字名。过程中,一般还涉及到对消息进行哈希运算以确保消息的完整性。 验签是指使用公钥对收到的数字名和消息进行解和验证,以确认消息的来源和完整性。验签的过程中,我们需要获取公钥所在的证书,并通过.NET中的CryptoAPI类库获取相关的公钥信息。然后,使用公钥对数字名进行解操作,得到原始消息。接下来,对原始消息进行哈希运算,再与接收到的数字名进行对比,如果完全一致,则可以确认消息的来源和完整性。 在.NET框架中,我们可以使用System.Security.Cryptography命名空间下的相关类实现证书签验的功能。具体而言,可以使用RSACryptoServiceProvider类操作证书私钥和公钥,通过调用它的SignData方法进行操作,调用VerifyData方法进行验签操作。同时,还需要了解证书的获取、存储和管理等相关知识,比如使用X509Certificate2类获取证书信息。 总之,通过.NET框架提供的相关功能,我们可以方便地实现证书的签验操作,以确保消息的可靠性和完整性。 ### 回答3: 在.NET框架中,证书验签是一种常见的数据完整性和安全性保障机制。它基于公钥基础设施(PKI)体系,其中使用了数字证书对数据进行加密和验证。 在进行证书过程中,首先需要获取有效证书。我们可以通过在Windows证书存储中查找和选择合适的证书来完成此操作。然后,需要使用私钥对待的数据进行名。在.NET中,可以使用Cryptography命名空间下的相关类(如RSACryptoServiceProvider)来实现这一步骤。名过程使用私钥对数据进行哈希运算,然后将哈希值使用私钥进行加密生成数字名。最后,将数字名与原始数据一起发送给接收方。 在证书验签过程中,接收方首先需要获取证书的公钥。然后,需要使用公钥对数字名进行解,得到名的哈希值。接下来,接收方对原始数据进行哈希运算,与解得到的哈希值进行比较。如果二者相等,则表明数据的完整性和真实性得到验证,否则证明数据可能被篡改或伪造。 在.NET中,可以使用Cryptography命名空间下的相关类(如RSACryptoServiceProvider)来实现对数字名进行解和数据的哈希运算。在验证过程中,还可以通过对比证书的有效期、证书颁发者等信息来进一步确认证书的可信度。 总结而言,.NET框架提供了一套完善的API和类库,可以方便地实现证书验签操作。通过正确使用证书,可以保障数据的完整性和安全性,确保数据在传输和存储过程中不被篡改或伪造。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值