When I generate a random byte sequence, decode the sequence into a string representation, then encode it back to a byte array, it is different from the original encoded sequence. See example below:
[byte[]]$key = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($key)
$key
output: 15 173 198 89 162 161 144 104 125 86 154 204 166 238 193 40 51 58 167 0 150 118 37 203 198 161 64 229 101 25 176 201
$decoded = [System.Text.Encoding]::UTF8.GetString($key)
$encoded = [System.Text.Encoding]::UTF8.GetBytes($decoded)
$encoded
output: 15 239 191 189 239 191 189 89 239 191 189 239 191 189 239 191 189 104 125 86 239 191 189 204 166 239 191 189 239 191 189 40 51 58 239 191 189 0 239 191 189 118 37 239 191 189 198 161 64 239 191 189 101 25 239 191 189 239 191 189
The byte sequence was clearly modified after decoding/encoding. This process works fine if I use [System.Text.Encoding]::Unicode...
. It seems that UTF8 can't handle certain bytes, but I was under the impression that UTF8 should be able to handle any character in the unicode standard. Can someone explain why this happens? Please and thanks