继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

常用加密方法

哈士奇WWW
关注TA
已关注
手记 503
粉丝 71
获赞 400

1.MD5

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

public class MD5Strategy

{

    public String   encode(String src)

    {

        try

        {

            MessageDigest   md = MessageDigest.getInstance("MD5");

            byte[]   encodeBytes = md.digest(src.getBytes());

 

            return Hex.encodeHexString(encodeBytes);

        }

        catch (NoSuchAlgorithmException   e)

        {

            e.printStackTrace();

        }

        return null;

    }

 

    public String   decode(String src)

    {

        throw new RuntimeException("MD5   no decode");

    }

}

2.SHA

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

public class SHAStrategy

{

 

    public String   encode(String src)

    {

        try

        {

            MessageDigest   md = MessageDigest.getInstance("SHA");

            md.update(src.getBytes());

            return Hex.encodeHexString(md.digest());

        }

        catch (NoSuchAlgorithmException   e)

        {

            e.printStackTrace();

        }

        return null;

    }

    public String   decode(String src)

    {

        throw new RuntimeException("SHA   no decode");

    }

}

3.DES

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

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

48

public class DESStrategy

{

    private Cipher   cipher;

    private SecretKey   generateKey;

 

    public String   encode(String src)

    {

        try

        {

            KeyGenerator   keyGenerator = KeyGenerator.getInstance("DES");

            keyGenerator.init(56);//size

            SecretKey   secretKey = keyGenerator.generateKey();

            byte[]   keyBytes = secretKey.getEncoded();

 

            DESKeySpec   desKeySpec = new DESKeySpec(keyBytes);

            SecretKeyFactory   secretKeyFactory = SecretKeyFactory.getInstance("DES");

            generateKey   = secretKeyFactory.generateSecret(desKeySpec);

 

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

            cipher.init(Cipher.ENCRYPT_MODE,   generateKey);

            byte[]   resultBytes = cipher.doFinal(src.getBytes());

 

            return Hex.encodeHexString(resultBytes);

        }

        catch (Exception   e)

        {

            e.printStackTrace();

        }

 

        return null;

    }

 

    public String   decode(String src)

    {

        try

        {

            cipher.init(Cipher.DECRYPT_MODE,   generateKey);

            byte[]   result = Hex.decodeHex(src.toCharArray());

            return new String(cipher.doFinal(result));

        }

        catch(Exception   e)

        {

            e.printStackTrace();

        }

 

        return null;

    }

}

4.AES

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

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

public class AESStrategy

{

    private Cipher   cipher;

    private SecretKey   generateKey;

 

    public String   encode(String src)

    {

        try

        {

            KeyGenerator   keyGenerator = KeyGenerator.getInstance("AES");

            keyGenerator.init(128);//size

            SecretKey   secretKey = keyGenerator.generateKey();

            byte[]   keyBytes = secretKey.getEncoded();

 

            generateKey   = new SecretKeySpec(keyBytes, "AES");

 

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

            cipher.init(Cipher.ENCRYPT_MODE,   generateKey);

            byte[]   resultBytes = cipher.doFinal(src.getBytes());

 

            return Hex.encodeHexString(resultBytes);

        }

        catch (Exception   e)

        {

            e.printStackTrace();

        }

 

        return null;

    }

 

    public String   decode(String src)

    {

        try

        {

            cipher.init(Cipher.DECRYPT_MODE,   generateKey);

            byte[]   result = Hex.decodeHex(src.toCharArray());

            return new String(cipher.doFinal(result));

        }

        catch(Exception   e)

        {

            e.printStackTrace();

        }

 

        return null;

    }

}

5.RSA

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

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

48

49

50

51

52

53

54

55

public class RSAStrategy   implements IStrategy

{

    private RSAPublicKey   rsaPublicKey;

    private RSAPrivateKey   rsaPrivateKey;

 

    public String   encode(String src)

    {

        try

        {

            //初始化密钥

            KeyPairGenerator   keyPairGenerator = KeyPairGenerator.getInstance("RSA");

            keyPairGenerator.initialize(512);

            KeyPair   keyPair = keyPairGenerator.generateKeyPair();

            rsaPublicKey   = (RSAPublicKey)keyPair.getPublic();

            rsaPrivateKey   = (RSAPrivateKey)keyPair.getPrivate();

 

            //私钥加密 公钥解密

            PKCS8EncodedKeySpec   pkcs8EncodedKeySpec

                =   new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());

            KeyFactory   keyFactory = KeyFactory.getInstance("RSA");

            PrivateKey   privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

            Cipher   cipher = Cipher.getInstance("RSA");

            cipher.init(Cipher.ENCRYPT_MODE,   privateKey);

            byte[]   resultBytes = cipher.doFinal(src.getBytes());

            return Hex.encodeHexString(resultBytes);

        }

        catch(Exception   e)

        {

            e.printStackTrace();

        }

 

        return null;

    }

 

    public String   decode(String src)

    {

        try

        {

            //私钥加密 公钥解密

            X509EncodedKeySpec   x509EncodedKeySpec =

                    new X509EncodedKeySpec(rsaPublicKey.getEncoded());

            KeyFactory   keyFactory = KeyFactory.getInstance("RSA");

            PublicKey   publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

            Cipher   cipher = Cipher.getInstance("RSA");

            cipher.init(Cipher.DECRYPT_MODE,   publicKey);

            byte[]   resultBytes = cipher.doFinal(Hex.decodeHex(src.toCharArray()));

            return new String(resultBytes);

        }

        catch(Exception   e)

        {

            e.printStackTrace();

        }

        return null;

    }

}

原文链接:http://www.apkbus.com/blog-780485-77077.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP