Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
VB
Private Function Encrypt(clearText As String) As String
        Dim EncryptionKey As String = "MAKV2SPBNI99212"
        Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText)
        Using encryptor As Aes = Aes.Create()
            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _
             &H65, &H64, &H76, &H65, &H64, &H65, _
             &H76})
            encryptor.Key = pdb.GetBytes(32)
            encryptor.IV = pdb.GetBytes(16)
            Using ms As New MemoryStream()
                Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
                    cs.Write(clearBytes, 0, clearBytes.Length)
                    cs.Close()
                End Using
                clearText = Convert.ToBase64String(ms.ToArray())
            End Using
        End Using
        Return clearText
    End Function


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 11-Jul-14 8:02am
v2
Comments
OriginalGriff 11-Jul-14 14:03pm    
Well, you wrote it - so what do you think it does?
Member 10940701 11-Jul-14 14:15pm    
friend i know it doeses encryption but i want to know how i just copied the code from somewhere because i want to use encryption in a simple data interacting software

Pretty much, there are two ways to explain what is done:
1) "It encrypts data"
Or
2) Explain the code line by line, because it's quite involved for a beginner.

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

So, you either need to find a book on encryption, or start by using Google and MSDN to follow what each of the classes and methods used does, how it does it, and how it is used.
But nobody (except possibly the author of the code) is going to explain that to you in the depth you want.
 
Share this answer
 
Comments
Member 10940701 11-Jul-14 14:36pm    
Ok then can you please tell me I just want to encrypt the strings before storing them to the database will this code work for that
OriginalGriff 11-Jul-14 14:40pm    
Probably - I haven't tried it.
I'd rather have the key being passed into the method personally, instead of having it as a fixed string.
But it looks at though it should work.
Have you tried it and the decryption method at all?
Member 10940701 11-Jul-14 14:45pm    
It encrypts the string but i just want to know is this encryption method harder to decrypt also if you need its full code i can provide you
Blutfaust 11-Jul-14 16:35pm    
Unbelievable
Member 10940701 11-Jul-14 14:59pm    
Please find the code file in attached Link
https://www.dropbox.com/sh/62h43x3y6pqfl6m/AABuL34msMy_XJnPhvqGZtEWa
I think this article, where the exact code seems to have originated, explains it:

http://aspsnippets.com/Articles/AES-Encryption-Decryption-Cryptography-Tutorial-with-example-in-ASPNet-using-C-and-VBNet.aspx
 
Share this answer
 
Comments
phil.o 20-Mar-15 20:18pm    
What is the point to dig out a 9 months old question that has been marked as answered?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900