我需要加密一個字符串,然後能夠再次解密。簡單的字符串加密/解密與一個小的結果字符串
我實施瞭解決方案here,它運行良好,但結果字符串並不合適,因爲它需要簡單和足夠短以供用戶使用。
我正在加密增量數據庫的ID(從1),不會超過500.理想情況下,我想要加密的字符串長度不超過6個字符。
任何想法表示讚賞..
編輯:這是一個漫長的形式,用戶可以在日後與此生成的字符串
我需要加密一個字符串,然後能夠再次解密。簡單的字符串加密/解密與一個小的結果字符串
我實施瞭解決方案here,它運行良好,但結果字符串並不合適,因爲它需要簡單和足夠短以供用戶使用。
我正在加密增量數據庫的ID(從1),不會超過500.理想情況下,我想要加密的字符串長度不超過6個字符。
任何想法表示讚賞..
編輯:這是一個漫長的形式,用戶可以在日後與此生成的字符串
,如果你希望它是簡單的,也許用一個主鍵恢復的6信 其添加到6個字母輸入做基於所述容許輸入模字符
其狀如= 1
b = 2
C = 3
,然後簡單地增加一個號碼+ 13模24> ...
不會說,它的安全,但它的簡單,因爲你的要求 你也可以做一些類似的組合爲下一個字符被解碼prev char as + xx
//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;
}
既然你說這些數據庫ID是遞增的,我假設我們正在談論正整數。限制加密確實不是一個好主意,因爲加密越有限就越容易中斷。我不確定你的目標,但聽起來你可能只想通過「加密」來隱藏最終用戶的ID。所以,如果我假設這些是正整數在1到999之間,那麼我的建議如下。一,簡單的字符替換。這將是很容易弄清楚,因爲你將被限制爲10個字符。二,將它們轉換爲十六進制。不會欺騙很多人,但平均最終用戶可能無法識別它。第三,將數字轉換爲Base64。這不會像十六進制那樣容易被識別。最後,提出一些可以應用於總是會產生獨特結果的數字的公式。不過我真的不建議,因爲這很困難。
您可以在沒有任何填充的情況下使用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
哇。我把這個代碼扔在一起。它的工作令人難以置信。謝謝。 –
您確定要加密和不壓縮?你描述的實際上聽起來像哈希。你能解釋更多的用例嗎? – Oded
那麼你的問題又是什麼?你爲什麼試圖限制加密的字符串爲6個字符?你甚至不需要對字符串進行加密,你可以實際上對它進行散列,並保持存儲在內存中的1-1關係。 –
你爲什麼要加密增量數據庫ID?你可以把它們打散嗎? –