My program generate AES key, crypt it with rsa algorithm and send to server. But when I fetch it from server and decrypt some bytes become corrupted and replace with sequence 239 191 189 According to this question UTF-8 Encoding and decoding issue I realized that the problem is that server (that also decrypt key) store it as UTF-8 string. Is there any way to create "UTF-8 friendly" keys? There how now generating keys:
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7Padding"); CipherKeyGenerator aesKeyGenerator = new CipherKeyGenerator(); var rand = new SecureRandom(); aesKeyGenerator.Init(new KeyGenerationParameters(rand, 256)); byte[] key = aesKeyGenerator.GenerateKey(); var aesKey = ParameterUtilities.CreateKeyParameter("AES", _key); var iv = new byte[32]; rand.NextBytes(iv); ParametersWithIV aesKeyParam = new ParametersWithIV(aesKey, iv);