當前位置:
首頁 > 知識 > C井——分享幾種常用的編碼轉換,base64、MD5、string

C井——分享幾種常用的編碼轉換,base64、MD5、string

C# Base64編碼

class Base64Helper
{
/// <summary>
/// Base64加密,採用utf8編碼方式加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns>加密後的字元串</returns>
public static string Base64Encode(string source)
{
return Base64Encode(Encoding.UTF8, source);
}
/// <summary>
/// Base64加密
/// </summary>
/// <param name="encodeType">加密採用的編碼方式</param>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
public static string Base64Encode(Encoding encodeType, string source)
{
string encode = string.Empty;
byte[] bytes = encodeType.GetBytes(source);
try
{
encode = Convert.ToBase64String(bytes);
}
catch
{
encode = source;
}
return encode;
}
/// <summary>
/// Base64解密,採用utf8編碼方式解密
/// </summary>
/// <param name="result">待解密的密文</param>
/// <returns>解密後的字元串</returns>
public static string Base64Decode(string result)
{
return Base64Decode(Encoding.UTF8, result);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name="encodeType">解密採用的編碼方式,注意和加密時採用的方式一致</param>
/// <param name="result">待解密的密文</param>
/// <returns>解密後的字元串</returns>
public static string Base64Decode(Encoding encodeType, string result)
{
string decode = string.Empty;
byte[] bytes = Convert.FromBase64String(result);
try
{
decode = encodeType.GetString(bytes);
}
catch
{
decode = result;
}
return decode;
}
}
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

C# 文件與二進位流

/// <summary>
/// 將文件轉換為byte數組
/// </summary>
/// <param name="path">文件地址</param>
/// <returns>轉換後的byte數組</returns>
public static byte[] File2Bytes(string path)
{
if (!System.IO.File.Exists(path))
{
return new byte[0];
}
FileInfo fi = new FileInfo(path);
byte[] buff = new byte[fi.Length];
FileStream fs = fi.OpenRead();
fs.Read(buff, 0, Convert.ToInt32(fs.Length));
fs.Close();
return buff;
}
/// <summary>
/// 將byte數組轉換為文件並保存到指定地址
/// </summary>
/// <param name="buff">byte數組</param>
/// <param name="savepath">保存地址</param>
public static void Bytes2File(byte[] buff, string savepath)
{
if (System.IO.File.Exists(savepath))
{
System.IO.File.Delete(savepath);
}
FileStream fs = new FileStream(savepath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff, 0, buff.Length);
bw.Close();
fs.Close();
}
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

C# MD5加密

public static string MD5Encrypt(string strText)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
return System.Text.Encoding.Default.GetString(result);
}
private static string GetMD5String(string sign)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(sign));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encryptedBytes.Length; i++)
{
sb.AppendFormat("{0:x2}", encryptedBytes[i]);
}
return sb.ToString();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

C# string和byte[]

string類型轉成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
byte[]轉成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );

C井——分享幾種常用的編碼轉換,base64、MD5、string

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

dubbo源碼分析之服務治理
RocketMQ底層通信機制

TAG:程序員小新人學習 |