title: How to automate activation of a HIKVISION DS-2CD2512F-IS?
body: <p>I've got a HIKVISION DS-2CD2512F-IS, and I'm trying to automate the activation step from a Linux box, and I'm stuck trying to figure out how to reverse-engineer how the web interface does it. Setting up everything else is easy - it's just a matter of uploading a bunch of XML files to the URL with the username and password embedded in the URL - but the initial activation step to set the password is driving me nutty. Sure, I can do it easily through the web interface, or through the HIKVISION software ... but if the camera works out, they'll need to be activated 30 at a time by a hundred different guys every year all around the country, and a typo in a password means that camera won't play. Plus, of course, the contractors who'd be doing this are not exactly 100% reliable on reading or following instructions (no exaggeration, it's pretty common for a contractor to say "Nobody told me I'd need to bring that" when it's on the work order in three different spots, in bold), so I can't be sure they'd bring a laptop with the appropriate software to be able to do it anyway. So for reliability and labor cost reasons, I'd like to automate everything involved. The only sticking point for me so far is the activation step on the camera.</p>
  
  <p>At all of these locations, I'm going to ship a linux box under my control (it's a cloned image). I can put a bash script on the master for that (as well as any command-line programs I like), and assign a button on the machine to run it, no problems, and I'm confident in automating everything except the activation at this point.</p>
  
  <p>Now, their customer service team sent me an activation guide (Which you can see here: <a href="http://docdro.id/ntP7v8w" rel="nofollow">http://docdro.id/ntP7v8w</a>), but whoever wrote it apparently didn't have English as their first language. I've used WireShark to listen in on the exchange between my browser and the camera, and it's definitely using the referenced API to do the actual activation. I can't seem to make it work, though. My test script so far ... I can't seem to get it to render well here, sadly. Going through the camera's javascript (painful, as someone ran a minimizer on it, so there aren't any comments and all the variables are single letters) suggests that I need to:</p>
  
  <p>1)    Generate my RSA key.
  2)    Change my key out of base 64 into binary.
  3)    Convert the binary key into a base 16 key.
  4)    Re-encode that base 16 key into base 64.
  5)    Transmit the key to the camera (wrapped in XML).
  6)    Change the return out of base 64 (after unwrapping the XML).
  7)    Convert that extracted information into binary, treating it as a base 16 human-readable integer for the conversion.
  8)    Convert that binary information into the base64 that openssl takes.
  9)    Decrypt the camera's response with my key from 1.
  10)    Use said random string as an AES key (which... will very likely involve steps 7 and 8 again) to encrypt my password with openssl.
  11)    Extract the binary data from that result by undoing the base64 transition.
  12)    Convert that binary data into an ascii-encoded base-16 integer
  13)    Convert that ascii-encoded base-16 integer into base64
  14)    transmit that to the camera (wrapped in XML)</p>
  
  <p>... and I'm hitting a bit of a brick wall in figuring out how to make that happen in Linux. Any help?</p>
  
  <p>As a side note, it seems the API pages referenced in the documentation remain active post-activation... not an issue for my planned implementation, as they'll all be on a physically isolated network, but not good.</p>