- 浏览: 2552175 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 1679I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 431Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 455VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 356Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 405PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 720Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 295Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 295Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 241MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 294MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 325Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 312Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 332Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 283Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 326K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 362Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 444Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 373Redis Cluster 2019(3)Redis Clus ...
相关推荐
BouncyCastle是一个强大的Java安全库,它为加密、数字签名、证书处理以及许多其他安全功能提供了全面的支持。在Android开发中,BouncyCastle扮演着重要角色,特别是在处理SSL/TLS连接、加密通信以及生成和验证X.509...
BouncyCastle是一个强大的开源加密库,它支持多种加密算法和标准,包括PKCS#7(也称为CMS,Cryptographic Message Syntax)。 PKCS#7是一种标准格式,用于打包和加密数字签名、证书和数据。它允许在一个单一的结构...
Bouncy Castle实现了PKCS#7(用于数据封装和签名)、PKCS#12(用于存储私钥和证书)、PKCS#5(用于密码化)和PKCS#11(用于硬件加密设备接口)等标准。 **OpenSSL兼容** Bouncy Castle还提供了一个名为BCrypt的...
本文将详细讲解如何使用C#语言和BouncyCastle库来实现带原文数据的PKCS#7签名。 PKCS#7(Public-Key Cryptography Standards #7)是由RSA Security提出的一种标准,它定义了证书、证书撤销列表(CRL)的格式以及...
BouncyCastle提供了实现RSA和DSA(数字签名算法)等数字签名功能,使得信息不能被篡改且能验证发送者的身份。 5. **PKCS标准**: 支持PKCS#7(用于封装和签名数据)和PKCS#12(个人证书存储格式),方便导入和...
Bouncy Castle 实现了 PKCS#1、PKCS#5、PKCS#7、PKCS#8 和 PKCS#12 等标准,这些标准涉及密钥管理、密码存储和数据封装等。 9. **JCE 兼容性**: 作为 JCE 的扩展,Bouncy Castle 可以作为替代提供更丰富的加密...
1. **加密算法支持**:BouncyCastle.Crypto.dll 提供了多种加密算法的实现,包括对称加密(如AES、Blowfish、DES)、非对称加密(如RSA、DSA、ECDSA)、哈希函数(如SHA-1、SHA-256、MD5)和消息认证码(MAC)。...
《BouncyCastle.Crypto.dll:理解.NET平台的加密利器》 在网络安全日益重要的今天,加密技术扮演了至关重要的角色。BouncyCastle库是Java和.NET平台上广泛使用的加密库之一,而"BouncyCastle.Crypto.dll"正是.NET...
7. **随机数生成器**:加密过程中需要使用高质量的随机数,Bouncy Castle提供了安全的随机数生成器。 在实际应用中,开发人员可以通过引用BouncyCastle.Crypto.dll来利用这些功能。通过它的API,可以轻松地集成加密...
2. **广泛的密码学算法**:BouncyCastle 支持众多加密算法,如AES、DES、3DES、Blowfish、RSA、DSA、ECDSA等,还包括哈希函数如SHA-1、SHA-256、MD5等,以及消息认证码(MAC)和伪随机数生成器(PRNG)。 3. **PKCS...
BouncyCastle.Crypto.dll是.NET版本的核心组件,包含了大量加密算法的实现,如对称加密(如AES、DES、3DES)、非对称加密(RSA、DSA、ECC)、哈希算法(MD5、SHA-1、SHA-256等)以及消息认证码(MAC)等。...
Bouncy Castle 支持多种加密算法,包括对称加密(如 AES、DES、3DES)、非对称加密(RSA、DSA、ECDSA)、哈希函数(MD5、SHA-1、SHA-256 及以上)、消息认证码(MAC)等。这些算法的实现使得开发人员能够灵活选择...
《BouncyCastle.Crypto.dll 1.8.2:深入解析加密库的奥秘》 在信息技术领域,安全是至关重要的。特别是在网络通信、数据存储和传输等方面,强大的加密技术是保障信息安全的基础。BouncyCastle.Crypto.dll是这样一个...
《BouncyCastle1.59帮助文档:深入理解与CHM制作详解》 BouncyCastle,作为Java和.NET平台上广泛使用的开源加密库,为开发者提供了丰富的加密算法、密码学标准接口以及证书处理功能。这份“BouncyCastle1.59帮助...
1. **加密算法支持**:`org.bouncycastle`提供了大量的对称和非对称加密算法,例如AES(高级加密标准)、Blowfish、DES(数据加密标准)、3DES(三重DES)、RSA(公钥加密标准)、DSA(数字签名算法)等。...
BouncyCastle是JAVA专属库,但出来了C#的库。这个非常实用。仅仅一个dll文件
**Bouncy Castle 加密开源组件API Java Release 1.46** 是一个广泛使用的加密库,尤其在Java开发中占据重要地位。Bouncy Castle 提供了丰富的加密算法、密钥生成器、证书处理以及PKI(Public Key Infrastructure,...