VB - 31 Bit Encryption function - Page 2-VBForums
Page 2 of 2 FirstFirst 12
Results 41 to 59 of 59

Thread: VB - 31 Bit Encryption function

  1. #41
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Re: VB - 31 Bit Encryption function

    does anyone know if this function can be ported to c++?

  2. #42
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Re: VB - 31 Bit Encryption function

    does anyone know if this function can be ported to c++?

  3. #43

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,728

    Re: VB - 31 Bit Encryption function

    Most likely...

    Let me open C++ and try it...

  4. #44
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by CVMichael
    Most likely...

    Let me open C++ and try it...
    thank you soooo much CVMichael.. you are a king among kings

  5. #45

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,728

    Re: VB - 31 Bit Encryption function

    Here it is...

    I coded it in Turbo C++ Version 3.0, but it should work in any compiler... (hopefully)
    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    
    char* RndCrypt(char* str, int str_len, char* password);
    
    void main(void)
    {
    	int k;
    	char *orig_str = "Hello world";
    	int str_len = strlen(orig_str);
    	char *enc_str = NULL;
    	char *dec_str = NULL;
    
    	while(kbhit()) getch();
    	clrscr();
    
    	printf("Original  = %s\n", orig_str);
    
    	enc_str = RndCrypt(orig_str, str_len, "test_password4");
    
    	printf("Encrypted = %s\n", enc_str);
    
    	dec_str = RndCrypt(enc_str, str_len, "test_password4");
    
    	printf("Decrypted = %s\n", dec_str);
    
    	delete enc_str;
    	delete dec_str;
    
    	while(kbhit()) getch();
    	getch();
    	while(kbhit()) getch();
    }
    
    char* RndCrypt(char* str, int str_len, char* password)
    {
    	int k;
    	int pass_len = strlen(password);
    	unsigned long sk = 0;
    	char * ret_val = new char[str_len + 1];
    
    	srand(pass_len);
    
    	for(k = 0; k < pass_len; k++)
    		sk = sk + (((k % 256) ^ password[k]) ^ (rand() % 256));
    
    	srand(sk);
    
    	for(k = 0; k < str_len; k++)
    		ret_val[k] = (rand() % 256) ^ str[k];
    
    	ret_val[k] = 0;
    
    	return ret_val;
    }
    [Edit]
    Just to explain a little, the function is coded similarly to the VB one, except, it needs to know the length of the string especially when it needs to decrypt. This is because the encrypted string could contain NULL values, and strlen() would give an incorrect string length (the encrypted string is binary, not a string anymore). That's why it's not using strlen() to get the length of data for the str parameter, but it does use strlen() for getting the length of the password since it's always a NULL ended string.
    Last edited by CVMichael; Jan 7th, 2008 at 08:11 PM.

  6. #46
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Re: VB - 31 Bit Encryption function

    neat thanks

    do you know if this c++ function can decrypt a string encrypted by the vb6 function?
    Last edited by VaxoP; Jan 8th, 2008 at 12:07 AM.

  7. #47

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,728

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by VaxoP
    neat thanks

    do you know if this c++ function can decrypt a string encrypted by the vb6 function?
    I am at work now, so I cannot try it, but I'm pretty sure you can't.
    Unless in the background VB6 uses the same C++ functions for random numbers, but I really doubt it.

  8. #48
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Re: VB - 31 Bit Encryption function

    hi im new to vb. can u teach me how to decrypt using ur code, and last thing
    how do i use this code to display in my textbox3?

    [vbcode] Debug.Print(Len(S), Len(Text2.Text), Asc(Mid(S, 36, 1)), Asc(Mid(S, 37, 1)))[/vbcode]

  9. #49
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Re: VB - 31 Bit Encryption function

    hi im new to vb. can u teach me how to decrypt using ur code, and last thing
    how do i use this code to display in my textbox3?

    Code:
    Debug.Print(Len(S), Len(Text2.Text), Asc(Mid(S, 36, 1)), Asc(Mid(S, 37, 1)))

  10. #50

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,728

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by pohyf
    hi im new to vb. can u teach me how to decrypt using ur code
    To encrypt:
    result = RndCrypt("string to encrypt", "password")

    To decrypt
    result = RndCrypt("string to decrypt", "password")

  11. #51
    New Member
    Join Date
    Dec 2008
    Posts
    7

    Re: VB - 31 Bit Encryption function

    CVMichael,

    After 10 months, I want to ask something about this useful code. I didn't understand how to displaying "decrypted strings" on a textbox.

    1) I want to encrypt a text (with textbox and cmd),
    2) send it database,
    3) than decrypt it,
    4) and show it in a textbox correctly.

    I want to make a database protection, thats all.

    I couldn't show the text in a textbox correctly. I think you answered it but I guess I missed the point. Please help me.

    My second question is, is there any limitation about password character? For example, can I use 100 characters here? And how much is this important about protection?

  12. #52

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,728

    Re: VB - 31 Bit Encryption function

    When you encrypt, the result is binary data, therefore you have to encode it back to a string format, assuming that you want to hold the data into a text/varchar data type in the database.

    You have to encode the encrypted string using Base64:
    http://www.vbforums.com/showpost.php...53&postcount=4

    Like this:
    To encrypt:
    plain_text = data from text box, file, etc.
    Encrypted_string = Base64Encoding(RndCrypt(plain_text, "password"))
    Store result in database = Encrypted_string

    To decrypt:
    Encrypted_string = Read from database
    plain_text = RndCrypt(Base64Decoding(Encrypted_string), "password")


    But I suggest that you use my improved version of the RndCrypt here:
    VB6 - 31 Bit Encryption - To the Next Level

    The encryption is stronger, you can even set how strong you want it to be (but it's still 31 bit), it just encrypts many times. And it also has Base64 integrated.

    There is no limitation on the password length, and the longer the password, the better the encryption. This is true to any encryption...

  13. #53
    New Member
    Join Date
    Dec 2008
    Posts
    7

    Resolved Re: VB - 31 Bit Encryption function

    But I suggest that you use my improved version of the RndCrypt here:
    VB6 - 31 Bit Encryption - To the Next Level

    The encryption is stronger, you can even set how strong you want it to be (but it's still 31 bit), it just encrypts many times. And it also has Base64 integrated.
    This is awesome! I am using it know. Thanks a lot!

    And before I finish my post, I want to say that, "you are genius".

  14. #54
    New Member
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    4

    Re: VB - 31 Bit Encryption function

    Hey CVMichael

    I found a bug in your function RndCrypt.

    ================
    This not working
    ================
    Password: transcom
    Text to encrypt: om

    ===========
    This working
    ===========
    Password: transcom
    Text to encrypt: Om

    RndCrypt Function return (first) character NUL ( dec: 0 Hex:0 Oct: 000 Char :NUL (null) )

    Therefore the string is empty.

    Otherwise nice code

    // Best regards Pragma.
    Last edited by Pragma; Oct 20th, 2010 at 06:17 AM.

  15. #55

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,728

    Re: VB - 31 Bit Encryption function

    Actually... it's working fine...

    If you put the encrypted string in a text box, then yes, it won't display the text because of the NULL(s). But YOU ARE NOT SUPPOSED to put binary data into a text box in the first place !!!

    If you want to display binary data, then convert it to HEX, or Base64, so that the result is TEXT not binary.

    This is a limitation of the text box control, not the encryption function.

    You can do a simple test to prove that:
    Code:
    Dim EncStr As String
    
    EncStr = RndCrypt("om", "transcom")
    
    MsgBox RndCrypt(EncStr, "transcom")
    Your text box will show "om", this means that the encrypted string WAS stored (and encrypted) properly.
    Last edited by CVMichael; Oct 20th, 2010 at 07:50 AM.

  16. #56
    New Member
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    4

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by CVMichael View Post
    Actually... it's working fine...

    If you put the encrypted string in a text box, then yes, it won't display the text because of the NULL(s). But YOU ARE NOT SUPPOSED to put binary data into a text box in the first place !!!

    If you want to display binary data, then convert it to HEX, or Base64, so that the result is TEXT not binary.

    This is a limitation of the text box control, not the encryption function.

    You can do a simple test to prove that:
    Code:
    Dim EncStr As String
    
    EncStr = RndCrypt("om", "transcom")
    
    MsgBox RndCrypt(EncStr, "transcom")
    Your text box will show "om", this means that the encrypted string WAS stored (and encrypted) properly.
    Hey

    My bad sorry
    I forgot to save/read file in binary mode.

    // Best regards Pragma
    Last edited by Pragma; Oct 20th, 2010 at 10:17 AM.

  17. #57
    Member
    Join Date
    Sep 2013
    Posts
    32

    Post Re: VB - 31 Bit Encryption function

    Code:
    Private Function AddByte(ByRef b As Byte, ByRef a As Byte) As Byte
            Dim c As Short = CShort(b) + CShort(a)
            If c > 255 Then
                Return CByte(c - 256)
            Else
                Return CByte(c)
            End If
        End Function
    
        Private Function TakeByte(ByRef b As Byte, ByRef a As Byte) As Byte
            Dim c As Short = CShort(b) - CShort(a)
            If c < 0 Then
                Return CByte(c + 256)
            Else
                Return CByte(c)
            End If
        End Function
    
        Private Function AddByteArray(ByRef b As Byte, ByRef a() As Byte) As Byte
            For i = 0 To a.Length - 1
                b = AddByte(b, a(i))
            Next
    
            Return b
        End Function
    
        Private Function TakeByteArray(ByRef b As Byte, ByRef a() As Byte) As Byte
            For i = 0 To a.Length - 1
                b = TakeByte(b, a(i))
            Next
    
            Return b
        End Function
    
        Private Function AddByteArray(ByRef a() As Byte, ByRef b As Byte) As Byte
            For i = 0 To a.Length - 1
                b = AddByte(a(i), b)
            Next
    
            Return b
        End Function
    
        Private Function TakeByteArray(ByRef a() As Byte, ByRef b As Byte) As Byte
            For i = 0 To a.Length - 1
                b = TakeByte(a(i), b)
            Next
    
            Return b
        End Function
    
        Public Function Encrypt_v2(ByRef stringValue As String) As String
            Return System.Text.Encoding.Default.GetChars(Encrypt_v2(System.Text.Encoding.Default.GetBytes(stringValue)))
        End Function
    
        Public Function Encrypt_v2(ByRef stringValue As String, ByRef passwordValue As String) As String
            Return System.Text.Encoding.Default.GetChars(Encrypt_v2(System.Text.Encoding.Default.GetBytes(stringValue), System.Text.Encoding.Default.GetBytes(passwordValue)))
        End Function
    
        Public Function Encrypt_v2(ByRef arrayBytes() As Byte) As Byte()
            ' we need to fill in a blank array.
            Dim RandomByteArray(arrayBytes.Length - 1) As Byte
            Dim ConstByteArray() As Byte = New Byte() {107, 68, 12, 99, 88, 100, 66, 14}
            Dim Result(arrayBytes.Length * 2 - 1) As Byte
            Dim x As Integer = -1
    
            r.NextBytes(RandomByteArray)
    
            For i = 0 To arrayBytes.Length - 1
                x += 1
                Result.SetValue(TakeByteArray(AddByte(arrayBytes(i), RandomByteArray(i)), ConstByteArray), x)
                x += 1
                Result.SetValue(TakeByteArray(RandomByteArray(i), ConstByteArray), x)
            Next
    
            Return Result
        End Function
    
        Public Function Encrypt_v2(ByRef arrayBytes() As Byte, ByRef passwordByte() As Byte) As Byte()
            ' we need to fill in a blank array.
            Dim RandomByteArray(arrayBytes.Length - 1) As Byte
            Dim Result(arrayBytes.Length * 2 - 1) As Byte
            Dim x As Integer = -1
    
            r.NextBytes(RandomByteArray)
    
            For i = 0 To arrayBytes.Length - 1
                x += 1
                Result.SetValue(TakeByteArray(AddByte(arrayBytes(i), RandomByteArray(i)), passwordByte), x)
                x += 1
                Result.SetValue(TakeByteArray(RandomByteArray(i), passwordByte), x)
            Next
    
            Return Result
        End Function
    
        Public Function Decrypt_v2(ByRef stringValue As String) As String
            Return System.Text.Encoding.Default.GetChars(Decrypt_v2(System.Text.Encoding.Default.GetBytes(stringValue)))
        End Function
    
        Public Function Decrypt_v2(ByRef stringValue As String, ByRef passwordValue As String) As String
            Return System.Text.Encoding.Default.GetChars(Decrypt_v2(System.Text.Encoding.Default.GetBytes(stringValue), System.Text.Encoding.Default.GetBytes(passwordValue)))
        End Function
    
        Public Function Decrypt_v2(ByRef arrayBytes() As Byte) As Byte()
            If arrayBytes.Length Mod 2 = 1 Then Return New Byte() {}
            Dim ConstByteArray() As Byte = New Byte() {107, 68, 12, 99, 88, 100, 66, 14}
            Dim Result(arrayBytes.Length * 0.5 - 1) As Byte
            Dim x As Integer = arrayBytes.Length - 1
    
            For i = Result.Length - 1 To 0 Step -1
                Result.SetValue(TakeByte(AddByteArray(ConstByteArray, arrayBytes(x - 1)), AddByteArray(ConstByteArray, arrayBytes(x))), i)
                x -= 2
            Next
    
            Return Result
        End Function
    
        Public Function Decrypt_v2(ByRef arrayBytes() As Byte, ByRef passwordByte() As Byte) As Byte()
            If arrayBytes.Length Mod 2 = 1 Then Return New Byte() {}
            Dim Result(arrayBytes.Length * 0.5 - 1) As Byte
            Dim x As Integer = arrayBytes.Length - 1
    
            For i = Result.Length - 1 To 0 Step -1
                Result.SetValue(TakeByte(AddByteArray(passwordByte, arrayBytes(x - 1)), AddByteArray(passwordByte, arrayBytes(x))), i)
                x -= 2
            Next
    
            Return Result
        End Function

  18. #58

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,728

    Re: VB - 31 Bit Encryption function

    I am flattered that you posted in my thread, but I fail to see what your code in .NET has anything to do with my 31 bit encryption?

    PS. Welcome to VB Forums!

  19. #59
    Member
    Join Date
    Sep 2013
    Posts
    32

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by CVMichael View Post
    I am flattered that you posted in my thread, but I fail to see what your code in .NET has anything to do with my 31 bit encryption?

    PS. Welcome to VB Forums!
    Sorry I came to this forum from another website, (I searched VB.net Encryptions)

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.