does anyone know if this function can be ported to c++?
does anyone know if this function can be ported to c++?
does anyone know if this function can be ported to c++?
Most likely...
Let me open C++ and try it...
thank you soooo much CVMichael.. you are a king among kingsOriginally Posted by CVMichael
![]()
![]()
![]()
Here it is...
I coded it in Turbo C++ Version 3.0, but it should work in any compiler... (hopefully)
[Edit]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; }
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.
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.
I am at work now, so I cannot try it, but I'm pretty sure you can't.Originally Posted by VaxoP
Unless in the background VB6 uses the same C++ functions for random numbers, but I really doubt it.
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]
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)))
To encrypt:Originally Posted by pohyf
result = RndCrypt("string to encrypt", "password")
To decrypt
result = RndCrypt("string to decrypt", "password")
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?
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...
This is awesome!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.I am using it know. Thanks a lot!
And before I finish my post, I want to say that, "you are genius".![]()
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.
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:
Your text box will show "om", this means that the encrypted string WAS stored (and encrypted) properly.Code:Dim EncStr As String EncStr = RndCrypt("om", "transcom") MsgBox RndCrypt(EncStr, "transcom")
Last edited by CVMichael; Oct 20th, 2010 at 07:50 AM.