java签名证书
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; public class CertificateCoder { public static final String CERT_TYPE="X.509"; /**
* 获取私匙
* @param keyStorePath
* @param pwd
* @param alias
* @return PrivateKey 私匙
* @throws Exception
*/
private static PrivateKey getPrivateKey(String keyStorePath,String pwd,String alias) throws Exception{
KeyStore ks=getKeyStore(keyStorePath, pwd);
return (PrivateKey)ks.getKey(alias, pwd.toCharArray()); } /**
*
* @param keyStorePath
* @param pwd
* @return keyStore 密匙库
* @throws Exception
*/
private static KeyStore getKeyStore(String keyStorePath,String pwd) throws Exception{
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream in=new FileInputStream(keyStorePath);
ks.load(in,pwd.toCharArray());
in.close();
return ks;
} /**
*
* @param certificatePath
* @return Certificate 证书
* @throws Exception
*/
private static Certificate getCertificate(String certificatePath) throws Exception{
CertificateFactory factory=CertificateFactory.getInstance(CERT_TYPE);
FileInputStream in=new FileInputStream(certificatePath);
Certificate certificate=factory.generateCertificate(in);
in.close();
return certificate; } /**
* 通过证书返回公匙
* @param certificatePath
* @return Publickey 返回公匙
* @throws Exception
*/
private static PublicKey getPublicKeyByCertificate(String certificatePath) throws Exception{
Certificate certificate=getCertificate(certificatePath);
return certificate.getPublicKey();
} /**
*
* @param keyStorePath
* @param alias
* @param pwd
* @return Certificate 证书
* @throws Exception
*/
private static Certificate getCertificate(String keyStorePath,String alias,String pwd) throws Exception{
KeyStore ks=getKeyStore(keyStorePath, pwd);
//获取证书
return ks.getCertificate(alias);
} /**
* 私匙加密
* @param data
* @param keyStorePath
* @param alias
* @param pwd
* @return byte[] 被私匙加密的数据
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data,String keyStorePath,String alias,String pwd) throws Exception{
PrivateKey privateKey=getPrivateKey(keyStorePath, pwd, alias);
//对数据进行加密
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data); } /**
* 私匙解密
* @param data
* @param keyStorePath
* @param alias
* @param pwd
* @return byte[] 私匙解密的数据
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data,String keyStorePath,String alias,String pwd) throws Exception{
PrivateKey privateKey=getPrivateKey(keyStorePath, pwd, alias);
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
} /**
* 公匙加密
* @param data
* @param cerPath
* @return byte[] 被公匙加密的数据
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data,String cerPath) throws Exception{
//获取公匙
PublicKey publicKey=getPublicKeyByCertificate(cerPath);
System.out.println(publicKey.getAlgorithm());
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
} /**
* 公匙解密
* @param data
* @param cerPath
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data,String cerPath) throws Exception{
PublicKey publicKey=getPublicKeyByCertificate(cerPath);
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
} /**
* 签名
* @param sign
* @param keyStorePath
* @param pwd
* @param alias
* @return
* @throws Exception
*/
public static byte[] sign(byte[] sign,String keyStorePath,String pwd,String alias) throws Exception{
//获取证书
X509Certificate x509=(X509Certificate)getCertificate(keyStorePath, alias, pwd);
//构建签名,由证书指定签名算法
Signature sa=Signature.getInstance(x509.getSigAlgName());
//获取私匙
PrivateKey privateKey=getPrivateKey(keyStorePath, pwd, alias);
sa.initSign(privateKey);
sa.update(sign);
return sa.sign();
} /**
* 验证签名
* @param data
* @param sign
* @param cerPath
* @return
* @throws Exception
*/
public static boolean verify(byte[] data,byte[] sign,String cerPath) throws Exception{
X509Certificate x509=(X509Certificate)getCertificate(cerPath);
Signature sa=Signature.getInstance(x509.getSigAlgName());
sa.initVerify(x509);
sa.update(data);
return sa.verify(sign);
}
}
java签名证书的更多相关文章
- Java代码签名证书申请和使用指南
第1步 下载签名工具 Step 1: Download Signing Tools 如果您还没有签名工具,请到SUN公司网站免费下载:http://java.sun.com/j2se/,推荐下载JDK ...
- java keytool证书工具使用小结
java keytool证书工具使用小结 在Security编程中,有几种典型的密码交换信息文件格式: DER-encoded certificate: .cer, .crt PEM-encod ...
- 修改Android签名证书keystore的密码、别名alias以及别名密码
Eclipse ADT的Custom debug keystore自定义调试证书的时候,Android应用开发接入各种SDK时会发现,有很多SDK是需要靠package name和keystore的指 ...
- cmd命令生成android签名证书
cmd命令生成android签名证书,有空在写一篇eclipse导出带签名的apk,这里面包括生成新的签名.现在还是讲讲在cmd怎么操作生成签名证书. 1.dos下进入JDK的bin目录 运行如下命令 ...
- springboot配置SSL自签名证书
1.证书生成 每一个JDK或者JRE里都有一个工具,叫做:keytool,安装了jdk或jre之后,配置好JAVA环境之后,就可以直接在控制台使用该命令生成自签名证书: 在控制台输入: keytool ...
- java keytool证书工具使用小结【转】
java keytool证书工具使用小结 keytool导入导出多条目对比 在Security编程中,有几种典型的密码交换信息文件格式: DER-encoded certificate: .cer, ...
- 使用Symantec代码签名证书对代码进行签名的 5 个理由
借助 Symantec Code Signing,在更多平台上将您的代码提供给更多客户,我们总结了5大理由告诉软件开发者在发布自己的软件时一定要购买Symantec 代码签名证书签名即将发布的软件. ...
- 用tomcat配置https自签名证书,解决 ios7.1以上系统, 苹果inHouse发布
用tomcat配置https自签名证书,解决 ios7.1以上系统苹果inHouse发布不能下载安装的问题教程,话说,我其实最讨厌配置某某环境了,因为某一个小环节一旦出错,你的所有工作往往会功亏一篑, ...
- java keytool证书工具使用小结(转)
Keytool 是一个Java数据证书的管理工具 ,Keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里,包含两种数据:密钥实体(K ...
随机推荐
- Windows下Postgre SQL数据库通过Slony-I 实现数据库双机同步备份
一. 我们要实现的环境是windows xp.windows2003上安装Postgre SQL数据库,实现目的是两台数据库服务器进行数据库同步,即数据库同步更新.删除.插入等对数据库的操作. 二. ...
- ios中框架介绍
ios中框架介绍 参考博客: 参考文章:框架介绍 框架介绍 框架就是一个目录,一个目录包含了共享库,访问共享库里面的代码的头文件,和其他的图片和声音的资源文件.一个共享库定义的方法和函数可以被应用程序 ...
- 从页面底部向上弹出dialog,消失时逐渐向下(转)
我想实现一个效果,从底部向上逐渐弹出.如下图所示: 1.点击 显示 按钮时,一个dialog对话框从底部慢慢向上弹出. 2.关闭dialog时, dialog缓慢的移动向底部消失.很平滑的效果. ...
- 传感器 Sensor 加速度【示例】
简介 坐标系 x轴:从左到右 y轴:从下到上 z轴:从内到外 这个坐标系与Android 2D API中的不同,传感器中的返回值都以此坐标系为准. SENSOR_TYPE_ACCELEROMETER ...
- codevs 1994 排队 排列组合+高精度
/* 数学题0.0 最后答案:A(n,n)*A(n+1,2)*A(n+3,m)+A(n,n)*C(m,1)*A(2,2)*C(n+1,1)*A(n+2,m-1); 简单解释一下 +之前的很显然 先排男 ...
- css渐变色
<!DOCTYPE html><html><head> <meta http-equiv="content-type" content=& ...
- GridView的初级使用
使用GridView自带的分页功能,需要激发PageIndexChanging protected void gvNewsList_PageIndexChanging(object sender, G ...
- html table 知识点
第一点就是图片在表格中不能对齐边沿的问题,这是和浏览器的渲染模式相关的,在标准模式中是不能对齐的,除非改变CSS某些样式.而在准标准模式和怪异模式中,默认就是对齐的. 这一特性在以前的表格布局时代是大 ...
- Css简介
- Python os常用模块
Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Wi ...