- Home /
Trying to Create Custom Encryption
Hello, I am trying to create an encryption for my game. I have the thing all laid out, but I have not been able to get it to work. The file is a bit big so I linked the file. I made sure I added the namespace to my code. I then typed
MyEncryptionConverter converter = new MyEncryptionConverter();Once I had that, I tried to access the function to write to the file using:
converter.WriteFile(fileLines, filePath);I feel like it should be working, but when I open the file, none of my code has been encrypted! Help is greatly appreciated!
How would we know what your $$anonymous$$yEncryptionConverter()
does?...
Answer by Bunny83 · Jan 16, 2019 at 02:33 AM
You have several issues here. I will seperate them into pure programming issues, logic issues and finally some advice on encryption in general.
First of all strings are immutable classes. That means once a string is created it can't be changed. So all you can do with strings is replace them with new strings. The Replace method does not change the string (as i said that isn't possible). Instead it returns a new string where all occurences of the search string has been replaced. Since you do not do anything with the return value, all the Replace calls are pointless.
Next issue is in a foreach loop you can not modify the for loop variable. So even when you use
fileLine = fileLine.Replace("A", "B");
It won't work since the actual string in the array can't be changed inside a foreeach loop.
So you would need to use a "normal" for loop with an index variable.
Now here comes the logic error that renders your encryption useless or that turns it into a "garbage generator". Your first replace replaces all "A" with "B". Later you have another replace that replaces all "B" with "C". However that means you do not only replace the "B"s that were in the string originally, but also those which were "A"s in the first place. This actually cascades downwards. You later replace C with D, then D with E then E with F ... This goes on to Z. So essentially all capital letters will be Z in the end. At the end you have another replace where you replace Z with A. So all capital letters in the original string will become A in the end. This process can not be reversed because a string like "HELLO WORLD"
would become "AAAAA AAAAA"
. How would you turn that back to HELLO WORLD?
So your approach is generally flawed. Besides that using that many Replace calls on the whole line would also generate tons of garbage memory.
Finally some generally notes on encryption. The kind of encryption you tried to implement is one of the easiest and one of the most unsafe encryptions out there. The same letter always encodes to the same encoded letter. In general it's usually pointless to implement your owh encryption. With C# / .NET you can simply use advanced encryption algorithms like AES. Though if you store encrypted data on a client machine it will never be "safe" as the user could always reverse engineer your application and figure out the used key.
Your answer
Follow this Question
Related Questions
Remove empty line from string 2 Answers
Multiple Cars not working 1 Answer
Any reason not to use namespace for scripts for asset store 0 Answers
Replace the first string C# 1 Answer
Distribute terrain in zones 3 Answers