利用ASP.NET寫一支簡單的文字加密(Encode)與解密(Decode)程式

利用ASP.NET寫一支簡單的文字加密(Encode)與解密(Decode)程式

最天在小舖文章看到一篇還不錯的文章,把它收藏到Blog裡...

資料來源:http://www.blueshop.com.tw/board/show.asp?subcde=BRD20060303094220NJD&fumcde=FUM20050124191756KKC&rplcnt=5

文字加密跟解密加密後的文字長度跟解密後的文字長度都是一樣
對於要塞入資料庫的話會很方便,這支程式是用class寫的可以建置成元件來使用
因為可以建置成元件所以就不用可慮轉成C#語法了...只要建置好VB.net or C#都可以用了

原始碼(將檔案放在App_Code裡)
EverEncode.vb

01 Imports Microsoft.VisualBasic
02
03 Public Class EverEncode
04     Function TurnEncode(ByVal FormCode As String) As String
05         Dim EncodeView As String
06         Dim i As Integer
07         Dim jj As Integer
08
09         jj = Len(Trim(FormCode))
10
11         For i = 1 To jj Step 1
12             EncodeView = EncodeView & StringEnDeCodecn(Mid(Trim(FormCode), i, 1))
13         Next i
14         Return EncodeView
15     End Function

16     Function TurnUncode(ByVal FormCode As String) As String
17         Dim i, kk As Integer
18         Dim UnCodeView As String
19         kk = Len(FormCode)
20         For i = 1 To kk Step 1
21             UnCodeView = UnCodeView & StringEnDeCodecn(Mid(Trim(FormCode), i, 1))
22         Next i
23         Return UnCodeView
24
25     End Function

26
27     Private Function StringEnDeCodecn(ByVal strSource As String) As String
28         Dim X As Single
29         Dim CHARNUM As Long
30         Dim RANDOMINTEGER As Integer
31         Dim SINGLECHAR As String
32         Dim strTmp As String
33         Dim i, k As Integer
34
35         For i = 1 To Len(strSource) Step 1
36             SINGLECHAR = Mid(strSource, i, 1)
37             CHARNUM = Asc(SINGLECHAR)
38             CHARNUM = CHARNUM Xor 5
39             strTmp = strTmp & Chr(CHARNUM)
40         Next i
41
42         If strTmp = "?" Then
43             strTmp = ""
44             For k = 1 To Len(strSource) Step 1
45                 SINGLECHAR = Mid(strSource, k, 1)
46                 CHARNUM = Asc(SINGLECHAR)
47                 strTmp = strTmp & Chr(CHARNUM)
48             Next k
49         End If
50
51         Return strTmp
52         Exit Function
53
54     End Function

55 End Class


使用C#來測試加解密文字

01     protected void Page_Load(object sender, EventArgs e)
02     {
03         EverEncode code = new EverEncode();
04
05         //puma會加密成→uphd
06         Response.Write(code.TurnEncode("puma")); Response.Write("<br />");
07
08         //uphd會解密成→puma
09         Response.Write(code.TurnUncode("uphd"));
10     }


最後感謝Tom大大提供此程式碼