  # Decrypt

Discussion in 'Questions (Windows Mobile)' started by Big JR, May 16, 2007.

1. Hi,
I'm having trouble with encrypt/decrypt. I can encrypt the string and display it as an encrypted string but I want to save that (in the registry eventually) to be retrieved later and decrypted. The Cypto example stores the encrypted string as an array to be decrypted. I've tried taking my encrypted string and converting it to an array of bytes and decrypting that but I get a bad data error.
John

2. Were did you store the cripted data?

If you are trying to read from a file, I think that you must read as a normal file and then pass it to a variable to decript... reding as binary will read also the heading of the file wich is not a valid set of values to decript....
I havent tryed it yet but if you still get errors I'll try to help...

3. I faced the same problem until I figured out how to handle it...

I use these two subs, built up from the example, to encrypt strings for a registry, and then decrypt back.

Sub EncryptIt(a)
string() = Bit.StringToBytes(a,0,StrLength(a)) 'Convert the string to an array of bytes.
secret() = Crypto.Encrypt(ED, string()) 'Save the encrypted data.

for i = 0 to ArrayLen(secret())-1 'Show the encrypted data in the TextBox
ss = bit.DecToHex(secret(i))
If StrLength(ss)<2 then ss = "0" & ss ' Add leading zero to byte if not two digits!
s = s & ss
next
Return s
End Sub

Sub DecryptIt(aa)
y = StrLength(aa) / 2
Dim NewArray(y) as Byte ' Array may end up being 8, 16, 32... bytes long

For z = 0 to (StrLength(aa) / 2) - 1 ' Hex values are two digits long (8 byte should be 0 to 3 loop)
NewArray(z)= bit.HexToDec(StrAt(aa,z * 2) & StrAt(aa,(z * 2)+1))
Next
if ArrayLen(NewArray()) = 0 then return
string() = Crypto.Decrypt(ED,NewArray()) 'Decrypt the data.
zz = Bit.BytesToString(string(),0,ArrayLen(string())) 'Convert the array to a string.
Return zz
End Sub

Regards,
Mike

4. Many thanks Mike. Works a treat now.
John

5. That's why I use your piece of code I have one question. When I don't put dim NewArray(0) as byte in my Global section, the compiler will crash with an undeclared array .....

So I don't get the point about the Dim NewArray(y) as Byte in the Decryptit Sub. Is this how you redeclare an existing array?

Confused .....

Scub

6. From what i can "read" the code, newarray is were the crypted bytes are passed before decrypt....a sort of temporary way of storing the hole data string before decrypt

7. What I ment was there are two dim declarations for newarray.
The 1st time in the global section as newarray(0) and the second in the subsection as newarray(y).

When I remove the one in the global section, the compiler think it's missing an undeclared array: newarray(y). But that array is defined with a dim in it's sub.

That confuses me ....... still.

Scub

8. In V5 ALL arrays have to be declared in globals...if one doesnt known the length of the array then set to zero and later re-set to the final length or increment, as in this case, the array length....

9. That clarifies it! So a redimension of the array. I was looking for a way to redimension an array at runtime and just found it.

Dim B4PPC(0)
...
IsVeryNice = 250
Dim B4PPC(IsVeryNice)

Thanks Cabelguy