/// <summary> /// Decrypts the target key with the CryptoManager's private RSA key. It is assumbed that the /// @rijKey was previously encrypted with the CryptoManager's public key. /// </summary> /// <param name="rijKey"></param> /// <returns></returns> public byte[] DecryptRijndaelKey(byte[] rijKey) { byte[] key = new byte[0]; lock (this) { m_RSA = new RiaWebSoftRu.Cryptography.Crypto.RsaManaged(); m_RSA.FromXmlString(System.Text.UTF8Encoding.UTF8.GetString(m_PrivateKey)); m_RSA.Decrypt(rijKey, false); } return(key); }
public UnityCryptoManager() { m_RSA_Exch_Formatter = new RiaWebSoftRu.Cryptography.Crypto.Formater.RSAESPKCSFormater(); m_RSA = new RiaWebSoftRu.Cryptography.Crypto.RsaManaged(m_KeySize); m_SHA = new SHA256Managed(); m_RIJ = new AesManaged(); //m_RSA.GenerateKey(); Not necessary in unity. Unity clients gets a public key from server and uses that to encrypt the Rijndael key m_SHA = new SHA256Managed(); m_RIJ.Padding = PaddingMode.PKCS7; m_RIJ.Mode = CipherMode.CBC; m_RIJ.IV = new byte[16]; m_RIJ.BlockSize = 128; m_RIJ.KeySize = 256; m_RIJ.GenerateKey(); m_PublicKey = Encoding.UTF8.GetBytes(m_RSA.ToXmlString(false).ToString()); m_PrivateKey = Encoding.UTF8.GetBytes(m_RSA.ToXmlString(true).ToString()); }
/// <summary> /// Encrypts the target @rijKey with the given RSA public key. Generally, the public RSA key is /// transmitted via plaintext and then used to encrypt the Rijndael key. The encrypted Rijndael key can then /// only be decrypted by the holder of the private RSA key that was generated along with the public key. /// This is the core of the encryption key exchange at the beginning of all Kronus network communication. /// </summary> /// <param name="publicKey">the public RSA key used to encrpt @rijKey</param> /// <param name="rijKey">the Rijndael key to encrypt</param> /// <returns></returns> public byte[] EncryptRijndaelKey(byte[] publicKey, byte[] rijKey) { byte[] key = new byte[0]; lock (this) { m_RSA = new RiaWebSoftRu.Cryptography.Crypto.RsaManaged(); m_RSA.FromXmlString(System.Text.UTF8Encoding.UTF8.GetString(publicKey)); key = m_RSA.Encrypt(rijKey, false); } return key; }