.NET 中字符串的简单混淆?

问题描述 投票:0回答:4

我需要通过互联网发送一串大约 30 个字符的字符串,该字符串最终可能会作为另一家公司数据库中的 ID。

虽然字符串本身无法识别,但我仍然希望它不会以任何方式被识别。

在 .NET 中混淆此类字符串以便在必要时可以轻松反转的最简单方法是什么?

c# .net string obfuscation
4个回答
30
投票

这不是密码学

请勿将此答案用于任何必须保密的信息。

它会让人类难以阅读字符串。

它会往返,但如果您的字符串不是“普通”并且您使用较大的移位值,则可能不会。

此代码不会保护数据免遭协同“破解”的攻击。聪明而熟练的人可能会用笔和纸破解这个问题,但这需要一些努力。


来点经典的(带有现代风格的)怎么样?

public static string Caesar(this string source, Int16 shift)
{
    var maxChar = Convert.ToInt32(char.MaxValue);
    var minChar = Convert.ToInt32(char.MinValue);

    var buffer = source.ToCharArray();

    for (var i = 0; i < buffer.Length; i++)
    {
        var shifted = Convert.ToInt32(buffer[i]) + shift;

        if (shifted > maxChar)
        {
            shifted -= maxChar;
        }
        else if (shifted < minChar)
        {
            shifted += maxChar;
        }

        buffer[i] = Convert.ToChar(shifted);
    }

    return new string(buffer);
}

显然你会这样使用

var plain = "Wibble";
var caesered = plain.Caesar(42);
var newPlain = caesered.Caesar(-42);

速度很快,您的密钥只是一个

Int16
,它可以防止随意的观察者复制粘贴该值,但是,它不安全。


11
投票

怎么样:

    Convert.ToBase64String(Encoding.UTF8.GetBytes(myString));

及其相反:

    Encoding.UTF8.GetString(Convert.FromBase64String(myObfuscatedString));

只要你不介意增加弦的长度


6
投票

尝试使用例如 AES 对其进行加密,如果您知道另一台计算机上的加密密钥,则可以轻松地在那里解密

http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes(v=vs.100).aspx

周围有很多代码示例。例如,我通过快速搜索找到了这篇文章,尽管它只有 128 位,但我认为它应该可以解决问题

在C#中使用AES加密


3
投票

我受到@Jodrell 的回答的启发,这是我的替代版本。唯一真正的区别是我使用模运算符而不是 if-then-else 结构。

如果你和我一样,以前从未听说过凯撒密码,这里有一个链接:

https://en.wikipedia.org/wiki/Caesar_cipher

   public static partial class MString
   {
      ...

      /// <summary>
      /// Method to perform a very simple (and classical) encryption for a string. This is NOT at 
      /// all secure, it is only intended to make the string value non-obvious at a first glance.
      ///
      /// The shiftOrUnshift argument is an arbitrary "key value", and must be a non-zero integer 
      /// between -65535 and 65535 (inclusive). To decrypt the encrypted string you use the negative 
      /// value. For example, if you encrypt with -42, then you decrypt with +42, or vice-versa.
      ///
      /// This is inspired by, and largely based on, this:
      /// https://stackoverflow.com/a/13026595/253938
      /// </summary>
      /// <param name="inputString">string to be encrypted or decrypted, must not be null</param>
      /// <param name="shiftOrUnshift">see above</param>
      /// <returns>encrypted or decrypted string</returns>
      public static string CaesarCipher(string inputString, int shiftOrUnshift)
      {
         // Check C# is still C#
         Debug.Assert(char.MinValue == 0 && char.MaxValue == UInt16.MaxValue);

         const int C64K = UInt16.MaxValue + 1;

         // Check the arguments
         if (inputString == null)
            throw new ArgumentException("Must not be null.", "inputString");
         if (shiftOrUnshift == 0)
            throw new ArgumentException("Must not be zero.", "shiftOrUnshift");
         if (shiftOrUnshift <= -C64K || shiftOrUnshift >= C64K)
            throw new ArgumentException("Out of range.", "shiftOrUnshift");

         // Perform the Caesar cipher shifting, using modulo operator to provide wrap-around
         char[] charArray = new char[inputString.Length];
         for (int i = 0; i < inputString.Length; i++)
         {
            charArray[i] = 
                  Convert.ToChar((Convert.ToInt32(inputString[i]) + shiftOrUnshift + C64K) % C64K);
         }

         // Return the result as a new string
         return new string(charArray);
      }

      ...
   }

还有一些测试代码:

     // Test CaesarCipher() method
     const string CHelloWorld = "Hello world!";
     const int CCaesarCipherKey = 42;
     string caesarCiphered = MString.CaesarCipher(CHelloWorld, CCaesarCipherKey);
     if (MString.CaesarCipher(caesarCiphered, -CCaesarCipherKey) != CHelloWorld)
        throw new Exception("Oh no!");
© www.soinside.com 2019 - 2024. All rights reserved.