【问题标题】:Encrypt a number to another number of the same length将一个数字加密为另一个相同长度的数字
【发布时间】:2011-06-16 02:10:57
【问题描述】:

我需要一种方法来获取一个 12 位数字并将其加密为不同的 12 位数字(除 0123456789 之外没有其他字符)。然后稍后我需要能够将加密号码解密回原始号码。

重要的是,2 个加密数字是否按顺序排列并不明显。因此,例如,如果我加密 0000000000001,它在加密时看起来应该与 000000000002 完全不同。它不一定是世界上最安全的东西,但越安全越好。

我一直在四处寻找,但没有找到任何似乎完美契合的东西。从我所见,某种类型的 XOR 可能是最简单的方法,但我不知道该怎么做。

谢谢, 吉姆

【问题讨论】:

  • 我想随机生成但静态的 1 万亿项字典是不可能的?
  • 您的结果必须是数值吗?另外,您的安全要求是什么?加密规则 #1 是“不要依赖自己的加密”
  • 理想的结果是数值,但不是绝对要求。结果将打印在包装上并输入网站。如果字母数字是唯一的选项,那没关系,但只能包括 a-z 和 0-9 没有特殊字符。至于安全要求,我认为我们不会有黑客团队试图破解这个程序。我们并没有真正保护任何关键的东西,所以我想说的是,数字有序(如上所述)不容易区分比完全安全更重要。

标签: c# .net encryption


【解决方案1】:

感谢你们使用维基百科页面 http://en.wikipedia.org/wiki/Format-preserving_encryption 中的“来自前缀密码的 FPE”,我最终解决了这个问题。我将在下面给出基本步骤,希望对将来的人有所帮助。

注意 - 我相信任何专家都会告诉你这是一个 hack。这些数字似乎是随机的,并且对于我需要的东西来说足够安全,但是如果安全性是一个大问题,请使用其他东西。我相信专家可以指出我所做的事情中的漏洞。我发布此内容的唯一目标是因为在搜索问题的答案时我会发现它很有用。也只能在无法反编译的情况下使用。

我打算发布步骤,但解释太多了。我只会发布我的代码。这是我仍然需要清理的概念验证代码,但你会明白的。请注意,我的代码特定于 12 位数字,但针对其他数字进行调整应该很容易。按照我的做法,Max 大概 16 岁。

public static string DoEncrypt(string unencryptedString)
{
    string encryptedString = "";
    unencryptedString = new string(unencryptedString.ToCharArray().Reverse().ToArray());
    foreach (char character in unencryptedString.ToCharArray())
    {
        string randomizationSeed = (encryptedString.Length > 0) ? unencryptedString.Substring(0, encryptedString.Length) : "";
        encryptedString += GetRandomSubstitutionArray(randomizationSeed)[int.Parse(character.ToString())];
    }

    return Shuffle(encryptedString);
}

public static string DoDecrypt(string encryptedString)
{
    // Unshuffle the string first to make processing easier.
    encryptedString = Unshuffle(encryptedString);

    string unencryptedString = "";
    foreach (char character in encryptedString.ToCharArray().ToArray())
        unencryptedString += GetRandomSubstitutionArray(unencryptedString).IndexOf(int.Parse(character.ToString()));

    // Reverse string since encrypted string was reversed while processing.
    return new string(unencryptedString.ToCharArray().Reverse().ToArray());
}

private static string Shuffle(string unshuffled)
{
    char[] unshuffledCharacters = unshuffled.ToCharArray();
    char[] shuffledCharacters = new char[12];
    shuffledCharacters[0] = unshuffledCharacters[2];
    shuffledCharacters[1] = unshuffledCharacters[7];
    shuffledCharacters[2] = unshuffledCharacters[10];
    shuffledCharacters[3] = unshuffledCharacters[5];
    shuffledCharacters[4] = unshuffledCharacters[3];
    shuffledCharacters[5] = unshuffledCharacters[1];
    shuffledCharacters[6] = unshuffledCharacters[0];
    shuffledCharacters[7] = unshuffledCharacters[4];
    shuffledCharacters[8] = unshuffledCharacters[8];
    shuffledCharacters[9] = unshuffledCharacters[11];
    shuffledCharacters[10] = unshuffledCharacters[6];
    shuffledCharacters[11] = unshuffledCharacters[9];
    return new string(shuffledCharacters);
}

private static string Unshuffle(string shuffled)
{
    char[] shuffledCharacters = shuffled.ToCharArray();
    char[] unshuffledCharacters = new char[12];
    unshuffledCharacters[0] = shuffledCharacters[6];
    unshuffledCharacters[1] = shuffledCharacters[5];
    unshuffledCharacters[2] = shuffledCharacters[0];
    unshuffledCharacters[3] = shuffledCharacters[4];
    unshuffledCharacters[4] = shuffledCharacters[7];
    unshuffledCharacters[5] = shuffledCharacters[3];
    unshuffledCharacters[6] = shuffledCharacters[10];
    unshuffledCharacters[7] = shuffledCharacters[1];
    unshuffledCharacters[8] = shuffledCharacters[8];
    unshuffledCharacters[9] = shuffledCharacters[11];
    unshuffledCharacters[10] = shuffledCharacters[2];
    unshuffledCharacters[11] = shuffledCharacters[9];
    return new string(unshuffledCharacters);
}

public static string DoPrefixCipherEncrypt(string strIn, byte[] btKey)
{
    if (strIn.Length < 1)
        return strIn;

    // Convert the input string to a byte array 
    byte[] btToEncrypt = System.Text.Encoding.Unicode.GetBytes(strIn);
    RijndaelManaged cryptoRijndael = new RijndaelManaged();
    cryptoRijndael.Mode =
    CipherMode.ECB;//Doesn't require Initialization Vector 
    cryptoRijndael.Padding =
    PaddingMode.PKCS7;


    // Create a key (No IV needed because we are using ECB mode) 
    ASCIIEncoding textConverter = new ASCIIEncoding();

    // Get an encryptor 
    ICryptoTransform ictEncryptor = cryptoRijndael.CreateEncryptor(btKey, null);


    // Encrypt the data... 
    MemoryStream msEncrypt = new MemoryStream();
    CryptoStream csEncrypt = new CryptoStream(msEncrypt, ictEncryptor, CryptoStreamMode.Write);


    // Write all data to the crypto stream to encrypt it 
    csEncrypt.Write(btToEncrypt, 0, btToEncrypt.Length);
    csEncrypt.Close();


    //flush, close, dispose 
    // Get the encrypted array of bytes 
    byte[] btEncrypted = msEncrypt.ToArray();


    // Convert the resulting encrypted byte array to string for return 
    return (Convert.ToBase64String(btEncrypted));
}

private static List<int> GetRandomSubstitutionArray(string number)
{
    // Pad number as needed to achieve longer key length and seed more randomly.
    // NOTE I didn't want to make the code here available and it would take too longer to clean, so I'll tell you what I did. I basically took every number seed that was passed in and prefixed it and  postfixed it with some values to make it 16 characters long and to get a more unique result. For example:
    // if (number.Length = 15)
    //    number = "Y" + number;
    // if (number.Length = 14)
    //    number = "7" + number + "z";
    // etc - hey I already said this is a hack ;)

    // We pass in the current number as the password to an AES encryption of each of the
    // digits 0 - 9. This returns us a set of values that we can then sort and get a 
    // random order for the digits based on the current state of the number.
    Dictionary<string, int> prefixCipherResults = new Dictionary<string, int>();
    for (int ndx = 0; ndx < 10; ndx++)
        prefixCipherResults.Add(DoPrefixCipherEncrypt(ndx.ToString(), Encoding.UTF8.GetBytes(number)), ndx);

    // Order the results and loop through to build your int array.
    List<int> group = new List<int>();
    foreach (string key in prefixCipherResults.Keys.OrderBy(k => k))
        group.Add(prefixCipherResults[key]);

    return group;
}

【讨论】:

  • 有什么方法可以为不同的用户添加一些salt 吗?像用户A salt:8875。用户B salt:5525。这样就可以正确加密和解​​密也安全。
【解决方案2】:

另一种简单加密的方法,您可以将每个数字从 10 中取出。

例如 初始数字:123456

10-1 = 9 10-2 = 8 10-3 = 7 等等

你会得到 987654

您可以将它与 XOR 结合使用以实现更安全的加密。

【讨论】:

  • 我认为它不适用于以 0 结尾的数字。例如 12340 变成 987610 所以我们在解密时会遇到问题。
【解决方案3】:

您所说的有点像一次性便笺簿。一个与明文长度相同的密钥,然后对每个单独的字符进行模数运算。

A xor B = C
C xor B = A

或者换句话说

A xor B xor B = A

只要您不在多个不同的输入上使用相同的密钥 B(例如 B 必须是唯一的,每次加密时),那么理论上您永远无法在不知道的情况下恢复原始 A B 是什么。如果您多次使用相同的B,那么所有的赌注都会被取消。

评论跟进:

您不应该在结束后得到比开始时更多的位。 xor 只是翻转位,它没有任何进位功能。以 6 位结尾很奇怪……至于代码:

$plaintext = array(digit1, digit2, digit3, digit4, digit5, digit6);
$key = array(key1, key2, key3, key4, key5, key6);
$ciphertext = array()

# encryption
foreach($plaintext as $idx => $char) {
   $ciphertext[$idx] = $char xor $key[$idx];
}

# decryption
foreach($ciphertext as $idx => $char) {
   $decrypted[$idx] = $char xor $key[$idx];
}

为简单起见,仅将其作为数组进行。对于实际数据,您将在每个字节或每个字的基础上工作,并且只需按顺序对每个块进行异或。您可以使用比输入更短的密钥字符串,但这可以更轻松地对密钥进行逆向工程。理论上,您可以使用单个字节进行异或运算,但是您基本上已经实现了 rot-13 的位级等效。

【讨论】:

  • 所以当我 Xor 2 5 个字符的数字(例如)时,有时我会得到一个 6 位数的结果。只是不应该这么艰难。我要么做错了 Xor,要么有时它们加起来大于 9。你有没有机会输入一个 5 行代码示例?
【解决方案4】:

例如,您可以将数字添加到数字 const (214354178963...whatever) 并应用“~”运算符(反转所有位),这并不安全,但请确保您始终可以解密您的数字。

【讨论】:

    【解决方案5】:

    任何拥有反射器或 ildasm 的人都可以破解这种加密算法。

    我不知道您的业务需求是什么,但您必须知道。

    【讨论】:

    • 这是一个很好的观点。在这种情况下,进行加密和解密的代码都将在网站上运行,因此不应访问。
    【解决方案6】:

    如果要求中有足够的回旋余地,您可以接受 16 位十六进制数字作为加密方,只需将 12 位十进制数字解释为 64 位明文并使用 64 位分组密码,如 Blowfish、Triple-DES 或 IDEA .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多