繁体   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