Symmetric encryption and decryption module

Source: Internet
Author: User

Imports system. Text

Imports system. Security. Cryptography

 

Module modsecurity

Function entext (byval text as string, byval skey as string) as string

TEXT = text. tolower

Dim des as new descryptoserviceprovider

Dim inputbytearray () as byte

Inputbytearray = encoding. Default. getbytes (text)

Des. Key = asciiencoding. ASCII. getbytes (left (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (skey, "MD5"), 8 ))

Des. IV = asciiencoding. ASCII. getbytes (left (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (skey, "MD5"), 8 ))

Dim MS as new system. Io. memorystream

Dim CS as new cryptostream (MS, Des. createencryptor, cryptostreammode. Write)

CS. Write (inputbytearray, 0, inputbytearray. length)

CS. flushfinalblock ()

Dim RET as new stringbuilder

Dim B as byte

For each B in ms. toarray ()

Ret. appendformat ("{0: X2}", B)

Next

Return ret. tostring ()

End Function

 

Function detext (byval text as string, byval skey as string) as string

Dim des as new descryptoserviceprovider

Dim Len as integer

Len = text. Length/2-1

Dim inputbytearray (LEN) as byte

Dim X, I as integer

For x = 0 to Len

I = convert. toint32 (text. substring (x * 2, 2), 16)

Inputbytearray (x) = ctype (I, byte)

Next

Des. Key = asciiencoding. ASCII. getbytes (left (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (skey, "MD5"), 8 ))

Des. IV = asciiencoding. ASCII. getbytes (left (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (skey, "MD5"), 8 ))

Dim MS as new system. Io. memorystream

Dim CS as new cryptostream (MS, Des. createdecryptor, cryptostreammode. Write)

CS. Write (inputbytearray, 0, inputbytearray. length)

CS. flushfinalblock ()

Return encoding. Default. getstring (Ms. toarray)

End Function

End Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.