【问题标题】:Simple String Encryption without Dependencies没有依赖关系的简单字符串加密
【发布时间】:2016-12-13 10:47:08
【问题描述】:

我需要一个简单的算法来加密/解密一个字符串。类似于 Base64 的东西,但更安全一些。它不是关键任务。 我需要的只是一些字符串操作。不像复制字符串并使用简单的 base 64 解码器使其易于阅读。

为什么不使用 AES?

由于我的应用是使用 .NET Core 创建的,因此它可以在 windows 和 mac 上运行。 我面临的问题是,为了在 mac 上使用System.Security,我需要安装 openssl。 由于我没有 sudo 访问权限,我无法安装它。

所以这里是要求:

  • 简单的字符串加密
  • 不依赖于System.Security.*

我读过Simple insecure two-way "obfuscation" for C#,但是没有依赖就没有解决方案。

【问题讨论】:

  • 为什么不实现一些简单的位移并将结果包装在 Base64 中?
  • 如果您有这样的解决方案,请随时将其发布为答案。谢谢!
  • 您是否测试过加密实现的托管版本?比如msdn.microsoft.com/en-us/library/…。我会假设他们不依赖外部依赖,但我不确定。
  • 在 Mac 上,您不需要将 openssl 安装到已安装的忽略器即可使用 AES,通过包含 Security.framework 来使用 Common Crypto。 Common Crypto 只是“C”代码,使用任何可用的硬件加密。

标签: c#


【解决方案1】:

如果您正在寻找混淆而不是安全性,您可以将字符串与常量或使用常量种子初始化的 PRNG 的输出进行异或。

常量示例:

byte xorConstant = 0x53;

string input = "foo";
byte[] data = Encoding.UTF8.GetBytes(input);
for (int i = 0; i < data.Length; i++)
{
    data[i] = (byte)(data[i] ^ xorConstant)
}
string output = Convert.ToBase64String(data);

解码:

byte xorConstant = 0x53;
byte[] data = Convert.FromBase64String(input);
for (int i = 0; i < data.Length; i++)
{
    data[i] = (byte)(data[i] ^ xorConstant)
}
string plainText = Encoding.UTF8.GetString(data);

【讨论】:

  • 正是我想要发布的内容。拦你。 +1。
  • 问题来了,如果有一串 0(甚至一个),字符串常量会立即被知道。
  • @zaph,对于任何已知的明文,该常量都是已知的。这就是为什么它毫无疑问是不安全的。此外,只有 256 个可能的键,因此您可以轻松地暴力破解它。它并不意味着安全。
  • 也许我对此更加关注,因为一位同事“加密”了一些前导零的数据,当我看到输出时,我什至不尝试就立即知道了密钥。
  • 不错的答案,但代码有simem问题,解决方案是for (int i = 0; i &lt; data.Length; i++)data[i] = (byte)(data[i] ^ xorConstant);
【解决方案2】:

所有非对称和对称加密方法都位于System.Security 命名空间中。来自this SO answer

.NET Core 中可用的对称加密选项有:

  • AES (System.Security.Cryptography.Aes.Create())
  • 3DES (System.Security.Cryptography.TripleDES.Create())

对于非对称加密

  • RSA (System.Security.Cryptography.RSA.Create())

看来你至少需要System.Security

编辑Here's 一个很好的 SO 问题,其中包含大量与加密相关的功能。注意System.Security 命名空间类和方法的广泛使用。

【讨论】:

  • 我同意这个答案。事实上,如果您不想包含System.Security,唯一的其他选择是自己实现它或从所述命名空间复制粘贴您想要的加密方法。
  • @AngelosChalaris 完全正确-实际上,OP,您为什么不能从安全库中复制和粘贴代码?这是来自 SO 答案的一堆函数:stackoverflow.com/questions/165808/…
  • 问题是这些方法在没有安装openssl的mac上是行不通的。
  • @dknaack 你为什么要开始用 C# 为 Mac 编写一个应用程序?为什么不以 Java 为例,它得到更好的支持并且与 C# 非常相似? 不过,您始终可以从所述命名空间中复制和更改库,以便在 Mac 上工作,这应该不会太麻烦。跨度>
【解决方案3】:

使用XTEA,它实际上是相当安全的。

以下是维基百科的完整源代码:

#include <stdint.h>

/* take 64 bits of data in v[0] and v[1] and 128 bits of key[0] - key[3] */

void encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
    for (i=0; i < num_rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
    }
    v[0]=v0; v[1]=v1;
}

void decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*num_rounds;
    for (i=0; i < num_rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0]=v0; v[1]=v1;
}

【讨论】:

    【解决方案4】:

    BitShifting:

     var topSecret = "This is%&/(/ TopSecret 111!!";
     int shft = 5;
     string encrypted = topSecret.Select(ch => ((int) ch) << shft).Aggregate("", (current, val) => current + (char) (val*2));
     encrypted = Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypted));
     string decrypted = Encoding.UTF8.GetString(Convert.FromBase64String(encrypted)).Select(ch => ((int) ch) >> shft).Aggregate("", (current, val) => current + (char) (val/2));
    

    加密:

    4ZSA4aiA4amA4bOA4KCA4amA4bOA4KWA4KaA4K+A4KiA4K+A4KCA4ZSA4a+A4bCA4ZOA4aWA4aOA4bKA4aWA4bSA4KCA4LGA4LGA4LGA4KGA4KGA

    再次解密:

    这是%&/(/TopSecret 111!!

    注意

    不是很漂亮,也不是很简单。您可以调整盐和编码以满足您的需求。

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 2014-03-10
      • 2013-05-03
      • 1970-01-01
      相关资源
      最近更新 更多