1.DES算法的
DES算法的介绍和相关说明我就不讲了,大家可以自己google,要了解的就是DES算法属于块加密算法,通常是64位为一块来进行加密,因此就这会涉及到块填充的问题。所以如果是异构系统间的通讯,一定要问清楚大家的块填充方式,当然最好就是要加密的明文刚好是64位的倍数,这样双方都不用填充。另外DES算法加密后不会改变长度
我自己下的DESUtil如下
1
private static final String TDESAlgorithm = "TripleDES";
2
private static final String DESAlgorithm = "DES/ECB/NoPadding";
3
/** *//**
4
* ECB模式和NoPadding填充的DES加密函数。
5
* @param src - 明文
6
* @param sKeyByte - 密钥
7
* @return encryptByte - 密文
8
*/
9
public static byte[] encryptWithDES(byte[] src,byte[] sKeyByte)
{
10
try
{
11
Cipher myCipher = Cipher.getInstance(DESAlgorithm);
12
SecretKey sKey = new SecretKeySpec(sKeyByte,"DES");
13
myCipher.init(Cipher.ENCRYPT_MODE, sKey);
14
byte[] encryptByte = myCipher.doFinal(src);
15
return encryptByte;
16
} catch (NoSuchAlgorithmException e)
{
17
e.printStackTrace();
18
} catch (NoSuchPaddingException e)
{
19
e.printStackTrace();
20
} catch (InvalidKeyException e)
{
21
e.printStackTrace();
22
} catch (IllegalBlockSizeException e)
{
23
e.printStackTrace();
24
} catch (BadPaddingException e)
{
25
e.printStackTrace();
26
}
27
return null;
28
}
29
30
/** *//**
31
* ECB模式和NoPadding填充的DES解密函数。
32
* @param src - 密文
33
* @param sKeyByte - 密钥
34
* @return decryptByte - 明文
35
*/
36
public static byte[] decryptWithDES(byte[] src,byte[] sKeyByte)
{
37
try
{
38
Cipher myCipher = Cipher.getInstance(DESAlgorithm);
39
SecretKey sKey = new SecretKeySpec(sKeyByte,"DES");
40
myCipher.init(Cipher.DECRYPT_MODE, sKey);
41
byte[] decryptByte = myCipher.doFinal(src);
42
return decryptByte;
43
} catch (NoSuchAlgorithmException e)
{
44
e.printStackTrace();
45
} catch (NoSuchPaddingException e)
{
46
e.printStackTrace();
47
} catch (InvalidKeyException e)
{
48
e.printStackTrace();
49
} catch (IllegalBlockSizeException e)
{
50
e.printStackTrace();
51
} catch (BadPaddingException e)
{
52
e.printStackTrace();
53
}
54
return null;
55
}
2.RSA算法
相对DES算法,RSA算法是比较复杂的了,说复杂是因为在异构系统之间的兼容性差一点。具体介绍也不说了,RSA算法需要注意的是
The RSA algorithm can only encrypt data that has a maximum byte length of the RSA key length in bits divided with eight minus eleven padding bytes, i.e. number of maximum bytes = key length in bits / 8 - 11. In your case it means 2048 / 8 - 11 = 245. If you want to encrypt larger data, then use a larger key, for example, a key with 4096 bits will allow you to encrypt 501 bytes of data. 也就是密文长度和密钥长度之间的一个关系,否则也会导致加密失败。RSA算法加密后会改变长度
RSA中另外一个关键的概念是 模,公用指数和私钥的关系
模和公用指数生成公钥,Java中的公用指数有自定义的两个RSAKeyGenParameterSpec.F1和RSAKeyGenParameterSpec.F4
生成公钥key的方式为
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
RSAPublicKey pubKey = (RSAPublicKey)keyFactory.generatePublic(pubKeySpec);
模和私钥指数生成私钥,方式为
RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(modulus,prikeyInteger);
RSAPrivateKey privKey = (RSAPrivateKey)keyFactory.generatePrivate(privKeySpec);
这些参数都是以BigInteger的方式传入的,具体可以参看BigInteger的构造函数,其中有一个常用的是
BigInteger bint = new BigInteger(String str,int radix)
用途是把str按照radix进制来解析,非常方便。Integer也有这种类似的方法,所以十六进制和十进制的转换也可以用这个方式变相实现。
Java里边有两本书对加密解密说的比较详细,个人认为还不错。
Java的RSA中需要特别注意的是,java没有无符号数这个概念,所以有的时候要特别注意密钥,模的正负关系
《Beginning Cryptography with Java》
《Core Security Patterns: Best Practices and Strategies for J2EE(TM), Web Services, and Identity Management (Core Series)》(中文名:《安全模式--J2EE、WEB服务和身份管理最佳实践与策略》)
posted on 2007-07-20 14:47
Caixiaopig 阅读(1188)
评论(0) 编辑 收藏 所属分类:
Java高级