• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Data Encryption using a Cipher

Level 31
Joined
Jul 10, 2007
Messages
6,306
Many people love to make save/load codes only loadable given a specific username. Most people do this by saving the username into the save/load code and then comparing what was stored into the code to the user's name. This makes the code quite a bit longer. A better method is to make encryption use an encryption key dependent upon the player name, thus not having to store the player name at all.

Given a set of 1000 encryption keys for 0 to 1000, a save/load code can be encrypted with a set of the keys, and this combination is based entirely on the name of the player.

JASS:
set k = StringHash(GetPlayerName(p.get))
if (k < 0) then
    set k = -k
endif

From there loop, store the keys (10 keys possible per player)
JASS:
//where curp is (p*11)+12
loop
    set playerKeys[curp+playerKeyCount] = k-k/10*10
    set playerKeyCount = playerKeyCount+1
    set k = k/10
    exitwhen k == 0
endloop

From there, store the last cipher used within the save/load code. If last cipher used was 0, start at 1 (upon load, load up last cipher from code).

The numbers generated represent the current base. If the player name resulted in a cipher of something like 18391854, it would be
Code:
0: 1
1: 8
2: 3
3: 9
4: 1
5: 8
6: 5
7: 4

Because this isn't being used for actual encryption, repetitions don't matter.

10 would be 81 in this example.

When the counter exceeds 3 digits, reset the counter to 1. In this case, that would occur at 444.

Given 1000 encryptions keys, this style would cycle through a set of them.

8,3,9,1,8,5,4,81,88,83,89,81,88,85,84,31,38,33,39,31,38,35,34, etc. This ensures that the base doesn't need to be saved in the code if wanting to make it appear random and it ensures that the player name doesn't need to be saved into the code to make it run only for that player (the correct encryption key is retrieved by loading up the last cipher used and using the player keys to build the correct key from the array).

If wanting to use a combination of keys to encrypt the data (maybe a different key for every single value?), then simply store the last cipher used (if went through 45 keys from 1 to 45, store 45). From there, load up the last and decrypt backwards (last set of data using 45, second last with 44, etc). Be sure to store the 45 somewhere so that the cycling can continue. If at 0, go to the highest possible value. If at the highest possible value, reset to 1.

This same technique can be used for scramble keys.
 
Top