在Java中,对于加密密钥和解密密钥相同的加密我们称之为对称加密,其中主要有DES,3DES和AES加密。
DES加密
- package com.example.asiatravel.learndes.util;
- import android.util.Log;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * Created by kuangxiaoguo on 16/9/11.
- *
- * DES加密工具类
- */
- public class DESUtil {
- private static final String TAG = "TAG";
- /**
- * 生成密钥
- */
- public static byte[] initKey() {
- try {
- //KeyGenerator 密钥生成器
- KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
- //初始化密钥生成器
- keyGenerator.init(56);
- //生成密钥
- SecretKey secretKey = keyGenerator.generateKey();
- return secretKey.getEncoded();
- } catch (Exception e) {
- Log.e(TAG, "initKey: " + e.getMessage());
- }
- return null;
- }
- /**
- * DES加密
- *
- * @param data 需要加密的数据
- * @param key 加密使用的密钥
- * @return 加密后获取的字节数组
- */
- public static byte[] encrypt(byte[] data, byte[] key) {
- //恢复密钥
- SecretKey secretKey = new SecretKeySpec(key, "DES");
- try {
- //Cipher完成加密或解密工作
- Cipher cipher = Cipher.getInstance("DES");
- //根据密钥对Cipher进行初始化 ENCRYPT_MODE, DECRYPT_MODE
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- //加密
- return cipher.doFinal(data);
- } catch (Exception e) {
- Log.e(TAG, "encrypt: " + e.getMessage());
- }
- return null;
- }
- /**
- * DES解密
- */
- /**
- * @param data 密文对应的字节数组
- * @param key 算法名字
- * @return 解密后的字节数组
- */
- public static byte[] decrypt(byte[] data, byte[] key) {
- SecretKey secretKey = new SecretKeySpec(key, "DES");
- try {
- Cipher cipher = Cipher.getInstance("DES");
- cipher.init(Cipher.DECRYPT_MODE, secretKey);
- return cipher.doFinal(data);
- } catch (Exception e) {
- Log.e(TAG, "decrypt: " + e.getMessage());
- }
- return null;
- }
- }
3DES加密:
- package com.example.asiatravel.learndes.util;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * Created by kuangxiaoguo on 16/9/13.
- *
- * 3DES加密工具类
- */
- public class TripleDESUtil {
- /**
- * 生成密钥
- *
- * @return 密钥
- * @throws Exception
- */
- public static byte[] initKey() throws Exception {
- KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
- keyGenerator.init(168);//密钥长度 112 168
- SecretKey secretKey = keyGenerator.generateKey();
- return secretKey.getEncoded();
- }
- /**
- * 3DES加密
- *
- * @param data 要加密的数据
- * @param key 加密所使用的密钥
- * @return 加密后的数据
- * @throws Exception
- */
- public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
- SecretKey secretKey = new SecretKeySpec(key, "DESede");
- Cipher cipher = Cipher.getInstance("DESede");
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- return cipher.doFinal(data);
- }
- /**
- * 3DES解密
- *
- * @param data 加密后的数据
- * @param key 解密所需要的key
- * @return 解密后的数据
- * @throws Exception
- */
- public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
- SecretKey secretKey = new SecretKeySpec(key, "DESede");
- Cipher cipher = Cipher.getInstance("DESede");
- cipher.init(Cipher.DECRYPT_MODE, secretKey);
- return cipher.doFinal(data);
- }
- }
AES加密:
- package com.example.asiatravel.learndes.util;
- import java.security.NoSuchAlgorithmException;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * Created by kuangxiaoguo on 16/9/13.
- *
- * AES加密工具类
- */
- public class AESUtil {
- /**
- * 获取密钥
- *
- * @return 密钥
- * @throws NoSuchAlgorithmException
- */
- public static byte[] initKey() throws NoSuchAlgorithmException {
- KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
- keyGenerator.init(128); //192,256
- SecretKey secretKey = keyGenerator.generateKey();
- return secretKey.getEncoded();
- }
- /**
- * AES加密
- *
- * @param data 要加密的数据
- * @param key 加密所使用的密钥
- * @return 加密后的数据
- * @throws Exception
- */
- public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
- SecretKey secretKey = new SecretKeySpec(key, "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- return cipher.doFinal(data);
- }
- /**
- * AES解密
- *
- * @param data 要解密的数据
- * @param key 解密所使用的密钥
- * @return 解密后的数据, 即源数据
- * @throws Exception
- */
- public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
- SecretKey secretKey = new SecretKeySpec(key, "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.DECRYPT_MODE, secretKey);
- return cipher.doFinal(data);
- }
- }
测试三种加密方式的类:
- package com.example.asiatravel.learndes;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import com.example.asiatravel.learndes.util.AESUtil;
- import com.example.asiatravel.learndes.util.ByteToHexUtil;
- import com.example.asiatravel.learndes.util.DESUtil;
- import com.example.asiatravel.learndes.util.TripleDESUtil;
- public class MainActivity extends AppCompatActivity {
- private static final String DATA = "asiatravel";
- private static final String TAG = "TAG";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- testDES();
- test3DES();
- testAES();
- }
- /**
- * 测试AES加密-->对称加密
- */
- private void testAES() {
- try {
- /**
- * 密钥
- */
- byte[] aesKey = AESUtil.initKey();
- System.out.println(DATA + "AES key: " + ByteToHexUtil.fromByteToHex(aesKey));
- /**
- * 加密后的数据
- */
- byte[] encryptResult = AESUtil.encrypt(DATA.getBytes(), aesKey);
- System.out.println(DATA + "AES 加密: " + ByteToHexUtil.fromByteToHex(encryptResult));
- /**
- * 解密后的数据
- */
- byte[] decryptResult = AESUtil.decrypt(encryptResult, aesKey);
- System.out.println(DATA + "AES 解密: " + new String(decryptResult));
- } catch (Exception e) {
- Log.e(TAG, "testAES: " + e.getMessage());
- }
- }
- /**
- * 测试3DES加密-->对称加密
- */
- private void test3DES() {
- try {
- /**
- * 密钥
- */
- byte[] tripleKey = TripleDESUtil.initKey();
- System.out.println(DATA + "3DES key: " + ByteToHexUtil.fromByteToHex(tripleKey));
- /**
- * 加密后的数据
- */
- byte[] encryptResult = TripleDESUtil.encrypt(DATA.getBytes(), tripleKey);
- System.out.println(DATA + "3DES 加密: " + ByteToHexUtil.fromByteToHex(encryptResult));
- /**
- * 解密后的数据
- */
- byte[] decryptResult = TripleDESUtil.decrypt(encryptResult, tripleKey);
- System.out.println(DATA + "3DES 解密: " + new String(decryptResult));
- } catch (Exception e) {
- Log.e(TAG, "test3DES: " + e.getMessage());
- }
- }
- /**
- * 测试DES加密-->对称加密
- */
- private void testDES() {
- /**
- * DES 加密
- */
- byte[] desKey = DESUtil.initKey();
- System.out.println("DES key: " + ByteToHexUtil.fromByteToHex(desKey));
- /**
- * 加密后的数据
- */
- byte[] desResult = DESUtil.encrypt(DATA.getBytes(), desKey);
- System.out.println(DATA + "DES 加密>>>" + ByteToHexUtil.fromByteToHex(desResult));
- /**
- * DES 解密
- */
- byte[] decryptResult = DESUtil.decrypt(desResult, desKey);
- System.out.println(DATA + "DES 解密" + new String(decryptResult));
- }
- }