2012-11-08 101 views
7

我需要加密一个字符串,然后能够再次解密。简单的字符串加密/解密与一个小的结果字符串

我实施了解决方案here,它运行良好,但结果字符串并不合适,因为它需要简单和足够短以供用户使用。

我正在加密增量数据库的ID(从1),不会超过500.理想情况下,我想要加密的字符串长度不超过6个字符。

任何想法表示赞赏..

编辑:这是一个漫长的形式,用户可以在日后与此生成的字符串

+5

您确定要加密和不压缩?你描述的实际上听起来像哈希。你能解释更多的用例吗? – Oded

+0

那么你的问题又是什么?你为什么试图限制加密的字符串为6个字符?你甚至不需要对字符串进行加密,你可以实际上对它进行散列,并保持存储在内存中的1-1关系。 –

+0

你为什么要加密增量数据库ID?你可以把它们打散吗? –

回答

1

,如果你希望它是简单的,也许用一个主键恢复的6信 其添加到6个字母输入做基于所述容许输入模字符

其状如= 1

b = 2

C = 3

,然后简单地增加一个号码+ 13模24> ...

不会说,它的安全,但它的简单,因为你的要求 你也可以做一些类似的组合为下一个字符被解码prev char as + xx

2
//encryption 
string output=""; 
char[] readChar = yourInput.ToCharArray(); 
for (int i = 0; i < readChar.Length; i++) 
{ 
    int no = Convert.ToInt32(readChar[i]) + 10; 
    string r = Convert.ToChar(no).ToString(); 
    output+=r; 
} 
//decryption 
string output=""; 
char[] readChar = yourInput.ToCharArray(); 
for (int i = 0; i < readChar.Length; i++) 
{ 
    int no = Convert.ToInt32(readChar[i]) - 10; 
    string r = Convert.ToChar(no).ToString(); 
    output+=r; 
} 
0

既然你说这些数据库ID是递增的,我假设我们正在谈论正整数。限制加密确实不是一个好主意,因为加密越有限就越容易中断。我不确定你的目标,但听起来你可能只想通过“加密”来隐藏最终用户的ID。所以,如果我假设这些是正整数在1到999之间,那么我的建议如下。一,简单的字符替换。这将是很容易弄清楚,因为你将被限制为10个字符。二,将它们转换为十六进制。不会欺骗很多人,但平均最终用户可能无法识别它。第三,将数字转换为Base64。这不会像十六进制那样容易被识别。最后,提出一些可以应用于总是会产生独特结果的数字的公式。不过我真的不建议,因为这很困难。

4

您可以在没有任何填充的情况下使用CTR mode中的AES。在这种模式下,有一个加密的计数器,然后结果与您的明文即数字进行异或。结果应该很小,并且您将得到AES的加密,这比您使用的任何替换密码(您可能会手动破解)要好。您将必须获得BouncyCastle crypto library,但是由于Rijndael的Microsoft实施不具有CTR作为available mode。以下是您需要实施的AES类的示例。以及加密和解密的例子。

using System; 
using System.Text; 
using Org.BouncyCastle.Crypto; 
using Org.BouncyCastle.Crypto.Engines; 
using Org.BouncyCastle.Crypto.Modes; 
using Org.BouncyCastle.Crypto.Parameters; 

public class AES 
{ 
    private readonly Encoding encoding; 

    private SicBlockCipher mode; 


    public AES(Encoding encoding) 
    { 
     this.encoding = encoding; 
     this.mode = new SicBlockCipher(new AesFastEngine()); 
    } 

    public static string ByteArrayToString(byte[] bytes) 
    { 
     return BitConverter.ToString(bytes).Replace("-", string.Empty); 
    } 

    public static byte[] StringToByteArray(string hex) 
    { 
     int numberChars = hex.Length; 
     byte[] bytes = new byte[numberChars/2]; 

     for (int i = 0; i < numberChars; i += 2) 
     { 
      bytes[i/2] = Convert.ToByte(hex.Substring(i, 2), 16); 
     } 

     return bytes; 
    } 


    public string Encrypt(string plain, byte[] key, byte[] iv) 
    { 
     byte[] input = this.encoding.GetBytes(plain); 

     byte[] bytes = this.BouncyCastleCrypto(true, input, key, iv); 

     string result = ByteArrayToString(bytes); 

     return result; 
    } 


    public string Decrypt(string cipher, byte[] key, byte[] iv) 
    { 
     byte[] bytes = this.BouncyCastleCrypto(false, StringToByteArray(cipher), key, iv); 

     string result = this.encoding.GetString(bytes); 

     return result; 
    } 


    private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, byte[] key, byte[] iv) 
    { 
     try 
     { 
      this.mode.Init(forEncrypt, new ParametersWithIV(new KeyParameter(key), iv)); 

      BufferedBlockCipher cipher = new BufferedBlockCipher(this.mode); 

      return cipher.DoFinal(input); 
     } 
     catch (CryptoException) 
     { 
      throw; 
     } 
    } 
} 

使用示例

string test = "1"; 

AES aes = new AES(System.Text.Encoding.UTF8); 

RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider(); 
byte[] key = new byte[32]; 
byte[] iv = new byte[32]; 

// Generate random key and IV 
rngCsp.GetBytes(key); 
rngCsp.GetBytes(iv); 

string cipher = aes.Encrypt(test, key, iv); 

string plaintext = aes.Decrypt(cipher, key, iv); 

Response.Write(cipher + "<BR/>"); 

Response.Write(plaintext); 

输出例

CB 
1 
+0

哇。我把这个代码扔在一起。它的工作令人难以置信。谢谢。 –