C# 将一个数字加密为另一个长度相同的数字
我需要一种方法来获取一个12位数字,并将其加密为另一个12位数字(除0123456789以外没有其他字符)。之后,我需要能够将加密的数字解密回原始数字 重要的是,如果2个加密的数字顺序正确,则不明显。因此,例如,如果我加密0000000000001,它在加密时应该与000000000002完全不同。它不一定是世界上最安全的东西,但越安全越好 我一直在四处寻找,但没有发现任何似乎是完美的适合。据我所见,某种类型的XOR可能是最简单的方法,但我不确定如何做到这一点 谢谢,C# 将一个数字加密为另一个长度相同的数字,c#,.net,encryption,C#,.net,Encryption,我需要一种方法来获取一个12位数字,并将其加密为另一个12位数字(除0123456789以外没有其他字符)。之后,我需要能够将加密的数字解密回原始数字 重要的是,如果2个加密的数字顺序正确,则不明显。因此,例如,如果我加密0000000000001,它在加密时应该与000000000002完全不同。它不一定是世界上最安全的东西,但越安全越好 我一直在四处寻找,但没有发现任何似乎是完美的适合。据我所见,某种类型的XOR可能是最简单的方法,但我不确定如何做到这一点 谢谢, 吉姆你说的有点像一次性便笺
吉姆你说的有点像一次性便笺簿。一个与明文长度相同的键,然后对每个字符进行模运算
A xor B = C
C xor B = A
或者换句话说
A xor B xor B = A
只要不在多个不同的输入上使用相同的密钥B
(例如,每次加密时,B必须是唯一的),那么理论上,如果不知道B
是什么,就永远无法恢复原始A
。如果您多次使用相同的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的位级。例如,你可以用一些常量(214354178963…什么)来添加数字,然后应用“~”操作符(反转所有位)。这并不安全,但可以确保你可以一直解密数字 简单加密的另一种方法是,只需从10中对每个数字进行子结构 比如说 起始号码:123456 10-1=9 10-2 = 8 10-3 = 7 等等 你会得到 987654
您可以将其与XOR结合使用以实现更安全的加密。任何使用reflector或ildasm的人都可以破解这种加密算法
我不知道您的业务要求是什么,但您必须知道。如果要求中有足够的回旋余地,您可以接受16位十六进制数字作为加密端,只需将12位十进制数字解释为64位明文,并使用64位分组密码,如Blowfish,三重DES或IDEA。多亏了你们使用维基百科页面上的“前缀密码的FPE”,我最终解决了这个问题。我将给出下面的基本步骤,希望对将来的人有所帮助 注意-我相信任何专家都会告诉你这是一个黑客。这些数字看起来是随机的,对于我所需要的东西来说已经足够安全了,但是如果安全性是一个大问题,那就用别的东西吧。我相信专家们可以指出我所做的工作中的漏洞。我发布这篇文章的唯一目的是,当我搜索问题的答案时,我会发现它很有用。也只能在无法反编译的情况下使用 我本打算发布步骤,但解释起来太多了。我会发布我的代码。这是我的概念验证代码,我仍然需要清理,但你会明白的。注意:我的代码特定于12位数字,但为其他数字进行调整应该很容易。马克斯大概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;
}
公共静态字符串DoEncrypt(字符串未加密字符串)
{
字符串encryptedString=“”;
unencryptedString=新字符串(unencryptedString.tocharray().Reverse().ToArray());
foreach(未加密字符串.ToCharArray()中的字符)
{
字符串随机化种子=(encryptedString.Length>0)?未加密的string.Substring(0,encryptedString.Length):“”;
encryptedString+=GetRandomSubstitutionArray(randomizationSeed)[int.Parse(character.ToString())];
}
返回洗牌(encryptedString);
}
公共静态字符串DoDecrypt(字符串加密字符串)
{
//首先取消填充字符串以使处理更容易。
encryptedString=取消缓冲(encryptedString);
字符串未加密字符串=”;
foreach(encryptedString.ToCharray().ToArray()中的字符)
unencryptedString+=GetRandomSubstitutionArray(unencryptedString).IndexOf(int.Parse(character.ToString());
//反转字符串,因为在处理时已反转加密字符串。
返回新字符串(未加密字符串.tocharray().Reverse().ToArray());
}
私有静态字符串混洗(字符串未混洗)
{
char[]unshuffledCharacters=unshuffled.tocharray();
char[]shuffledCharacters=新字符[12];
shuffledCharacters[0]=非shuffledCharacters[2];
shuffledCharacters[1]=非shuffledCharacters[7];
shuffledCharacters[2]=非shuffledCharacters[10];
shuffledCharacters[3]=非shuffledCharacters[5];
shuffledCharacters[4]=非shuffledCharacters[3];
shuffledCharacters[5]=非shuffledCharacters[1];
shuffledCharacters[6]=非shuffledCharacters[0];
shuffledCharacters[7]=非shuffledCharacters[4];
shuffledCharacters[8]=非shuffledCharacters[8];
shuffledCharacters[9]=非shuffledCharacters[11];
shuffledCharacters[10]=非shuffledCharacters[6];
shuffledCharacters[11]=非shuffledCharacters[9];
返回新字符串(洗牌字符);
}
私有静态字符串取消缓冲(字符串已洗牌)
{
char[]shuffledCharacters=shuffled.tocharray();
char[]unsuffledCharacters=新字符[12];
unshuffledCharacters[0]=shuffledCharacters[6];
unshuffledCharacters[1]=shuffledCharacters[5];
unshuffledCharacters[2]=shuffledCharacters[0];
unshuffledCharacters[3]=shuffledcharactere