2025-05-11 10:49:04 +08:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using System;
|
2025-05-11 09:17:04 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.Utils
|
|
|
|
|
{
|
|
|
|
|
public static class KeyGenerator
|
|
|
|
|
{
|
2025-05-11 10:49:04 +08:00
|
|
|
|
public static byte[] GenerateKey(string initialString, int keyLength)
|
2025-05-11 09:17:04 +08:00
|
|
|
|
{
|
|
|
|
|
byte[] initialBytes = Encoding.UTF8.GetBytes(initialString);
|
|
|
|
|
using var sha512 = SHA512.Create();
|
|
|
|
|
byte[] hash = sha512.ComputeHash(initialBytes);
|
2025-05-11 10:49:04 +08:00
|
|
|
|
byte[] key = new byte[keyLength];
|
2025-05-11 09:17:04 +08:00
|
|
|
|
int bytesCopied = 0;
|
|
|
|
|
while (bytesCopied < key.Length)
|
|
|
|
|
{
|
|
|
|
|
if (bytesCopied > 0)
|
|
|
|
|
{
|
|
|
|
|
// 再次哈希之前的哈希值以生成更多数据
|
|
|
|
|
hash = sha512.ComputeHash(hash);
|
|
|
|
|
}
|
|
|
|
|
int bytesToCopy = Math.Min(hash.Length, key.Length - bytesCopied);
|
|
|
|
|
Buffer.BlockCopy(hash, 0, key, bytesCopied, bytesToCopy);
|
|
|
|
|
bytesCopied += bytesToCopy;
|
|
|
|
|
}
|
|
|
|
|
return key;
|
|
|
|
|
}
|
2025-05-11 10:49:04 +08:00
|
|
|
|
|
|
|
|
|
public static int[] ConvertToIntKey(byte[] key)
|
|
|
|
|
{
|
2025-05-11 20:12:33 +08:00
|
|
|
|
return EncryptorBase.ConvertToIntKey(key);
|
2025-05-11 10:49:04 +08:00
|
|
|
|
}
|
2025-05-11 09:17:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|