2013-02-27 21 views
5

電子メールアドレスや数値などの文字列値を暗号化および復号化する必要がありますが、暗号化された文字列に '/'私はURLでそれを使用していて、セパレータには '/'を使用して値を取得しています。暗号化/復号化方法に暗号化された文字列に '/'が含まれていない

あなただけのURLを渡すためにそれをやっている場合は
string passPhrase = "[email protected]";  // can be any string 
    string saltValue = "[email protected]";  // can be any string 
    string hashAlgorithm = "SHA1";    // can be "MD5" 
    int passwordIterations = 2;     // can be any number 
    string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes 
    int keySize = 256;    // can be 192 or 128 

    public string Encrypt(string plainText) 
    {    
     byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); 
     byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);   
     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); 
     PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase,saltValueBytes,hashAlgorithm,passwordIterations); 
     byte[] keyBytes = password.GetBytes(keySize/8); 
     RijndaelManaged symmetricKey = new RijndaelManaged(); 
     symmetricKey.Mode = CipherMode.CBC;    
     ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes,initVectorBytes); 
     MemoryStream memoryStream = new MemoryStream(); 
     CryptoStream cryptoStream = new CryptoStream(memoryStream,encryptor,CryptoStreamMode.Write);   
     cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);    
     cryptoStream.FlushFinalBlock(); 
     byte[] cipherTextBytes = memoryStream.ToArray(); 
     memoryStream.Close(); 
     cryptoStream.Close(); 
     string cipherText = Convert.ToBase64String(cipherTextBytes); 
     return cipherText; 
    } 
+1

「/」を他の文字に置き換えるのはどうでしょうか? –

+4

URLで使用している場合、 'UrlEncode'と' UrlDecode'は動作しませんか? – Corak

+0

このリンクを試す1. [Link1](http://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp)2. [Link2](http://stackoverflow.com/質問/ 202011 /暗号化 - 解読 - 文字列 - ネット)ありがとうAbiruban – Niventh

答えて

11

が、私はあなたが(関係なく、それは/かを持っている場合)、任意の暗号化された文字列を生成する提案、および:

私は現在、以下の方法を使用しています操作を行います。

var sanitized = HttpUtility.UrlEncode(encryptedString); 

あなたが見ることができるように、/%2fになります。次に、簡単に行うことができます:

var encryptedString = HttpUtility.UrlDecode(sanitized) 

あなたは同じ文字列を再度取得します。

編集:HttpUtilitySystem.Webアセンブリです。

エンコード:

// ... 
string cipherText = Convert.ToBase64String(cipherTextBytes); 
string ctWithoutSlashes = cipherText.Replace("/", "-"); 

デコード

2

Convert.ToBase64String

は、文字、数字、 +/ので、あなたは、単にそれは、文字、数字または +ではありません何か他のもののためにアウト /を切り替えることができますを使用しています
string cipherText = ctWithoutSlashes.Replace("-", "/"); 
// ... 
4

暗号化自体は単にバイトを出力しますが、文字ではありません。したがって、この質問は暗号化/復号化とはまったく関係ありません。実際の問題は、任意のバイトをURLで使用できる文字列に変換することです。これには、通常のBase64よりもURLセーフBase64を使用することをおすすめします。

これらの/文字は、暗号文に適用するBase64エンコーディングによって生成されます。 Base64はASCII文字と数字(合計62)を使用し、さらに/+、最後に=をパディングとして使用します。

パディングはかなり役に立たないので、私はそれをストリップします。

/_と置き換えます。+-に置き換えてください。これは、URLセーフBase64またはbase64urlと呼ばれています。それはRFC4648に記載されています。

public static string Base64UrlEncode(byte[] bytes) 
{ 
    return Convert.ToBase64String(bytes).Replace("=", "").Replace('+', '-').Replace('/', '_'); 
} 

public static byte[] Base64UrlDecode(string s) 
{ 
    s = s.Replace('-', '+').Replace('_', '/'); 
    string padding = new String('=', 3 - (s.Length + 3) % 4); 
    s += padding; 
    return Convert.FromBase64String(s); 
} 
関連する問題