diff --git a/Editor/EncryptionVM/VirtualMachineSimulator.cs b/Editor/EncryptionVM/VirtualMachineSimulator.cs index 9bfd308..67796dd 100644 --- a/Editor/EncryptionVM/VirtualMachineSimulator.cs +++ b/Editor/EncryptionVM/VirtualMachineSimulator.cs @@ -34,6 +34,27 @@ namespace Obfuz.EncryptionVM int decryptedValue = _opCodes[i].Decrypt(encryptedValue, _secretKey, i); Assert.AreEqual(value, decryptedValue); } + + int ops = 11223344; + int salt = 789; + Assert.AreEqual(1, Decrypt(Encrypt(1, ops, salt), ops, salt)); + Assert.AreEqual(1L, Decrypt(Encrypt(1L, ops, salt), ops, salt)); + Assert.AreEqual(1.0f, Decrypt(Encrypt(1.0f, ops, salt), ops, salt)); + Assert.AreEqual(1.0, Decrypt(Encrypt(1.0, ops, salt), ops, salt)); + + byte[] strBytes = Encrypt("abcdef", ops, salt); + Assert.AreEqual("abcdef", DecryptString(strBytes, 0, strBytes.Length, ops, salt)); + var arr = new byte[100]; + for (int i = 0; i < arr.Length ; i++) + { + arr[i] = (byte)i; + } + EncryptBlock(arr, ops, salt); + DecryptBlock(arr, ops, salt); + for (int i = 0; i < arr.Length; i++) + { + Assert.AreEqual(i, arr[i]); + } } private List DecodeOps(int ops) diff --git a/Runtime/EncryptorBase.cs b/Runtime/EncryptorBase.cs index f897a25..7c1c5bd 100644 --- a/Runtime/EncryptorBase.cs +++ b/Runtime/EncryptorBase.cs @@ -1,4 +1,5 @@ -using System; +using JetBrains.Annotations; +using System; using System.Text; using Unity.Collections.LowLevel.Unsafe; using UnityEngine.Assertions; @@ -87,10 +88,11 @@ namespace Obfuz fixed (byte* dstBytePtr = &encryptedBytes[0]) { int* dstIntPtr = (int*)dstBytePtr; - + int last = 0; for (int i = 0; i < intArrLength; i++) { - dstIntPtr[i] = Encrypt(dstIntPtr[i], ops, salt); + last ^= Encrypt(dstIntPtr[i], ops, salt); + dstIntPtr[i] = last; } } for (int i = intArrLength * 4; i < length; i++) @@ -108,9 +110,11 @@ namespace Obfuz int* srcIntPtr = (int*)srcBytePtr; int* dstIntPtr = (int*)dstBytePtr; + int last = 0; for (int i = 0; i < intArrLength; i++) { - dstIntPtr[i] = Encrypt(srcIntPtr[i], ops, salt); + last ^= Encrypt(srcIntPtr[i], ops, salt); + dstIntPtr[i] = last; } } } @@ -137,10 +141,12 @@ namespace Obfuz fixed (byte* dstBytePtr = &decryptedBytes[0]) { int* dstIntPtr = (int*)dstBytePtr; - + int last = 0; for (int i = 0; i < intArrLength; i++) { - dstIntPtr[i] = Decrypt(dstIntPtr[i], ops, salt); + int oldLast = last; + last = dstIntPtr[i]; + dstIntPtr[i] = Decrypt(last ^ oldLast, ops, salt); } } for (int i = intArrLength * 4; i < length; i++) @@ -157,10 +163,12 @@ namespace Obfuz { int* srcIntPtr = (int*)srcBytePtr; int* dstIntPtr = (int*)dstBytePtr; - + int last = 0; for (int i = 0; i < intArrLength; i++) { - dstIntPtr[i] = Decrypt(srcIntPtr[i], ops, salt); + int oldLast = last; + last = srcIntPtr[i]; + dstIntPtr[i] = Decrypt(last ^ oldLast, ops, salt); } } } @@ -192,10 +200,11 @@ namespace Obfuz fixed (byte* dstBytePtr = &data[0]) { int* dstIntPtr = (int*)dstBytePtr; - + int last = 0; for (int i = 0; i < intArrLength; i++) { - dstIntPtr[i] = Encrypt(dstIntPtr[i], ops, salt); + last ^= Encrypt(dstIntPtr[i], ops, salt); + dstIntPtr[i] = last; } } for (int i = intArrLength * 4; i < length; i++) @@ -212,10 +221,12 @@ namespace Obfuz fixed (byte* dstBytePtr = &data[0]) { int* dstIntPtr = (int*)dstBytePtr; - + int last = 0; for (int i = 0; i < intArrLength; i++) { - dstIntPtr[i] = Decrypt(dstIntPtr[i], ops, salt); + int oldLast = last; + last = dstIntPtr[i]; + dstIntPtr[i] = Decrypt(oldLast ^ last, ops, salt); } } for (int i = intArrLength * 4; i < length; i++)