1
/**
2
* @param input
3
* @return
4
* @throws Exception
5
*/
6
public static String encryptData(String input) throws Exception {
7
8
SecureRandom sr = new SecureRandom();
9
byte rawKeyData[] = "ABCDEFGH".getBytes();
10
DESKeySpec dks = new DESKeySpec(rawKeyData);
11
12
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
13
SecretKey key = keyFactory.generateSecret(dks);
14
15
Cipher c = Cipher.getInstance("DES");
16
c.init(Cipher.ENCRYPT_MODE, key, sr);
17
byte[] cipherByte = c.doFinal(input.getBytes());
18
String dec = new BASE64Encoder().encode(cipherByte);
19
return dec;
20
21
}
22
23
/**
24
* @param input
25
* @return
26
* @throws Exception
27
*/
28
public static String decryptData(String input) throws Exception {
29
byte[] dec = new BASE64Decoder().decodeBuffer(input);
30
31
SecureRandom sr = new SecureRandom();
32
byte rawKeyData[] = "ABCDEFGH".getBytes();
33
34
DESKeySpec dks = new DESKeySpec(rawKeyData);
35
36
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
37
38
SecretKey key = keyFactory.generateSecret(dks);
39
40
Cipher c = Cipher.getInstance("DES");
41
c.init(Cipher.DECRYPT_MODE, key, sr);
42
byte[] clearByte = c.doFinal(dec);
43
44
return new String(clearByte);
45
46
}
47
注:轉(zhuǎn)自 http://jlusdy.javaeye.com/blog/145803

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47
