简体   繁体   English

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

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

I'm using below method to encrypt a password which I later on save in a string that I separate with a colon like this: 我正在使用下面的方法来加密密码,我稍后将其保存在一个字符串中,我用这样的冒号分隔:

username:MyEncryptedString 用户名:MyEncryptedString

My question is, is it possible that my method could return a string containing a colon? 我的问题是,我的方法可能会返回一个包含冒号的字符串吗?

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;
}

Modern ciphers produce binary output that should be indistinguishable from random noise. 现代密码产生的二进制输出应该与随机噪声无法区分。 If you interpret this binary output as text (ASCII, UTF-8, etc.) you might see a : in there if the ciphertext is long enough. 如果您将此二进制输出解释为文本(ASCII,UTF-8等),您可能会看到:如果密文足够长则在那里。 But you also see it for shorter ciphertexts, but not necessarily in every one. 但是你也看到它用于更短的密文,但不一定是每一个密文。 The point is, the output is binary and not a "string". 关键是,输出是二进制而不是“字符串”。

It is possible to encode the binary output to get a string. 可以对二进制输出进行编码以获得字符串。 If Base 64 or Hex are used, then there is no way to get a : , because that's not in their alphabet. 如果使用Base 64或Hex,则无法获得: ,因为它不在其字母表中。 If you decide to use Base 85 encoding, then you might get a : , depending on the specific alphabet (eg Z85 ). 如果您决定使用Base 85编码,那么您可能会得到: ,具体取决于特定的字母(例如Z85 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM