16进制可逆加密算法
生活随笔
收集整理的這篇文章主要介紹了
16进制可逆加密算法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
16進制可逆操作類:
public static class Hex16{/// <summary>/// 作用:將字符串內(nèi)容轉(zhuǎn)化為16進制數(shù)據(jù)編碼,其逆過程是Decode/// 參數(shù)說明:/// strEncode 需要轉(zhuǎn)化的原始字符串/// 轉(zhuǎn)換的過程是直接把字符轉(zhuǎn)換成Unicode字符,比如數(shù)字"3"-->0033,漢字"我"-->U+6211/// 函數(shù)decode的過程是encode的逆過程./// </summary>public static string Encode(string strEncode){string strReturn = "";// 存儲轉(zhuǎn)換后的編碼try{foreach (short shortx in strEncode.ToCharArray()){strReturn += shortx.ToString("X4");}}catch { }return strReturn;}/// <summary>/// 作用:將16進制數(shù)據(jù)編碼轉(zhuǎn)化為字符串,是Encode的逆過程/// </summary>public static string Decode(string strDecode){string sResult = "";try{for (int i = 0; i < strDecode.Length / 4; i++){sResult += (char)short.Parse(strDecode.Substring(i * 4, 4),global::System.Globalization.NumberStyles.HexNumber);}}catch { }return sResult;}/// <summary>/// 將數(shù)字轉(zhuǎn)換成16進制字符串,后兩位加入隨機字符,其可逆方法為DecodeForNum/// </summary>public static string EncodeForNum(int id){//用戶加上起始位置后的int startUserIndex = id;//轉(zhuǎn)換成16進制string hexStr = Convert.ToString(startUserIndex, 16);//后面兩位加入隨機數(shù)string randomchars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";string tmpstr ="";//整除的后得到的數(shù)可能大于被除數(shù)tmpstr += randomchars[(id / randomchars.Length) > randomchars.Length ? randomchars.Length - 1 : (id / randomchars.Length)];//余數(shù)不可能大于被除數(shù)tmpstr += randomchars[(id % randomchars.Length) > randomchars.Length ? randomchars.Length - 1 : (id % randomchars.Length)];//返回拼接后的字符,轉(zhuǎn)成大寫string retStr = (hexStr + tmpstr).ToUpper();return retStr;}/// <summary>/// 解密16進制字符串,此方法只適合后面兩位有隨機字符的/// </summary>public static int DecodeForNum(string strDecode){if (strDecode.Length>2){strDecode = strDecode.Substring(0, strDecode.Length - 2);return Convert.ToInt32(strDecode, 16);}return 0;}}?
轉(zhuǎn)載于:https://www.cnblogs.com/linJie1930906722/p/5968177.html
總結(jié)
以上是生活随笔為你收集整理的16进制可逆加密算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux和unix的关系
- 下一篇: Windows 10 下使用 telne