SteamKit2.CryptoHelper.SymmetricEncrypt C# (CSharp) Method

SymmetricEncrypt() public static method

Performs an encryption using AES/CBC/PKCS7 with an input byte array and key, with a random IV prepended using AES/ECB/None
public static SymmetricEncrypt ( byte input, byte key ) : byte[]
input byte
key byte
return byte[]
        public static byte[] SymmetricEncrypt( byte[] input, byte[] key )
        {
            Debug.Assert( key.Length == 32 );

            using ( var aes = new RijndaelManaged() )
            {
                aes.BlockSize = 128;
                aes.KeySize = 256;

                // generate iv
                byte[] iv = GenerateRandomBlock( 16 );
                byte[] cryptedIv = new byte[ 16 ];


                // encrypt iv using ECB and provided key
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.None;

                using ( var aesTransform = aes.CreateEncryptor( key, null ) )
                {
                    cryptedIv = aesTransform.TransformFinalBlock( iv, 0, iv.Length );
                }

                // encrypt input plaintext with CBC using the generated (plaintext) IV and the provided key
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                using ( var aesTransform = aes.CreateEncryptor( key, iv ) )
                using ( var ms = new MemoryStream() )
                using ( var cs = new CryptoStream( ms, aesTransform, CryptoStreamMode.Write ) )
                {
                    cs.Write( input, 0, input.Length );
                    cs.FlushFinalBlock();

                    byte[] cipherText = ms.ToArray();

                    // final output is 16 byte ecb crypted IV + cbc crypted plaintext
                    byte[] output = new byte[ cryptedIv.Length + cipherText.Length ];

                    Array.Copy( cryptedIv, 0, output, 0, cryptedIv.Length );
                    Array.Copy( cipherText, 0, output, cryptedIv.Length, cipherText.Length );

                    return output;
                }
            }
        }

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Authenticate a CDNClient to a depot in the connected session
        /// </summary>
        /// <param name="depotid">The id of the depot being accessed.</param>
        /// <param name="depotKey">
        /// The optional depot decryption key for the depot that will be downloaded.
        /// This is used for decrypting filenames (if needed) in depot manifests, and processing depot chunks.
        /// </param>
        /// <param name="cdnAuthToken">CDN auth token for CDN content server endpoints.</param>
        /// <exception cref="HttpRequestException">An network error occurred when performing the request.</exception>
        /// <exception cref="SteamKitWebRequestException">A network error occurred when performing the request.</exception>
        public async Task AuthenticateDepotAsync(uint depotid, byte[] depotKey = null, string cdnAuthToken = null)
        {
            if (depotIds.ContainsKey(depotid))
            {
                return;
            }

            string data;

            if (connectedServer.Type != "CDN" || cdnAuthToken == null)
            {
                if (appTicket == null)
                {
                    data = string.Format("depotid={0}", depotid);
                }
                else
                {
                    byte[] encryptedAppTicket = CryptoHelper.SymmetricEncrypt(appTicket, sessionKey);
                    data = string.Format("appticket={0}", WebHelpers.UrlEncode(encryptedAppTicket));
                }

                await DoCommandAsync(connectedServer, HttpMethod.Post, "authdepot", data, doAuth : true).ConfigureAwait(false);
            }

            depotIds[depotid]         = true;
            depotKeys[depotid]        = depotKey;
            depotCdnAuthKeys[depotid] = cdnAuthToken;
        }
All Usage Examples Of SteamKit2.CryptoHelper::SymmetricEncrypt