- 浏览: 2551646 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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(2)Learn from others BASE64 MD5 SHA HMAC
I read the blog written by others. They summarize the encryption and decryption things in java. So I start to read their blog and learn from them about JCE.
Single Side Encryption
BASE64 encoding method, not encryption
MD5(Message Digest algorithm 5)
SHA(Secure Hash Algorithm)
HMAC(Hash Message Authentication Code)
Symmetry Side Encryption
DES(Data Encryption Standard)
PBE(Password-based encryption)
RSA(The authors are as follow: Ron Rivest, AdiShamir, Leonard Adleman)
DH(Diffie-Hellman)
DSA(Digital Signature Algorithm)
ECC(Elliptic Curves Cryptography)
BASE64
The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.
A can encrypt the data, and send to B. B will decrypt the data directly.
This is the tool class as follow:
/**
* BASE64 decryption
*
* @param source
* @return
* @throws Exception
*/
publicstatic String decryptBASE64(String source) throws Exception {
byte[] return_source = (new BASE64Decoder()).decodeBuffer(source);
returnnew String(return_source);
}
/**
* BASE64 encryption
*
* @param source
* @return
* @throws Exception
*/
publicstatic String encryptBASE64(String source) throws Exception {
byte[] key = source.getBytes();
return (new BASE64Encoder()).encodeBuffer(key);
}
And the test class named Base64UtilTest will be as follow:
@Test
publicvoid encryptAndDecryptBASE64() throws Exception{
String source = "hello,sillycat";
String return_str = Base64Util.encryptBASE64(source);
Assert.assertTrue(!source.equals(return_str));
String source_str = Base64Util.decryptBASE64(return_str);
Assert.assertEquals(source, source_str);
}
Base64 encode the data into 8 * n bytes, if some empty blanks are there, we will use =, for example, aGVsbG8sc2lsbHljYXQ=.
MD5
MD5--- message-digest algorithm 5. It is used to verify if the file is completed downloaded.
A will share the algorithm to B, for example, MD5, SHA, SHA1, SHA-1. And A will share the encrypt data to B. But B will get the algorithm first, and then, B will decrypt the data with algorithm.
The implementation is as follow:
// MD5, SHA, SHA1, SHA-1
privatestaticfinal String KEY_MD5 = "MD5";
publicstatic String encryptMD5(String source) throws Exception {
byte[] data = source.getBytes();
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
byte[] return_data = md5.digest();
returnnew String(return_data);
}
The test implementation will be as follow:
@Test
publicvoid encryptMD5() throws Exception{
String source = "welcome to this world.";
String return_str = MD5Util.encryptMD5(source);
Assert.assertTrue(!source.equals(return_str));
System.out.println(return_str);
}
SHA
The process and flows are mostly like the MD5, so does the implementation.
HMAC
Hash Message Authentication Code.
A will generate the key first, share the key with B. A will encrypt the data with key, send the data to B.
B will validate the data with key.
The tool implementation will be as follow:
//HMACSHA1,HmacMD5
privatefinalstatic String KEY_MAC = "HMACSHA1";
/**
* HMAC key
*
* @return
* @throws Exception
*/
publicstatic String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return Base64Util.encryptBASE64(new String(secretKey.getEncoded()));
}
/**
* HMAC encrypt
*
* @param data
* @param key
* @return
* @throws Exception
*/
publicstatic String encryptHMAC(String source, String key) throws Exception {
byte[] data = source.getBytes();
SecretKey secretKey = new SecretKeySpec(Base64Util.decryptBASE64(key)
.getBytes(), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
byte[] return_data = mac.doFinal(data);
returnnew String(return_data);
}
The test implementation will be as follow:
@Test
publicvoid encryptHMAC() throws Exception{
String key = HMACUtil.initMacKey();
System.out.println("key will be: " + key);
String return_str = HMACUtil.encryptHMAC("hello world", key);
System.out.println("return string will be: " + return_str);
}
From the blog written by others, the class coder will be as follow
package com.sillycat.easycastle.encryption;
import java.security.MessageDigest;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
publicabstractclass Coder {
publicstaticfinal String KEY_SHA = "SHA";
publicstaticfinal String KEY_MD5 = "MD5";
/**
* MAC算法可选以下多种算法
*
* <pre>
* HmacMD5
* HmacSHA1
* HmacSHA256
* HmacSHA384
* HmacSHA512
* </pre>
*/
publicstaticfinal String KEY_MAC = "HmacMD5";
/**
* BASE64解密
*
* @param key
* @return
* @throws Exception
*/
publicstaticbyte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
publicstatic String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* MD5加密
*
* @param data
* @return
* @throws Exception
*/
publicstaticbyte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
/**
* SHA加密
*
* @param data
* @return
* @throws Exception
*/
publicstaticbyte[] encryptSHA(byte[] data) throws Exception {
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
sha.update(data);
return sha.digest();
}
/**
* 初始化HMAC密钥
*
* @return
* @throws Exception
*/
publicstatic String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return encryptBASE64(secretKey.getEncoded());
}
/**
* HMAC加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
publicstaticbyte[] encryptHMAC(byte[] data, String key) throws Exception {
SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(data);
}
}
And his test class will be as follow:
package com.sillycat.easycastle.encryption;
importstatic org.junit.Assert.*;
import java.math.BigInteger;
import org.junit.Test;
publicclass CoderTest {
@Test
publicvoid test() throws Exception {
String inputStr = "简单加密";
System.out.println("原文:" + inputStr);
byte[] inputData = inputStr.getBytes();
String code = Coder.encryptBASE64(inputData);
System.out.println("BASE64加密后:" + code);
byte[] output = Coder.decryptBASE64(code);
String outputStr = new String(output);
System.out.println("BASE64解密后:" + outputStr);
// 验证BASE64加密解密一致性
assertEquals(inputStr, outputStr);
// 验证MD5对于同一内容加密是否一致
assertArrayEquals(Coder.encryptMD5(inputData),
Coder.encryptMD5(inputData));
// 验证SHA对于同一内容加密是否一致
assertArrayEquals(Coder.encryptSHA(inputData),
Coder.encryptSHA(inputData));
String key = Coder.initMacKey();
System.out.println("Mac密钥:" + key);
// 验证HMAC对于同一内容,同一密钥加密是否一致
assertArrayEquals(Coder.encryptHMAC(inputData, key),
Coder.encryptHMAC(inputData, key));
BigInteger md5 = new BigInteger(Coder.encryptMD5(inputData));
System.out.println("MD5:" + md5.toString(16));
BigInteger sha = new BigInteger(Coder.encryptSHA(inputData));
System.out.println("SHA:" + sha.toString(32));
BigInteger mac = new BigInteger(Coder.encryptHMAC(inputData, inputStr));
System.out.println("HMAC:" + mac.toString(16));
}
}
references:
http://snowolf.iteye.com/blog/379860
I read the blog written by others. They summarize the encryption and decryption things in java. So I start to read their blog and learn from them about JCE.
Single Side Encryption
BASE64 encoding method, not encryption
MD5(Message Digest algorithm 5)
SHA(Secure Hash Algorithm)
HMAC(Hash Message Authentication Code)
Symmetry Side Encryption
DES(Data Encryption Standard)
PBE(Password-based encryption)
RSA(The authors are as follow: Ron Rivest, AdiShamir, Leonard Adleman)
DH(Diffie-Hellman)
DSA(Digital Signature Algorithm)
ECC(Elliptic Curves Cryptography)
BASE64
The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.
A can encrypt the data, and send to B. B will decrypt the data directly.
This is the tool class as follow:
/**
* BASE64 decryption
*
* @param source
* @return
* @throws Exception
*/
publicstatic String decryptBASE64(String source) throws Exception {
byte[] return_source = (new BASE64Decoder()).decodeBuffer(source);
returnnew String(return_source);
}
/**
* BASE64 encryption
*
* @param source
* @return
* @throws Exception
*/
publicstatic String encryptBASE64(String source) throws Exception {
byte[] key = source.getBytes();
return (new BASE64Encoder()).encodeBuffer(key);
}
And the test class named Base64UtilTest will be as follow:
@Test
publicvoid encryptAndDecryptBASE64() throws Exception{
String source = "hello,sillycat";
String return_str = Base64Util.encryptBASE64(source);
Assert.assertTrue(!source.equals(return_str));
String source_str = Base64Util.decryptBASE64(return_str);
Assert.assertEquals(source, source_str);
}
Base64 encode the data into 8 * n bytes, if some empty blanks are there, we will use =, for example, aGVsbG8sc2lsbHljYXQ=.
MD5
MD5--- message-digest algorithm 5. It is used to verify if the file is completed downloaded.
A will share the algorithm to B, for example, MD5, SHA, SHA1, SHA-1. And A will share the encrypt data to B. But B will get the algorithm first, and then, B will decrypt the data with algorithm.
The implementation is as follow:
// MD5, SHA, SHA1, SHA-1
privatestaticfinal String KEY_MD5 = "MD5";
publicstatic String encryptMD5(String source) throws Exception {
byte[] data = source.getBytes();
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
byte[] return_data = md5.digest();
returnnew String(return_data);
}
The test implementation will be as follow:
@Test
publicvoid encryptMD5() throws Exception{
String source = "welcome to this world.";
String return_str = MD5Util.encryptMD5(source);
Assert.assertTrue(!source.equals(return_str));
System.out.println(return_str);
}
SHA
The process and flows are mostly like the MD5, so does the implementation.
HMAC
Hash Message Authentication Code.
A will generate the key first, share the key with B. A will encrypt the data with key, send the data to B.
B will validate the data with key.
The tool implementation will be as follow:
//HMACSHA1,HmacMD5
privatefinalstatic String KEY_MAC = "HMACSHA1";
/**
* HMAC key
*
* @return
* @throws Exception
*/
publicstatic String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return Base64Util.encryptBASE64(new String(secretKey.getEncoded()));
}
/**
* HMAC encrypt
*
* @param data
* @param key
* @return
* @throws Exception
*/
publicstatic String encryptHMAC(String source, String key) throws Exception {
byte[] data = source.getBytes();
SecretKey secretKey = new SecretKeySpec(Base64Util.decryptBASE64(key)
.getBytes(), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
byte[] return_data = mac.doFinal(data);
returnnew String(return_data);
}
The test implementation will be as follow:
@Test
publicvoid encryptHMAC() throws Exception{
String key = HMACUtil.initMacKey();
System.out.println("key will be: " + key);
String return_str = HMACUtil.encryptHMAC("hello world", key);
System.out.println("return string will be: " + return_str);
}
From the blog written by others, the class coder will be as follow
package com.sillycat.easycastle.encryption;
import java.security.MessageDigest;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
publicabstractclass Coder {
publicstaticfinal String KEY_SHA = "SHA";
publicstaticfinal String KEY_MD5 = "MD5";
/**
* MAC算法可选以下多种算法
*
* <pre>
* HmacMD5
* HmacSHA1
* HmacSHA256
* HmacSHA384
* HmacSHA512
* </pre>
*/
publicstaticfinal String KEY_MAC = "HmacMD5";
/**
* BASE64解密
*
* @param key
* @return
* @throws Exception
*/
publicstaticbyte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
publicstatic String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* MD5加密
*
* @param data
* @return
* @throws Exception
*/
publicstaticbyte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
/**
* SHA加密
*
* @param data
* @return
* @throws Exception
*/
publicstaticbyte[] encryptSHA(byte[] data) throws Exception {
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
sha.update(data);
return sha.digest();
}
/**
* 初始化HMAC密钥
*
* @return
* @throws Exception
*/
publicstatic String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return encryptBASE64(secretKey.getEncoded());
}
/**
* HMAC加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
publicstaticbyte[] encryptHMAC(byte[] data, String key) throws Exception {
SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(data);
}
}
And his test class will be as follow:
package com.sillycat.easycastle.encryption;
importstatic org.junit.Assert.*;
import java.math.BigInteger;
import org.junit.Test;
publicclass CoderTest {
@Test
publicvoid test() throws Exception {
String inputStr = "简单加密";
System.out.println("原文:" + inputStr);
byte[] inputData = inputStr.getBytes();
String code = Coder.encryptBASE64(inputData);
System.out.println("BASE64加密后:" + code);
byte[] output = Coder.decryptBASE64(code);
String outputStr = new String(output);
System.out.println("BASE64解密后:" + outputStr);
// 验证BASE64加密解密一致性
assertEquals(inputStr, outputStr);
// 验证MD5对于同一内容加密是否一致
assertArrayEquals(Coder.encryptMD5(inputData),
Coder.encryptMD5(inputData));
// 验证SHA对于同一内容加密是否一致
assertArrayEquals(Coder.encryptSHA(inputData),
Coder.encryptSHA(inputData));
String key = Coder.initMacKey();
System.out.println("Mac密钥:" + key);
// 验证HMAC对于同一内容,同一密钥加密是否一致
assertArrayEquals(Coder.encryptHMAC(inputData, key),
Coder.encryptHMAC(inputData, key));
BigInteger md5 = new BigInteger(Coder.encryptMD5(inputData));
System.out.println("MD5:" + md5.toString(16));
BigInteger sha = new BigInteger(Coder.encryptSHA(inputData));
System.out.println("SHA:" + sha.toString(32));
BigInteger mac = new BigInteger(Coder.encryptHMAC(inputData, inputStr));
System.out.println("HMAC:" + mac.toString(16));
}
}
references:
http://snowolf.iteye.com/blog/379860
发表评论
-
Update Site will come soon
2021-06-02 04:10 1677I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 430Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 435Private 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 294Hadoop 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 ...
相关推荐
此外,Bouncy Castle还提供了数据压缩和各种编码方式的实现,如Base64、Hex编码等。 **使用Bouncy Castle** 在项目中引入Bouncy Castle jar包后,可以通过以下方式使用: 1. 添加依赖到项目类路径,例如在Maven...
1. **bcprov-jdk15on-1.62.jar**:这是BouncyCastle的主要加密提供者包,提供了广泛的加密算法实现,如RSA、AES、DES、SHA等。它支持Java 1.5及更高版本,因此名称中的"jdk15on"表示这个版本适用于Java 1.5及以上。...
Bouncy Castle提供了多种哈希算法,如MD5、SHA-1和SHA-2系列,其中SHA-256具有更高的安全性。 5. **消息认证码(MAC)**:MAC结合了哈希函数和密钥,提供了一种验证数据完整性的方法。Bouncy Castle支持HMAC(基于...
Bouncy Castle 支持多种加密算法,包括对称加密(如 AES、DES、3DES)、非对称加密(RSA、DSA、ECDSA)、哈希函数(MD5、SHA-1、SHA-256 及以上)、消息认证码(MAC)等。这些算法的实现使得开发人员能够灵活选择...
哈希函数如MD5和SHA系列,可以将任意长度的消息转换为固定长度的摘要,用于数据完整性检查。而MAC,如HMAC(基于哈希的消息认证码),结合密钥提供更高级别的数据完整性与来源验证。 4. **数字签名**: Bouncy...
5. 如果你需要与Java bouncycastle保持兼容,确保在编码和解码时使用相同的格式,例如Base64编码: ```python import base64 # 加密前将明文转换为Base64 plain_text_base64 = base64.b64encode(plain_text.encode...
6. **签名与哈希**:支持多种签名算法(如RSA、DSA、ECDSA)和哈希算法(如SHA-1、SHA-256、MD5等),可用于数字签名、消息认证码(MAC)等场景。 7. **TLS/SSL协议实现**:`bcprov`模块提供了轻量级的TLS/SSL协议...
这个库是BouncyCastle项目的一部分,它是一个开源的密码学API,支持多种加密算法和标准,如RSA、AES、DES、SHA、HMAC等。BouncyCastle库分为两个部分:轻量级加密API(Lightweight Cryptography API)和PKCS(Public...
5. **序列化PKCS#7结构**:将封装好的PKCS#7结构转换为字节数组或Base64字符串,以便在网络上传输或存储。 6. **验证签名**: - 接收到PKCS#7签名后,首先将其反序列化为`Pkcs7SignedData`对象。 - 使用`...
2. **广泛的密码学算法**:BouncyCastle 支持众多加密算法,如AES、DES、3DES、Blowfish、RSA、DSA、ECDSA等,还包括哈希函数如SHA-1、SHA-256、MD5等,以及消息认证码(MAC)和伪随机数生成器(PRNG)。 3. **PKCS...
Bouncy Castle 提供了包括 SHA-1、SHA-256、SHA-512 等在内的多种哈希函数,以及 DSA、RSA 等签名算法的实现。 4. **证书和证书路径验证**: 在 PKI 中,X.509 证书用于存储公钥和身份信息。Bouncy Castle 提供了...
BouncyCastle.Crypto.dll是.NET版本的核心组件,包含了大量加密算法的实现,如对称加密(如AES、DES、3DES)、非对称加密(RSA、DSA、ECC)、哈希算法(MD5、SHA-1、SHA-256等)以及消息认证码(MAC)等。...
BouncyCastle是一个广泛使用的开源加密库,支持多种加密算法,包括非对称加密、对称加密、哈希函数等。这个例子主要关注的是在中国广泛采用的国密标准——SM2、SM3和SM4算法的应用。 首先,让我们深入理解一下这些...
2. **lcrypto-j2me-138.jar**:这个文件是BouncyCastle针对Java ME(Micro Edition)的版本。"lcrypto"可能代表“Lightweight Crypto”,强调了该版本适用于资源有限的设备。"j2me"表明它是Java 2 Micro Edition的...
《BouncyCastle.Crypto.dll:理解.NET平台的加密利器》 在网络安全日益重要的今天,加密技术扮演了至关重要的角色。BouncyCastle库是Java和.NET平台上广泛使用的加密库之一,而"BouncyCastle.Crypto.dll"正是.NET...
3. **Crypto**:Bouncy Castle的Crypto模块包含了许多加密和哈希算法,如椭圆曲线加密(ECC)、DSA(Digital Signature Algorithm)、MD5、SHA-1、SHA-256等。此外,它还支持PKCS#7、PKCS#8、OpenPGP等相关标准,...