Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the equivalent of C#'s GetBytes() in Python?
    text
    copied!<p>I have </p> <pre><code>byte[] request = UTF8Encoding.UTF8.GetBytes(requestParams); </code></pre> <p>in a C# AES encryption only class that I'm converting to Python. Can anyone tell me the Python 2.5 equivalent(I'm using this on google app engine?</p> <p>Example inputs:</p> <p><strong>request_params:</strong> &amp;r=p&amp;playerid=6263017 (or a combination of query strings)<br> <strong>dev_key:</strong> GK1FzK12iPYKE9Kt<br> <strong>dev_iv:</strong> E2I21NEwsC9RdSN2<br> <strong>dev_id:</strong> 12 </p> <p><strong>Python function:</strong></p> <pre><code>def Encrypt(self, request_params, dev_key, dev_iv, dev_id): data_bytes = request_params.encode("utf-8") block_size = 16 mode = AES.MODE_CBC assert len(dev_key) == block_size and len(dev_iv) == block_size pad_char = '0' pad_length = block_size - len(data_bytes) % block_size padded_data_bytes = data_bytes + pad_length * pad_char encrypted_bytes = dev_iv + AES.new(dev_key, mode, dev_iv).encrypt(padded_data_bytes) base64_encrypted_string = base64.urlsafe_b64encode(str(encrypted_bytes)) request_uri = "http://api.blackoutrugby.com/?d=" + dev_id + "&amp;er=" + base64_encrypted_string #http://api.blackoutrugby.com/?d=19&amp;er=RTJJNTFORXdzQzNSZFNObNerdsGhiNoeue6c3mzed4Ty1YE-gTlVJVXHz05uPT-8 # output from this Python code, it's incorrect #http://api.blackoutrugby.com/?d=19&amp;er=16t2waGI2h657pzebN53hPr4kEjOzgsOEZiycDwPXR4= # correct output from C# code return request_uri </code></pre> <p><strong>C# Class:</strong></p> <pre><code>using System; using System.Security.Cryptography; using System.Text; using System.IO; using System.Net; using System.Xml; using Newtonsoft.Json; namespace BlackoutRugbyPOC.Controllers { public class BlackoutRugbyAPI { public static string Request(string requestParams, string devKey, string devIV, string devID) { // Create an unencrypted request as an array of bytes byte[] request = UTF8Encoding.UTF8.GetBytes(requestParams); byte[] key = UTF8Encoding.UTF8.GetBytes(devKey); byte[] iv = UTF8Encoding.UTF8.GetBytes(devIV); AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); aes.Key = key; aes.IV = iv; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.Zeros; // Get the transformer from the AES Encryptor ICryptoTransform cTransform = aes.CreateEncryptor(); // Use the transformer to encrypt our request byte[] result = cTransform.TransformFinalBlock(request, 0, request.Length); aes.Clear(); // Encode to base64 string encryptedRequest = Convert.ToBase64String(result, 0, result.Length); // Send request to API string requestUri = "http://api.blackoutrugby.com/?d=" + devID + "&amp;er=" + encryptedRequest; string xmlResponse = getWebResponse(requestUri); return XmlToJson(xmlResponse); } private static string getWebResponse(string url) { string html = ""; WebRequest request = HttpWebRequest.Create(url); WebResponse response = request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { html = reader.ReadToEnd(); } return html; } public static string XmlToJson(string xml) { if (string.IsNullOrEmpty(xml)) throw new ArgumentNullException("XML Input"); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(xml); } catch { throw new ArgumentNullException("Input could not be loaded into XML Document"); } return JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented); } } } </code></pre> <p>Thanks,<br> Denis</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload