Block和bytes加密,数据与上一个数据相关

backup
walon 2025-05-13 10:35:50 +08:00
parent bf79067e75
commit c17397eac0
2 changed files with 44 additions and 12 deletions

View File

@ -34,6 +34,27 @@ namespace Obfuz.EncryptionVM
int decryptedValue = _opCodes[i].Decrypt(encryptedValue, _secretKey, i); int decryptedValue = _opCodes[i].Decrypt(encryptedValue, _secretKey, i);
Assert.AreEqual(value, decryptedValue); 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<ushort> DecodeOps(int ops) private List<ushort> DecodeOps(int ops)

View File

@ -1,4 +1,5 @@
using System; using JetBrains.Annotations;
using System;
using System.Text; using System.Text;
using Unity.Collections.LowLevel.Unsafe; using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Assertions; using UnityEngine.Assertions;
@ -87,10 +88,11 @@ namespace Obfuz
fixed (byte* dstBytePtr = &encryptedBytes[0]) fixed (byte* dstBytePtr = &encryptedBytes[0])
{ {
int* dstIntPtr = (int*)dstBytePtr; int* dstIntPtr = (int*)dstBytePtr;
int last = 0;
for (int i = 0; i < intArrLength; i++) 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++) for (int i = intArrLength * 4; i < length; i++)
@ -108,9 +110,11 @@ namespace Obfuz
int* srcIntPtr = (int*)srcBytePtr; int* srcIntPtr = (int*)srcBytePtr;
int* dstIntPtr = (int*)dstBytePtr; int* dstIntPtr = (int*)dstBytePtr;
int last = 0;
for (int i = 0; i < intArrLength; i++) 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]) fixed (byte* dstBytePtr = &decryptedBytes[0])
{ {
int* dstIntPtr = (int*)dstBytePtr; int* dstIntPtr = (int*)dstBytePtr;
int last = 0;
for (int i = 0; i < intArrLength; i++) 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++) for (int i = intArrLength * 4; i < length; i++)
@ -157,10 +163,12 @@ namespace Obfuz
{ {
int* srcIntPtr = (int*)srcBytePtr; int* srcIntPtr = (int*)srcBytePtr;
int* dstIntPtr = (int*)dstBytePtr; int* dstIntPtr = (int*)dstBytePtr;
int last = 0;
for (int i = 0; i < intArrLength; i++) 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]) fixed (byte* dstBytePtr = &data[0])
{ {
int* dstIntPtr = (int*)dstBytePtr; int* dstIntPtr = (int*)dstBytePtr;
int last = 0;
for (int i = 0; i < intArrLength; i++) 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++) for (int i = intArrLength * 4; i < length; i++)
@ -212,10 +221,12 @@ namespace Obfuz
fixed (byte* dstBytePtr = &data[0]) fixed (byte* dstBytePtr = &data[0])
{ {
int* dstIntPtr = (int*)dstBytePtr; int* dstIntPtr = (int*)dstBytePtr;
int last = 0;
for (int i = 0; i < intArrLength; i++) 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++) for (int i = intArrLength * 4; i < length; i++)