代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
证书备忘
作者:
狙击手
/ 发布于
2015/1/12
/
510
package com.sniper.survey.util.coder; import java.io.FileInputStream; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.cert.Certificate; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import javax.crypto.Cipher; /** * 证书组建 * * @author sniper * */ public class CertificateCoder { /** * 证书类型 */ private static final String CERT_TYPE = "X.509"; /** * 获取私钥根据 keystore * * @param keyStorePath * @param alias * @param password * @return * @throws Exception */ public static PrivateKey getPrivateKeyByStore(String keyStorePath, String alias, String password) throws Exception { KeyStore ks = getKeyStore(keyStorePath, password); PrivateKey privateKey = (PrivateKey) ks.getKey(alias, password.toCharArray()); return privateKey; } /** * 获取 keystore * * @param keyStorePath * @param password * @return * @throws Exception */ private static KeyStore getKeyStore(String keyStorePath, String password) throws Exception { // 默认是jks KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream is = new FileInputStream(keyStorePath); ks.load(is, password.toCharArray()); is.close(); return ks; } /** * 根据证书获取公钥 * * @param certificatePath * @return * @throws Exception */ public static PublicKey getPublicKeyByCertificate(String certificatePath) throws Exception { Certificate certificate = getCertificate(certificatePath); return certificate.getPublicKey(); } /** * 根据证书路径获取证书 * * @param certificatePath * @return * @throws Exception */ private static Certificate getCertificate(String certificatePath) throws Exception { CertificateFactory certificateFactory = CertificateFactory .getInstance(CERT_TYPE); FileInputStream is = new FileInputStream(certificatePath); Certificate certificate = certificateFactory.generateCertificate(is); is.close(); return certificate; } /** * 获取证书 * * @param keyStorePath * @param alias * @param password * @return * @throws Exception */ private static Certificate getCertificate(String keyStorePath, String alias, String password) throws Exception { KeyStore ks = getKeyStore(keyStorePath, password); return ks.getCertificate(alias); } /** * 私钥加密 * * @param data * @param keyStorePath * @param alias * @param password * @return * @throws Exception */ public static byte[] encryptByPrivateKey(byte[] data, String keyStorePath, String alias, String password) throws Exception { PrivateKey privateKey = getPrivateKeyByStore(keyStorePath, alias, password); Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); } /** * 私钥解密 * * @param data * @param keyStorePath * @param alias * @param password * @return * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] data, String keyStorePath, String alias, String password) throws Exception { PrivateKey privateKey = getPrivateKeyByStore(keyStorePath, alias, password); Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } /** * 公钥加密 * * @param data * @param certificatePath * @return * @throws Exception */ public static byte[] encryptByPublicKey(byte[] data, String certificatePath) throws Exception { PublicKey publicKey = getPublicKeyByCertificate(certificatePath); Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); } /** * 公钥解密 * * @param data * @param certificatePath * @return * @throws Exception */ public static byte[] decryptByPublicKey(byte[] data, String certificatePath) throws Exception { PublicKey publicKey = getPublicKeyByCertificate(certificatePath); Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicKey); return cipher.doFinal(data); } /** * 签名 * * @param sign * @param keyStorePath * @param alias * @param password * @return * @throws Exception */ public static byte[] sign(byte[] sign, String keyStorePath, String alias, String password) throws Exception { X509Certificate certificate = (X509Certificate) getCertificate( keyStorePath, alias, password); Signature signature = Signature .getInstance(certificate.getSigAlgName()); PrivateKey privateKey = getPrivateKeyByStore(keyStorePath, alias, password); signature.initSign(privateKey); signature.update(sign); return signature.sign(); } /** * 验证签名 * * @param data * @param sign * @param certificatePath * @return * @throws Exception */ public static boolean verify(byte[] data, byte[] sign, String certificatePath) throws Exception { X509Certificate x509Certificate = (X509Certificate) getCertificate(certificatePath); Signature signature = Signature.getInstance(x509Certificate .getSigAlgName()); signature.initVerify(x509Certificate); signature.update(data); return signature.verify(sign); } public static void main(String[] args) throws Exception { String password = "sniper"; String alias = "laolang"; String aliasClient = "laolang.client"; String aliasServer = "laolang.server"; String aliasRoot = "laolang.root"; String certificatePath = "/etc/ssl/certs/laolang.server.cer"; String keystorePath = "/etc/ssl/laolang.server.keystore"; String inputStr = "数字证书"; byte[] data = inputStr.getBytes(); // 公钥加密 byte[] decrypt = encryptByPublicKey(data, certificatePath); // 私钥解密 byte[] encrypt = decryptByPrivateKey(decrypt, keystorePath, alias, password); String outStr = new String(encrypt); System.out.println("加密前:\t" + inputStr); System.out.println("加密后:\t" + outStr); } }
试试其它关键字
证书备忘
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
狙击手
贡献的其它代码
(
4
)
.
Ffmpeg 视频截图
.
图片常用操作
.
AES加密算法
.
证书备忘
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3