> 백엔드 개발 > C#.Net 튜토리얼 > ASP.NET의 DES 암호화 및 암호 해독 MD5에 대한 자세한 설명

ASP.NET의 DES 암호화 및 암호 해독 MD5에 대한 자세한 설명

零下一度
풀어 주다: 2017-07-16 15:29:59
원래의
1974명이 탐색했습니다.

이 글은 주로 ASP.NET의 DES 암호화 및 복호화 MD5 암호화 도우미 클래스의 예제 코드를 소개합니다. 더 알고 싶은 친구들은 한 번 살펴보세요.

1

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

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

public class TrialHelper

  {    //默认密钥向量

    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

    /// <summary>

    /// DES加密字符串

    /// </summary>

    /// <param name="encryptString">待加密的字符串</param>

    /// <param name="encryptKey">加密密钥,要求为8位</param>

    /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>

    public static string EncryptDES( string encryptString, string encryptKey = "" )

    {

      try

      {

        if (string.IsNullOrEmpty(encryptKey) || encryptKey.Length < 8)

        {

          encryptKey = "winform01";

        }

        byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));

        byte[] rgbIV = Keys;

        byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

        DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();

        MemoryStream mStream = new MemoryStream();

        CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

        cStream.Write(inputByteArray, 0, inputByteArray.Length);

        cStream.FlushFinalBlock();

        return Convert.ToBase64String(mStream.ToArray());

      }

      catch

      {

        return encryptString;

      }

    }

    /// <summary>

    /// DES解密字符串

    /// </summary>

    /// <param name="decryptString">待解密的字符串</param>

    /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>

    /// <returns>解密成功返回解密后的字符串,失败返源串</returns>

    public static string DecryptDES( string decryptString, string decryptKey = "" )

    {

      try

      {

        if (string.IsNullOrEmpty(decryptKey) || decryptKey.Length < 8)

        {

          decryptKey = "winform01";

        }

        byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));

        byte[] rgbIV = Keys;

        byte[] inputByteArray = Convert.FromBase64String(decryptString);

        DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();

        MemoryStream mStream = new MemoryStream();

        CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

        cStream.Write(inputByteArray, 0, inputByteArray.Length);

        cStream.FlushFinalBlock();

        return Encoding.UTF8.GetString(mStream.ToArray());

      }

      catch

      {

        return decryptString;

      }

    }

    /// <summary>

    /// MD5数据加密

    /// </summary>

    /// <param name="sDataIn">加密字段</param>

    /// <returns>加密后的字符串</returns>

    public static string GetMD5( string sDataIn )

    {

      System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

      byte[] bytValue, bytHash;

      bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);

      bytHash = md5.ComputeHash(bytValue);

      md5.Clear();

      string sTemp = "";

      for (int i = 0; i < bytHash.Length; i++)

      {

        sTemp += bytHash[i].ToString("x").PadLeft(2, &#39;0&#39;);

      }

      return sTemp;

    }

  }

로그인 후 복사

전화:

1

2

3

4

//获取登录信息

        loginRecord.Name = tbName.Text.Trim();

        loginRecord.MD5Pwd = TrialHelper.GetMD5(tbPwd.Password); //保存到数据库MD5加密方式

        loginRecord.Pwd = TrialHelper.EncryptDES(tbPwd.Password);//记住密码des加密方式,保存到本地

로그인 후 복사

위 내용은 ASP.NET의 DES 암호화 및 암호 해독 MD5에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿