簡體   English   中英

是否可以使用Base64在加密字符串中獲取冒號

[英]Is it possible to get colon in encrypted string using Base64

我正在使用下面的方法來加密密碼,我稍后將其保存在一個字符串中,我用這樣的冒號分隔:

用戶名:MyEncryptedString

我的問題是,我的方法可能會返回一個包含冒號的字符串嗎?

public static string EncryptString(string password, string sharedSecret) {
    if (string.IsNullOrEmpty(password))
        throw new ArgumentNullException("password");
    if (string.IsNullOrEmpty(sharedSecret))
        throw new ArgumentNullException("sharedSecret");

    string outStr = null;                       // Encrypted string to return
    RijndaelManaged aesAlg = null;              // RijndaelManaged object used to encrypt the data.

    try {
        // generate the key from the shared secret and the salt
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, Salt);

        // Create a RijndaelManaged object
        aesAlg = new RijndaelManaged();
        aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

        // Create a decryptor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream()) {
            // prepend the IV
            msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
            msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
                    //Write all data to the stream.
                    swEncrypt.Write(password);
                }
            }
            outStr = Convert.ToBase64String(msEncrypt.ToArray());
        }
    } finally {
        // Clear the RijndaelManaged object.
        if (aesAlg != null)
            aesAlg.Clear();
    }

    // Return the encrypted bytes from the memory stream.
    return outStr;
}

現代密碼產生的二進制輸出應該與隨機噪聲無法區分。 如果您將此二進制輸出解釋為文本(ASCII,UTF-8等),您可能會看到:如果密文足夠長則在那里。 但是你也看到它用於更短的密文,但不一定是每一個密文。 關鍵是,輸出是二進制而不是“字符串”。

可以對二進制輸出進行編碼以獲得字符串。 如果使用Base 64或Hex,則無法獲得: ,因為它不在其字母表中。 如果您決定使用Base 85編碼,那么您可能會得到: ,具體取決於特定的字母(例如Z85 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM