1.DES算法的
DES算法的介紹和相關(guān)說明我就不講了,大家可以自己google,要了解的就是DES算法屬于塊加密算法,通常是64位為一塊來進(jìn)行加密,因此就這會涉及到塊填充的問題。所以如果是異構(gòu)系統(tǒng)間的通訊,一定要問清楚大家的塊填充方式,當(dāng)然最好就是要加密的明文剛好是64位的倍數(shù),這樣雙方都不用填充。另外DES算法加密后不會改變長度
我自己下的DESUtil如下
1
private static final String TDESAlgorithm = "TripleDES";
2
private static final String DESAlgorithm = "DES/ECB/NoPadding";
3
/** *//**
4
* ECB模式和NoPadding填充的DES加密函數(shù)。
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解密函數(shù)。
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算法是比較復(fù)雜的了,說復(fù)雜是因?yàn)樵诋悩?gòu)系統(tǒng)之間的兼容性差一點(diǎn)。具體介紹也不說了,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. 也就是密文長度和密鑰長度之間的一個(gè)關(guān)系,否則也會導(dǎo)致加密失敗。RSA算法加密后會改變長度
RSA中另外一個(gè)關(guān)鍵的概念是 模,公用指數(shù)和私鑰的關(guān)系
模和公用指數(shù)生成公鑰,Java中的公用指數(shù)有自定義的兩個(gè)RSAKeyGenParameterSpec.F1和RSAKeyGenParameterSpec.F4
生成公鑰key的方式為
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
RSAPublicKey pubKey = (RSAPublicKey)keyFactory.generatePublic(pubKeySpec);
模和私鑰指數(shù)生成私鑰,方式為
RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(modulus,prikeyInteger);
RSAPrivateKey privKey = (RSAPrivateKey)keyFactory.generatePrivate(privKeySpec);
這些參數(shù)都是以BigInteger的方式傳入的,具體可以參看BigInteger的構(gòu)造函數(shù),其中有一個(gè)常用的是
BigInteger bint = new BigInteger(String str,int radix)
用途是把str按照radix進(jìn)制來解析,非常方便。Integer也有這種類似的方法,所以十六進(jìn)制和十進(jìn)制的轉(zhuǎn)換也可以用這個(gè)方式變相實(shí)現(xiàn)。
Java里邊有兩本書對加密解密說的比較詳細(xì),個(gè)人認(rèn)為還不錯(cuò)。
Java的RSA中需要特別注意的是,java沒有無符號數(shù)這個(gè)概念,所以有的時(shí)候要特別注意密鑰,模的正負(fù)關(guān)系
《Beginning Cryptography with Java》
《Core Security Patterns: Best Practices and Strategies for J2EE(TM), Web Services, and Identity Management (Core Series)》(中文名:《安全模式--J2EE、WEB服務(wù)和身份管理最佳實(shí)踐與策略》)
posted on 2007-07-20 14:47
Caixiaopig 閱讀(1189)
評論(0) 編輯 收藏 所屬分類:
Java高級