Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Very simple string Encryption / Decryption
Started by Darkmod Oct 31 2011 09:21 PM

- - - - -
17 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Darkmod

[GLOBAL: userInfoPane.html]
Darkmod
  • Contributor

  • 119 posts
  • Corona SDK

This is a very simple Encryption and provides very little protection. It can be reversed very easily. But its better than raw data.

-Darkmod
[lua=auto:0]local function convert( chars, dist, inv )
return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
end

local function crypt(str,k,inv)
local enc= "";
for i=1,#str do
if(#str-k[5] >= i or not inv)then
for inc=0,3 do
if(i%4 == inc)then
enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
break;
end
end
end
end
if(not inv)then
for i=1,k[5] do
enc = enc .. string.char(math.random(32,126));
end
end
return enc;
end

local enc1 = {29, 58, 93, 28, 27};
local str = "This is an encrypted string.";
local crypted = crypt(str,enc1)
print("Encryption: " .. crypted);
print("Decryption: " .. crypt(crypted,enc1,true));

-- returns:
-- Encryption: /f&1Zg0=%*JbV>Z^Ik!p=B@'7cHMY<
-- Decryption: This is an encrypted string.

uid: 7177 topic_id: 17199 reply_id: 317199


[TOPIC: post.html]
#2

JayantV

[GLOBAL: userInfoPane.html]
JayantV
  • Corona Geek

  • 1,740 posts
  • Corona SDK

have not tested it completely, but you will have to be careful that you do not end up with characters that break the strings.

for example
 local myEncodedString = "/f&1Zg0=<l<#Ia/7Kr""Zq10Dl$KT)8b>%*JbV>Z^Ik!p=B@'7cHMY<"

 print(decode(myEncodedString))

let's hope that this works... without errors

cheers,

?:)
uid: 3826 topic_id: 17199 reply_id: 64861


[TOPIC: post.html]
#3

jonathanbeebe

[GLOBAL: userInfoPane.html]
jonathanbeebe
  • Contributor

  • 654 posts
  • Alumni

Have you taken a look at Corona's crypto support?

http://developer.anscamobile.com/reference/crypto
uid: 52430 topic_id: 17199 reply_id: 64963


[TOPIC: post.html]
#4

Darkmod

[GLOBAL: userInfoPane.html]
Darkmod
  • Contributor

  • 119 posts
  • Corona SDK

Yes crypto is a one way hashing encryption that can't be Decrypted locally.
uid: 7177 topic_id: 17199 reply_id: 64973


[TOPIC: post.html]
#5

Omnigeek Media

[GLOBAL: userInfoPane.html]
Omnigeek Media
  • Corona Geek

  • 2,975 posts
  • Corona SDK

what are you planning on encrypting with this? What is its purpose?
uid: 19626 topic_id: 17199 reply_id: 64974


[TOPIC: post.html]
#6

Sven.Lua

[GLOBAL: userInfoPane.html]
Sven.Lua
  • Contributor

  • 145 posts
  • Corona SDK

Maybe he want to hide "secrets" placed in the app - as examples
a) solution words for riddles
b) cheat codes
c) text in book like apps with IAP
d) level data with infos for hidden treasures
...

for c) and d) you can't use hash based encryption like MD5 because it creates something like a check sum and is only for prove of validity - so you could use it for a) and b)

On other hand I do ask myself if Apple will be disturbed by using own decryption routines. They ask if you use encryption in your app. I think that's an relict from the cold war times when encryption technology was forbidden for export...
uid: 70114 topic_id: 17199 reply_id: 65007


[TOPIC: post.html]
#7

JayantV

[GLOBAL: userInfoPane.html]
JayantV
  • Corona Geek

  • 1,740 posts
  • Corona SDK

Have you tried the Rot13 or something similar? Use crypto, then you will also have issues in selling your app outside of US to some countries.

cheers,

?:)
uid: 3826 topic_id: 17199 reply_id: 65009


[TOPIC: post.html]
#8

Omnigeek Media

[GLOBAL: userInfoPane.html]
Omnigeek Media
  • Corona Geek

  • 2,975 posts
  • Corona SDK

Regarding using Apple's provided encryption, from iTunes Connect:

If I use the encryption available in the iOS or Mac OS X, do I still need to obtain U.S. government approval? What should I do about the source code, since I can’t provide it to them?
You still need to submit your app to the U.S. Bureau of Industry and Security (BIS) for approval because they want to know how you are using encryption in your particular app, even though they have already approved the encryption in the iOS and Mac OS X. You are responsible for obtaining separate approval.

It is not necessary to provide Apple’s source code to the government because it has already been reviewed and approved by the U.S. Bureau of Industry and Security (BIS).

So yes, if you use the Corona SDK crypt libraries or anything provided by iOS then you need to go through the export compliance process with the government before you submit your app to Apple.

Regarding ROT13. I sent a message to Apple to ask about ROT13 and MIME's Base64 Encode/Decode process. The purpose of ROT13 is to encrypt data so its not readable and I suspect that it will require the government's export compliance process. I'm unsure about Base64 since its purpose really isn't to encrypt data as much as make binary data safe for transport over text system. Hopefully Apple will find time in their busy day to answer my questions.
uid: 19626 topic_id: 17199 reply_id: 65042


[TOPIC: post.html]
#9

JayantV

[GLOBAL: userInfoPane.html]
JayantV
  • Corona Geek

  • 1,740 posts
  • Corona SDK

ROT13 and Base64 are used in emails and no one really needs to get a certificate from the BIS aka Big Brothers...

cheers,

?:)
uid: 3826 topic_id: 17199 reply_id: 65048


[TOPIC: post.html]
#10

Skatan

[GLOBAL: userInfoPane.html]
Skatan
  • Contributor

  • 547 posts
  • Corona SDK

Good code, though I'm wondering about what JayantV posted earlier.. What about ending up with characters that break a string? Anyone gotten around that?
uid: 14018 topic_id: 17199 reply_id: 106279


[TOPIC: post.html]
#11

Omnigeek Media

[GLOBAL: userInfoPane.html]
Omnigeek Media
  • Corona Geek

  • 2,975 posts
  • Corona SDK

Mime's Base64's purpose is to take any binary data and produce an ASCII safe string that can be sent over systems that react badly to binary data and control characters.

ROT13 really only deals with ASCII letters: A becomes N, B becomes O, C becomes P and is meant to obscure words only. If you have binary bytes or control characters (and even numbers and symbols) those will remain unchanged.

If you have things that will break a string I would recommend Mime's Base64 encode. You can also use the standard URL Encode/Decode to manage the non-ASCII characters and things that break strings then apply your encryption to the now ASCII safe string. Or visa versa. Encrypt first, then URL Encode/Decode it.
uid: 19626 topic_id: 17199 reply_id: 106298


[TOPIC: post.html]
#12

User25689

[GLOBAL: userInfoPane.html]
User25689
  • Enthusiast

  • 86 posts
  • Corona SDK

I use a very similar encryption method in my current app to save the users and the game save data. Since I use in-app purchases, I don't want them to go in and change all their details. Here is my process:

userInfo table-->Json Encode-->Proprietary key-based rotation algorithm-->Stored in a string
gameData table-->Json Encode-->Proprietary key-based rotation algorithm-->Stored in a string

userInfoString-->Base64 (Better for SQL to prevent errors)
gameDataString-->Base64

userInfoString-->SHA-384 key-hashed = userInfoHash
gameDataString-->SHA-384 key-hashed = gameDataHash
I then store the base64'd data and it's corresponding hash into a SQL database. When the app loads, I compare the stored data to it's hash and if it has been changed, I reset the users data. I also hash the entire database itself to prevent an injection attack. Each hash uses a different key that is based on the user, so it can't be shared to other users but still works when playing on your iPad or one of your other devices.

This offers way more security then just hashing or just encrypting and the result is still a small, quickly created file. The final file is still incredibly small since all of my saved data is essentialy just a big json text file. The resulting DB file was less than 100kb max size.

As a side note, all types of encryption can be reversed by someone who knows what they're doing. But what I'm doing here is making that as hard as possible while still keeping quick, efficient encryption (Not going overboard). Nobody is going to brute force the hash, what they would do is run the app through a debugger and see how to encryption is ran, then find the key used to encrypt it in the devices memory. If they're able to get the correct encryption process and obtain the keys then you're out of luck, but the % of people who could do that is very minimal and they wouldn't be buying from you anyway ;)

It's also a good idea to store your key in a table, then overwrite it with blank/dummy data after encryption. This is because LUA strings are immutable and would make it a Lot easier for an attacker to find the correct memory location and obtain the key.
Hope that helps,
Dac
uid: 63320 topic_id: 17199 reply_id: 106309


[TOPIC: post.html]
#13

Darkmod

[GLOBAL: userInfoPane.html]
Darkmod
  • Contributor

  • 119 posts
  • Corona SDK

Great Info dacriburdan!
uid: 7177 topic_id: 17199 reply_id: 106331


[TOPIC: post.html]
#14

aisaksen

[GLOBAL: userInfoPane.html]
aisaksen
  • Contributor

  • 141 posts
  • Enterprise

@dacriburdan - Thank you so much for your excellent comments!

Two questions:

1) Is there a reason you preferred storing in sqlite instead of just a flat file where you appended the signature to the end of the file? Just to make it one step harder to edit the file?

2) When you say "It's also a good idea to store your key in a table, then overwrite it with blank/dummy data after encryption." Can you give some quick code or explanation of how to do this? I'm not sure how keys={"abcdef"} would be better than key="abcdef".
uid: 122310 topic_id: 17199 reply_id: 119238


[TOPIC: post.html]
#15

devguy

[GLOBAL: userInfoPane.html]
devguy
  • Observer

  • 3 posts
  • Corona SDK

Hey guys. Thanks for all your replies. I'm building an app to store sensitive data like credit card info. What is the best methods you recommend I use for this? Will Dac's methods be good enough? Like Dac mentioned, the likelihood of someone obtaining someone's device, finding the app, working out the encryption and decrypting the data - would be very low.

Any advice would be greatly appreciated!
uid: 142833 topic_id: 17199 reply_id: 132205


[TOPIC: post.html]
#16

aisaksen

[GLOBAL: userInfoPane.html]
aisaksen
  • Contributor

  • 141 posts
  • Enterprise

I think if you are stored sensitive user information like passwords and credit card info you are obligated to use very strong encryption to make sure its not stolen. I don't know if this is actually allowed, but you could consider storing all the information encrypted on your servers, and then just pulling it down as needed using https (you aren't required to file with the government if you use https).
uid: 122310 topic_id: 17199 reply_id: 132236


[TOPIC: post.html]
#17

devguy

[GLOBAL: userInfoPane.html]
devguy
  • Observer

  • 3 posts
  • Corona SDK

Thanks for your reply. Yeah I agree about the obligation - that's why I'm asking and researching. I don't think the server option is appropriate, for speed and offline access, but thanks for the idea.

Does anyone know if Corona's crypto is a solid method of encryption?
uid: 142833 topic_id: 17199 reply_id: 132397


[TOPIC: post.html]
#18

aisaksen

[GLOBAL: userInfoPane.html]
aisaksen
  • Contributor

  • 141 posts
  • Enterprise

In order to avoid these sorts of export complications, Corona's crypto library doesn't actually do any encryption...only hashing (they discussed this in some other forum thread but I don't know which one anymore). So you can use crypto to generate signatures, or verify that a message wasn't tampered with using HMAC, but not actually encrypt the message.
uid: 122310 topic_id: 17199 reply_id: 132422



[topic_controls]
[/topic_controls]