Skip to main content
 首页 » 编程设计

c#之固定长度的 Int 混淆器。有谁知道如何做到这一点

2024年02月13日21del

我正在使用泛型类将 INT 转换为 X 基数:

BaseX basex = new BaseX("abcdefghijklmnopqrstuvwxyz"); 
 
var a = basex.ToBaseX(1002); 
var b = basex.FromBaseX("aghe"); 

BaseX类如下:

public class BaseX { 
  private readonly string _digits; 
 
  public BaseX(string digits) { 
    _digits = digits; 
  } 
  public string ToBaseX(int number) { 
    var output = ""; 
    do { 
      output = _digits[number % _digits.Length] + output; 
      number = number / _digits.Length; 
    } 
    while (number > 0); 
    return output; 
  } 
 
  public int FromBaseX(string number) { 
    return number.Aggregate(0, (a, c) => a * _digits.Length + _digits.IndexOf(c)); 
  } 
} 

我使用的是小写字母,但我可以使用任何其他字母。

是否可以使基 X 中的输出始终具有相同的长度?

我想我应该使用“Multiplicative Inverse”和一些类似的映射和编码过程,但我不确定该怎么做......

我可以得到一些帮助来创建这个吗?

基本上,我的目标不是创建用于促销或 ID 混淆的随机固定长度代码,而是创建一个固定长度的 INT(数据库中的 ID)。

谢谢你, 米格尔

请您参考如下方法:

如果我对你的理解正确,你想用“零”填充生成的值。例如。如果您使用的是纯数字并且您想要一个长度为 10 的 ID,而 ID 是 1234,则填充后的 ID 将为 0000001234。

最简单的方法是填充生成的值。您可以向 BaseX 类添加一个新方法:

public string ToBaseX(int number, int width) { 
  var output = ToBaseX(number); 
  return output.PadLeft(width, _digits[0]); 
} 

使用此方法 basex.ToBaseX(1002, 10) 返回

aaaaaaabmo 

and basex.FromBaseX("aaaaaaabmo") returns

1002 

In the comments you indicate that the resulting string aaaaaaabmo does not seem very random. But then you can use the approach that Eric Lippert describes in the article A practical use of multiplicative inverses that you are referring to.

First you need to pick an upper limit to the numbers you want to obfuscate (and this number should fit into a 32 bit integer). Eric Lippert uses 1000000000 (1 billion). You then need to pick a number less than the limit that is coprime with the limit (e.g. they do not share any prime factors). Eric Lippert chooses 387420489 (and explains that any number that ends in 9 will be coprime with a number that is a power of 10). You then need to calculate the modular multiplicative inverse of this number, e.g. a number inverse-x that satisfies the following condition:

387420489 * inverse-x = 1 (mod 1000000000) 

您可以使用扩展欧几里德算法进行此计算,例如使用 an online calculator .模乘逆为 513180409。

要混淆您的数字,您可以使用此代码(为避免溢出,使用 64 位整数执行计算很重要):

var value = 1002; 
var m = 1000000000L; 
var x = 387420489L; 
var inverseX = 513180409L; 
var encoded = value*x%m; 
var decoded = encoded*inverseX%m;   

对于此特定计算,编码 为 195329978。

如果您想使用小写字母来表示混淆后的数字,您可以使用 BaseX 类将数字转换为 26 进制数。您可以计算出表示以下任意数字所需的最大字母数10 亿:

Math.Log(1000000000)/Math.Log(26) = 6.36054383137796 

这意味着您需要不超过 7 个字母来表示您的号码。

我使用一些您可以轻松自定义的常量将所有这些组合成两个简单的方法:

static class Obfuscator { 
 
  const Int64 modulo = 1000000000L; 
 
  const Int64 coprime = 280619659L; 
 
  const Int64 inverseCoprime = 687208739L; 
 
  const String digits = "abcdefghijklmnopqrstuvwxyz"; 
 
  const Int32 maxDigits = 7; // Math.Log(modulo)/Math.Log(digits.Length) rounded up. 
 
  public static String Obfuscate(Int32 originalValue) { 
    if (originalValue >= modulo || originalValue < 0) 
      throw new ArgumentOutOfRangeException(); 
    var value = (Int32) (originalValue*coprime%modulo); 
    var buffer = new Char[maxDigits]; 
    var i = maxDigits; 
    do { 
      buffer[--i] = digits[value%digits.Length]; 
      value /= digits.Length; 
    } while (value > 0); 
    while (i > 0) 
      buffer[--i] = digits[0]; 
    return new String(buffer); 
  } 
 
  public static Int32 Deobfuscate(String obfuscatedValue) { 
    if (String.IsNullOrEmpty(obfuscatedValue)) 
      throw new ArgumentException(); 
    var value = obfuscatedValue 
      .Aggregate(0, (a, c) => a*digits.Length + digits.IndexOf(c)); 
    return (Int32) (value*inverseCoprime%modulo); 
  } 
 
} 

唯一需要注意的细节是 0 被混淆为 aaaaaaa。对于 1 到 999999999(含)之间的任何数字,您会得到看起来像是 7 个字符的随机字符串。