bouncycastle(3)Learn from others DES
DES - Data Encryption Standard
The most important parameters are as follow: Key, Data, Mode.
A will create the key, publish the key to B, create the encryption data with key, publish the data to B
B will receive the key and the data, decrypt the data with key.
The most import class is as follow:
package com.sillycat.easycastle.encryption;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public abstract class DESCoder extends Coder {
/**
* ALGORITHM
* we can change the algorithm to every value as follow,
* the key size will change according to the algorithm you choose
* <pre>
* DES key size must be equal to 56
* DESede(TripleDES) key size must be equal to 112 or 168
* AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
* Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
* RC2 key size must be between 40 and 1024 bits
* RC4(ARCFOUR) key size must be between 40 and 1024 bits
* </pre>
*/
public static final String ALGORITHM = "DES";
/**
* convert to key
* @param key
* @return
* @throws Exception
*/
private static Key toKey(byte[] key) throws Exception {
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(dks);
// If we want to use other algorithm as AES,Blowfish, Just use the follow statement
// SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
return secretKey;
}
/**
* decrypt the data
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, String key) throws Exception {
Key k = toKey(decryptBASE64(key));
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}
/**
* encrypt
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
Key k = toKey(decryptBASE64(key));
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
/**
* generate the key
*
* @return
* @throws Exception
*/
public static String initKey() throws Exception {
return initKey(null);
}
/**
* generate the key
*
* @param seed
* @return
* @throws Exception
*/
public static String initKey(String seed) throws Exception {
SecureRandom secureRandom = null;
if (seed != null) {
secureRandom = new SecureRandom(decryptBASE64(seed));
} else {
secureRandom = new SecureRandom();
}
KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
kg.init(secureRandom);
SecretKey secretKey = kg.generateKey();
return encryptBASE64(secretKey.getEncoded());
}
}
And the test class is as follow:
package com.sillycat.easycastle.encryption;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class DESCoderTest {
@Test
public void test() throws Exception {
String inputStr = "DES";
String key = DESCoder.initKey();
System.out.println("original:\t" + inputStr);
System.out.println("key:\t" + key);
byte[] inputData = inputStr.getBytes();
inputData = DESCoder.encrypt(inputData, key);
System.out.println("encryption data:\t" + inputData);
System.out.println("encryption data(base64+des):\t" + DESCoder.encryptBASE64(inputData));
byte[] outputData = DESCoder.decrypt(inputData, key);
String outputStr = new String(outputData);
System.out.println("decryption data:\t" + outputStr);
assertEquals(inputStr, outputStr);
}
}
This project is host by github and project name is easycastle.
references:
http://snowolf.iteye.com/blog/380034
bouncycastle(3)Learn from others DES
猜你喜欢
转载自sillycat.iteye.com/blog/1468900
今日推荐
周排行