- 浏览: 2491862 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
bouncycastle(7)Learn from others DSA
DSA (Digital Signature Algorithm)
A will generate the key pair, share the public key to B.
A will use private key to generate the signature, send the signature and data to B.
B will use public key to validate the signature and data.
package com.sillycat.easycastle.encryption;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public abstract class DSACoder extends Coder {
public static final String ALGORITHM = "DSA";
/**
* default key size
* <pre>
* DSA
* Default Keysize 1024
* Keysize must be a multiple of 64, ranging from 512 to 1024 (inclusive).
* </pre>
*/
private static final int KEY_SIZE = 1024;
/**
* default seed
*/
private static final String DEFAULT_SEED = "0f22507a10bbddd07d8a3082122966e3";
private static final String PUBLIC_KEY = "DSAPublicKey";
private static final String PRIVATE_KEY = "DSAPrivateKey";
/**
* use private key to signature the data
* @param data
* @param privateKey
*
* @return
* @throws Exception
*/
public static String sign(byte[] data, String privateKey) throws Exception {
// decrypt the private key
byte[] keyBytes = decryptBASE64(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
// KEY_ALGORITHM
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
// convert to private key object
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
// signature the data
Signature signature = Signature.getInstance(keyFactory.getAlgorithm());
signature.initSign(priKey);
signature.update(data);
return encryptBASE64(signature.sign());
}
/**
* validate the signature
* @param data
* @param publicKey
* @param sign
* @return true false
* @throws Exception
*/
public static boolean verify(byte[] data, String publicKey, String sign)
throws Exception {
byte[] keyBytes = decryptBASE64(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
// ALGORITHM
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
// convert to public key object
PublicKey pubKey = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(keyFactory.getAlgorithm());
signature.initVerify(pubKey);
signature.update(data);
return signature.verify(decryptBASE64(sign));
}
/**
* generate the key pair
* @param seed
* @return
* @throws Exception
*/
public static Map<String, Object> initKey(String seed) throws Exception {
KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = new SecureRandom();
secureRandom.setSeed(seed.getBytes());
keygen.initialize(KEY_SIZE, secureRandom);
KeyPair keys = keygen.genKeyPair();
DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic();
DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate();
Map<String, Object> map = new HashMap<String, Object>(2);
map.put(PUBLIC_KEY, publicKey);
map.put(PRIVATE_KEY, privateKey);
return map;
}
/**
* return key map object
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
return initKey(DEFAULT_SEED);
}
/**
* return private key
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return encryptBASE64(key.getEncoded());
}
/**
* return public key
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return encryptBASE64(key.getEncoded());
}
}
The test case will be as follow:
package com.sillycat.easycastle.encryption;
importstatic org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.Test;
publicclass DSACoderTest {
@Test
publicvoid test() throws Exception {
String inputStr = "hello world";
byte[] data = inputStr.getBytes();
// init the key pair
Map<String, Object> keyMap = DSACoder.initKey();
// get the public and private key
String publicKey = DSACoder.getPublicKey(keyMap);
String privateKey = DSACoder.getPrivateKey(keyMap);
System.out.println("public key:\r" + publicKey);
System.out.println("private key:\r" + privateKey);
// generate the signature
String sign = DSACoder.sign(data, privateKey);
System.out.println("signature:\r" + sign);
// validate the signature
boolean status = DSACoder.verify(data, publicKey, sign);
System.out.println("status:\r" + status);
assertTrue(status);
}
}
references:
http://snowolf.iteye.com/blog/382749
DSA (Digital Signature Algorithm)
A will generate the key pair, share the public key to B.
A will use private key to generate the signature, send the signature and data to B.
B will use public key to validate the signature and data.
package com.sillycat.easycastle.encryption;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public abstract class DSACoder extends Coder {
public static final String ALGORITHM = "DSA";
/**
* default key size
* <pre>
* DSA
* Default Keysize 1024
* Keysize must be a multiple of 64, ranging from 512 to 1024 (inclusive).
* </pre>
*/
private static final int KEY_SIZE = 1024;
/**
* default seed
*/
private static final String DEFAULT_SEED = "0f22507a10bbddd07d8a3082122966e3";
private static final String PUBLIC_KEY = "DSAPublicKey";
private static final String PRIVATE_KEY = "DSAPrivateKey";
/**
* use private key to signature the data
* @param data
* @param privateKey
*
* @return
* @throws Exception
*/
public static String sign(byte[] data, String privateKey) throws Exception {
// decrypt the private key
byte[] keyBytes = decryptBASE64(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
// KEY_ALGORITHM
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
// convert to private key object
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
// signature the data
Signature signature = Signature.getInstance(keyFactory.getAlgorithm());
signature.initSign(priKey);
signature.update(data);
return encryptBASE64(signature.sign());
}
/**
* validate the signature
* @param data
* @param publicKey
* @param sign
* @return true false
* @throws Exception
*/
public static boolean verify(byte[] data, String publicKey, String sign)
throws Exception {
byte[] keyBytes = decryptBASE64(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
// ALGORITHM
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
// convert to public key object
PublicKey pubKey = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(keyFactory.getAlgorithm());
signature.initVerify(pubKey);
signature.update(data);
return signature.verify(decryptBASE64(sign));
}
/**
* generate the key pair
* @param seed
* @return
* @throws Exception
*/
public static Map<String, Object> initKey(String seed) throws Exception {
KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = new SecureRandom();
secureRandom.setSeed(seed.getBytes());
keygen.initialize(KEY_SIZE, secureRandom);
KeyPair keys = keygen.genKeyPair();
DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic();
DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate();
Map<String, Object> map = new HashMap<String, Object>(2);
map.put(PUBLIC_KEY, publicKey);
map.put(PRIVATE_KEY, privateKey);
return map;
}
/**
* return key map object
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
return initKey(DEFAULT_SEED);
}
/**
* return private key
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return encryptBASE64(key.getEncoded());
}
/**
* return public key
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return encryptBASE64(key.getEncoded());
}
}
The test case will be as follow:
package com.sillycat.easycastle.encryption;
importstatic org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.Test;
publicclass DSACoderTest {
@Test
publicvoid test() throws Exception {
String inputStr = "hello world";
byte[] data = inputStr.getBytes();
// init the key pair
Map<String, Object> keyMap = DSACoder.initKey();
// get the public and private key
String publicKey = DSACoder.getPublicKey(keyMap);
String privateKey = DSACoder.getPrivateKey(keyMap);
System.out.println("public key:\r" + publicKey);
System.out.println("private key:\r" + privateKey);
// generate the signature
String sign = DSACoder.sign(data, privateKey);
System.out.println("signature:\r" + sign);
// validate the signature
boolean status = DSACoder.verify(data, publicKey, sign);
System.out.println("status:\r" + status);
assertTrue(status);
}
}
references:
http://snowolf.iteye.com/blog/382749
发表评论
-
Update Site will come soon
2021-06-02 04:10 1617I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 383Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 380Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 335Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 405VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 304Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 347PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 605Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 235Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 260Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 206MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 254MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 290Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 275Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 294Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 242Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 292K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 329Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 408Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 339Redis Cluster 2019(3)Redis Clus ...
相关推荐
借助 BouncyCastle 在C#里 实现Java中的PKCS7 签名和验签 仅做参考,如有问题欢迎指出。
bouncycastle jar包
BouncyCastle是JAVA专属库,但出来了C#的库。这个非常实用。仅仅一个dll文件
BouncyCastle的相关资源包,若生成x509证书则需要此资源包,里面包含两个,版本是1.62
BouncyCastle 加密算法.net组件 版本10.0
org.bouncycastle完整资源包,有需要的可以下
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.6.
解决org/bouncycastle/jce/provider/bouncycastlepr错误专用。
使用C# 版本 BouncyCastle 包 ,实现带原文数据PKCS#7 签名。 此版本请不要下载,只包含签名部分。
org.bouncycastle 加密算法包,csdn上很多这个包的下载都不全,我这完整包里包含了9个jar包 1.69官网最新版本
bouncy castle(轻量级密码术包)是一种用于 Java 平台的开放源码的轻量...因为 Bouncy Castle 被设计成轻量级的,所以从 J2SE 1.4 到 J2ME(包括 MIDP)平台,它都可以运行。它是在 MIDP 上运行的唯一完整的密码术包。
BouncyCastle dll库 1.8.6版
bouncy castle(轻量级密码术包)是一种用于 Java 平台的开放源码的轻量级密码术包。它支持大量的密码术算法,并提供 JCE 1.2.1 的实现。
Bouncy Castle Crypto.dll是一个C#实现的加密包。一款加解密的工具类,可以在C#程序中添加依赖项。
bouncycastle源码学习,下载源码,很好用