bouncycastle(2)Learn from others BASE64 MD5 SHA HMAC

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

猜你喜欢

转载自sillycat.iteye.com/blog/1468897
今日推荐