[J2SE]Java SE Security初探 -- 加解密

这里加解密以DES为例

首先加密解决过程的几个点作简要的说明
1.加密模式及补位
下面两行代码是等价,也就是JDK中对DES默认的算法是ECB模式,PKCS5Padding的补位方式(由于对加解密算法知识有限,所以就不对模式跟补位方式作解释)

Cipher cipher = Cipher.getInstance("DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

2.固定密钥生成,DESKeySpec的参数必须是byte[8],也就是如果密钥超过8位,加密效果是一样的,而如果不够8位,一定要进行补位,一般用0补位
DESKeySpec keySpec = new DESKeySpec(generateDESKeyBytes("orz"));


具体步骤:
[b]1.生成固定密钥,这里为了简化过程,完全可以做成随机密钥,随内容分发[/b]

/**
* 生成固定密钥 设定密钥的为orz
*
* @return
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
private static SecretKey generateDES_ECBMode_Key()
throws InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException {
// 参数必须是byte[8]的数组
DESKeySpec keySpec = new DESKeySpec(generateDESKeyBytes("orz"));

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
return key;
}

/**
* 生成byte[8]的数组,不足用0补足
*
* @param keyWord
* @return
*/
private static byte[] generateDESKeyBytes(String keyWord) {
byte[] key = new byte[8];

byte[] tmp = keyWord.getBytes();
for (int i = 0; i < key.length; i++) {
if (i < tmp.length)
key[i] = tmp[i];
}
return key;
}



[b]2.加密,返回16进制的字符串[/b]

public static String cryptDES(String plainText)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, InvalidKeySpecException,
IllegalBlockSizeException, BadPaddingException {
SecureRandom random = new SecureRandom();
String encryptText = null;
byte[] result = null;
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, generateDES_ECBMode_Key(), random);

result = cipher.doFinal(plainText.getBytes());
encryptText = StringBytesTransformUtils.bytesToHexString(result);
return encryptText;
}


[b]3.解密[/b]

public static String decryptDES(String encryptText)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, InvalidKeySpecException,
IllegalBlockSizeException, BadPaddingException {
SecureRandom random = new SecureRandom();
String decryptText = null;
byte[] result = null;
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, generateDES_ECBMode_Key(), random);
result = cipher.doFinal(StringBytesTransformUtils
.hexStringToBytes(encryptText));
// 还原原来的数据
decryptText = new String(result);
return decryptText;
}


String-byte[]转换器,下面这段代码来自网上

/**
* String-byte[]的转换器
*
* @author Kevin Chen
*
*/
public class StringBytesTransformUtils {
public static String bytesToHexString(byte[] bArray) {

StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}

public static byte[] hexStringToBytes(String hexString) {
int len = (hexString.length() / 2);
byte[] result = new byte[len];
char[] achar = hexString.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}

public static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值