-
Jan 5th, 2008, 08:36 PM
#41
Frenzied Member
Re: VB - 31 Bit Encryption function
does anyone know if this function can be ported to c++?
-
Jan 7th, 2008, 01:54 PM
#42
Frenzied Member
Re: VB - 31 Bit Encryption function
does anyone know if this function can be ported to c++?
-
Jan 7th, 2008, 06:51 PM
#43
Re: VB - 31 Bit Encryption function
Most likely...
Let me open C++ and try it...
-
Jan 7th, 2008, 08:24 PM
#44
Frenzied Member
Re: VB - 31 Bit Encryption function
Originally Posted by CVMichael
Most likely...
Let me open C++ and try it...
thank you soooo much CVMichael.. you are a king among kings
-
Jan 7th, 2008, 09:05 PM
#45
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 09:11 PM.
-
Jan 8th, 2008, 01:01 AM
#46
Frenzied Member
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 01:07 AM.
-
Jan 8th, 2008, 08:24 AM
#47
Re: VB - 31 Bit Encryption function
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.
-
Feb 28th, 2008, 06:54 AM
#48
New Member
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]
-
Mar 1st, 2008, 09:29 AM
#49
New Member
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)))
-
Mar 1st, 2008, 07:48 PM
#50
Re: VB - 31 Bit Encryption function
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")
-
Dec 20th, 2008, 12:19 AM
#51
New Member
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?
-
Dec 20th, 2008, 01:32 AM
#52
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...
-
Dec 22nd, 2008, 02:42 AM
#53
New Member
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".
-
Oct 20th, 2010, 05:21 AM
#54
New Member
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.
-
Oct 20th, 2010, 07:44 AM
#55
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.
-
Oct 20th, 2010, 10:14 AM
#56
New Member
Re: VB - 31 Bit Encryption function
Originally Posted by CVMichael
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.
-
Sep 26th, 2013, 09:10 PM
#57
Member
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
-
Sep 27th, 2013, 09:33 AM
#58
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!
-
Oct 1st, 2013, 08:42 PM
#59
Member
Re: VB - 31 Bit Encryption function
Originally Posted by CVMichael
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|