将数字加密到另一个相同长度的数字

Jim*_*Jim 5 .net c# encryption

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

重要的是,如果2个加密数字是有序的,则不明显.因此,例如,如果我加密0000000000001,加密时它应该看起来完全不同于000000000002.它不一定是世界上最安全的东西,但越安全越好.

我一直在四处寻找,但没有找到任何看起来非常合适的东西.从我所看到的某些类型的XOR可能是最简单的方法,但我不知道如何做到这一点.

谢谢,吉姆

Jim*_*Jim 5

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

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

我打算发布步骤,但解释得太多了.我只是发布我的代码.这是我仍然需要清理的概念代码证明,但你会明白这一点.请注意,我的代码特定于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;
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*c B 2

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

A xor B = C
C xor B = A
Run Code Online (Sandbox Code Playgroud)

或者换句话说

A xor B xor B = A
Run Code Online (Sandbox Code Playgroud)

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

评论跟进:

之后你不应该得到比开始时更多的位。异或只是翻转位,它没有任何进位功能。以 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];
}
Run Code Online (Sandbox Code Playgroud)

为了简单起见,只是将其作为数组进行。对于实际数据,您可以在每个字节或每个字的基础上进行工作,然后按顺序对每个块进行异或。您可以使用比输入短的密钥字符串,但这使得对密钥进行逆向工程变得更容易。理论上,您可以使用单个字节进行异或运算,但您基本上就达到了 rot-13 的位级等效效果。