乙個可逆加密的例子

2021-03-31 17:30:20 字數 3836 閱讀 5822

下面的這個例子實現了乙個可逆加密的例子功能。**很簡單,這裡就不多解釋了。**如下:

encstring.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="encstring.aspx.vb" inherits="aspxweb.encstring"%>

encstring.aspx.vb

imports system

imports system.io

imports system.xml

imports system.text

imports system.security.cryptography

public class encstring

inherits system.web.ui.page

protected withevents textbox1 as system.web.ui.webcontrols.textbox

protected withevents textbox2 as system.web.ui.webcontrols.textbox

protected withevents form1 as system.web.ui.htmlcontrols.htmlform

protected withevents radiobuttonlist1 as system.web.ui.webcontrols.radiobuttonlist

#region " web form designer generated code "

'this call is required by the web form designer.

private sub initialize***ponent()

end sub

private sub page_init(byval sender as system.object,_

byval e as system.eventargs) handles mybase.init

'codegen: this method call is required by the web form designer

'do not modify it using the code editor.

initialize***ponent()

end sub

#end region

private sub page_load(byval sender as system.object,_

byval e as system.eventargs) handles mybase.load

'put user code to initialize the page here

if not ispostback then

dim mylist as new arraylist()

mylist.add("加密")

mylist.add("解密")

radiobuttonlist1.datasource = mylist

radiobuttonlist1.databind()

end if

end sub

' 加密

public shared function encrypttext(byval strtext as string) as string

return encrypt(strtext, "&%#@?,:*")

end function

'解密public shared function decrypttext(byval strtext as string) as string

return decrypt(strtext, "&%#@?,:*")

end function

'加密函式

private shared function encrypt(byval strtext as string,_

byval strencrkey as string) as string

dim bykey() as byte = {}

dim iv() as byte =

trybykey = system.text.encoding.utf8.getbytes(left(strencrkey, 8))

dim des as new descryptoserviceprovider()

dim inputbytearray() as byte = encoding.utf8.getbytes(strtext)

dim ms as new memorystream()

dim cs as new cryptostream(ms, des.createencryptor(bykey, iv), cryptostreammode.write)

cs.write(inputbytearray, 0, inputbytearray.length)

cs.flushfinalblock()

return convert.tobase64string(ms.toarray())

catch ex as exception

return ex.message

end try

end function

'解密函式

private shared function decrypt(byval strtext as string,_

byval sdecrkey as string) as string

dim bykey() as byte = {}

dim iv() as byte =

dim inputbytearray(strtext.length) as byte

trybykey = system.text.encoding.utf8.getbytes(left(sdecrkey, 8))

dim des as new descryptoserviceprovider()

inputbytearray = convert.frombase64string(strtext)

dim ms as new memorystream()

dim cs as new cryptostream(ms, des.createdecryptor(bykey, iv), cryptostreammode.write)

cs.write(inputbytearray, 0, inputbytearray.length)

cs.flushfinalblock()

dim encoding as system.text.encoding = system.text.encoding.utf8

return encoding.getstring(ms.toarray())

catch ex as exception

return ex.message

end try

end function

public sub showres(byval sender as object, _

byval e as system.eventargs) handles radiobuttonlist1.selectedindexchanged

if radiobuttonlist1.selectedindex = 0 then

textbox2.text = encrypttext(textbox1.text)

else

textbox2.text = decrypttext(textbox1.text)

end if

end sub

end class

乙個可逆加密的例子

page language c enableviewstate true codebehind encryptstring.aspx.cs autoeventwireup false inherits emeng.exam.encryptstring using system using syste...

乙個可逆加密的類 使用3DES加密

一 提要 命名空間 system.security.cryptography.tripledes 類 簡單說明 表示三重資料加密標準演算法的基類,tripledes 的所有實現都必須從此基類派生。是從 symmetricalgorithm 類裡繼承出來。tripledes 使用 des 演算法的三次...

乙個簡潔的PHP可逆加密函式 分享

很多時候我們需要對資料進行加密解密,比如有些資料需要儲存到cookie中,但又不能被使用者輕易得到這些資料,這時我們就需要加密這些資料儲存到cookie中,等我們需要使用它們的時候再解密。加密的過程如下 複製 如下 加密資料並寫到cookie裡 cookie data this encrypt no...