From c17397eac06d3cd8f26917bdab7d11ded3e11b63 Mon Sep 17 00:00:00 2001 From: walon Date: Tue, 13 May 2025 10:35:50 +0800 Subject: [PATCH] =?UTF-8?q?Block=E5=92=8Cbytes=E5=8A=A0=E5=AF=86=EF=BC=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=B8=8E=E4=B8=8A=E4=B8=80=E4=B8=AA=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EncryptionVM/VirtualMachineSimulator.cs | 21 +++++++++++ Runtime/EncryptorBase.cs | 35 ++++++++++++------- 2 files changed, 44 insertions(+), 12 deletions(-) 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++)