你应该知道的加解密

在互联网高速发展的今天,数据的交互变得越来越频繁,然后有些数据是具有机密性的,不能别人轻易获取,这时对数据的加密就显得很重要了,现在使用比较多的加密算法有DES、3DES 、RC2和RC4、IDEA、RSA、DSA等等,有兴趣的话可以去了解一下。

一、了解对称加密算法

在对称加密算法中,数据发信方将明文(原始数据)和加密密钥一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去。收信方收到密文后,若想解读原文,则需要使用加密用过的密钥及相同算法的逆算法对密文进行解密,才能使其恢复成可读明文。在对称加密算法中,使用的密钥只有一个,发收信双方都使用这个密钥对数据进行加密和解密,这就要求解密方事先必须知道加密密钥。

对称加密算法的特点是算法公开、计算量小、加密速度快、加密效率高。

二、加密算法的使用

闲话不多说直接上代码:

				
package com.cs.test.encode;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESede {
	private final static String DESede = "DESede/CBC/PKCS5Padding";
	// 传入keystr 12位 返回一个byte数组
	public static byte[] getSKey(String keystr) {
		// 得到反向排序的String
		String tempStr = getString(keystr);
		// 构建一个新的字符串(正+反+反+正+正+反)72位
		String newStr = keystr + tempStr + tempStr + keystr + keystr + tempStr;
		int l = newStr.length() / 3;
		byte[] w = new byte[l];
		// 为byte数组传值
		for (int i = 0; i < l; i++) {
			// parseInt(String s, int radix) 使用第二个参数指定的基数,将字符串参数解析为有符号的整数。
w[i] = (byte) (Integer.parseInt(newStr.substring(i * 3, i * 3 + 3), 36) & 0xff);
		}
		return (w);
	}

	// 传入sIV 8位 返回一个byte数组
	public static byte[] getSIV(String sIV) {
		// 得到反向排序的String
		String tempStr = getString(sIV);
		// 构建一个新的字符串(正+反+正+反)32位
		String newStr = sIV + tempStr + sIV + tempStr;
		int l = newStr.length() / 4;
		byte[] w = new byte[l];
		for (int i = 0; i < l; i++) {
w[i] = (byte) (Integer.parseInt(newStr.substring(i * 4, i * 4 + 4), 36) & 0xff);
		}
		return (w);
	}

	// 传入一个字符串返回一个反向排序的字符串
	public static String getString(String keystr) {
		int l = keystr.length();
		char data[] = keystr.toCharArray();
		char str[] = new char[l];
		for (int i = 0, j = l - 1; i < l; i++, j--) {
			str[j] = data[i];
		}
		String strKey = new String(str);
		return strKey;
	}

	// 传入明文byte[]数组,12位sKey,8位sIV返回加密处理的byte[]数组
	private static byte[] EncryptionByteData(byte[] SourceData, String sKey, String sIV) throws Exception {
		byte[] retByte = null;
		byte[] EncryptionByte = getSKey(sKey);
		SecretKey securekey = new SecretKeySpec(EncryptionByte, "DESede");
		IvParameterSpec spec = new IvParameterSpec(getSIV(sIV));
		Cipher cipher = Cipher.getInstance(DESede);
		cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);
		retByte = cipher.doFinal(SourceData);
		return retByte;
	}




	// 传入密文byte[]数组,12位sKey,8位sIV 返回解密处理的byte[]数组
	private static byte[] DecryptionByteData(byte[] SourceData, String sKey, String sIV) throws Exception {
		byte[] retByte = null;
		byte[] EncryptionByte = getSKey(sKey);
		SecretKey securekey = new SecretKeySpec(EncryptionByte, "DESede");
		IvParameterSpec spec = new IvParameterSpec(getSIV(sIV));
		Cipher cipher = Cipher.getInstance(DESede);
		cipher.init(Cipher.DECRYPT_MODE, securekey, spec);
		retByte = cipher.doFinal(SourceData);
		return retByte;
	}

	// 传入明文,12位sKey,8位sIV,生成密文
	public static String EncryptCode(String strTobeEnCrypted, String sKey, String sIV) throws Exception {
		String retStr = null;
		byte[] retByte = null;
		byte[] sorData = strTobeEnCrypted.getBytes();
		retByte = EncryptionByteData(sorData, sKey, sIV);
		BASE64Encoder be = new BASE64Encoder();
		retStr = be.encode(retByte);
		return retStr;
	}

	// 传入密文,12位sKey,8位sIV,生成明文
	public static String DecryptCode(String strTobeDeCrypted, String sKey, String sIV) throws Exception {
		String retStr = null;
		byte[] retByte = null;
		BASE64Decoder bd = new BASE64Decoder();
		byte[] sorData = bd.decodeBuffer(strTobeDeCrypted);
		retByte = DecryptionByteData(sorData, sKey, sIV);
		retStr = new String(retByte);
		return retStr;
	}
}


到此我们的加密方法已经写完,下面我们进行一次测试:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值