为了账号安全,请及时绑定邮箱和手机立即绑定

常用加密方法

标签:
Android

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

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消