Being poised with a project that require transporting sensitive data from a backend (in Go) to a frontend (in AGK) and AppGameKit having no built in security/cryptography functionality I had to roll my own as it were. Before you ask, yes it do go through a SSL layer as defined by the cert on the backend - but even so, I felt uncomfortable having the traffic go from server to client without some extra protection.
Anyhow, on with the code:
main()
function main()
key as string
payload as string
encrypted as string
key = "y0GjsUb50HZntRWX712F"
payload = "1234 email@post.com beep, bopp & blip æøåÆØÅ"
encrypted = encrypt(key, payload)
print(encrypted)
print(decrypt(key, encrypted))
sync()
sleep(10000)
endFunction
function encrypt(key as string, in as string)
outTemp as integer[]
out as string
pLen = len(in) - 1
kLen = len(key) - 1
kCount = 0
for i = 0 to pLen
temp = asc(mid(in, i + 1, 1))
if kCount > kLen
kCount = 0
endif
temp = temp + asc(mid(key, kCount + 1, 1))
inc kCount
outTemp.insert(temp)
next i
for i = 0 to outTemp.length
out = out + str(outTemp[i]) + ","
next i
endFunction out
function decrypt(key as string, in as string)
out as string
sLen = CountStringTokens(in, ",")
inTemp as integer
kVal as integer
kLen = len(key) - 1
kCount = 0
for i = 0 to sLen
if kCount > kLen
kCount = 0
endif
inTemp = val(GetStringToken(in, ",", i + 1))
kVal = asc(mid(key, kCount + 1, 1))
inc kCount
out = out + chr(inTemp - kVal)
next i
endFunction out
The above is pretty much to just copy-paste into AppGameKit and good to go. Included a bit of print commands for showing the encrypted data and the same when decrypted. As for what to do on the server, you'll have to translate it to whatever language you prefer. Unless of course you run AppGameKit on the server as well for some odd reason. My encoding code in Go look like this:
func Encrypt(key string, in string) string {
var outA []int
var temp int
var out string
pLen := len(in) - 1
kLen := len(key) - 1
kCount := 0
for i := 0; i <= pLen; i++ {
temp = int(in[i])
if kCount > kLen {
kCount = 0
}
temp += int(key[kCount])
kCount++
outA = append(outA, temp)
}
for i := range outA {
out += strconv.Itoa(outA[i]) + ","
}
return out
}
As for how to generate and distribute the password, and assign one to each user and so forth - I am sure you can figure something out.
Using a 20 character key, with characters including 0..9, a..z and A..Z you get 6.200.000.000.000.000.000.000 possible combinations - assuming a hacker try and brute-force an intercepted package, at a billion guesses per minute it'll take just shy of 6 million years before having a 50% chance of hitting the right combination.