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
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. 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. If you decide to use Base 85 encoding, then you might get a :
, depending on the specific alphabet (eg Z85 ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.