Skip to content

Instantly share code, notes, and snippets.

@companje
Last active May 10, 2021 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save companje/b700748a49f4af73d57011c644f5a778 to your computer and use it in GitHub Desktop.
Save companje/b700748a49f4af73d57011c644f5a778 to your computer and use it in GitHub Desktop.
Experiment with Davinci version 3 protocol encrypt / decrypt / upload (based on pythreedub)
from Crypto.Cipher.AES import AESCipher, MODE_ECB, MODE_CBC
file = open("flatcube/flatcube-1-layer.3w", 'rb')
string = file.read()
enc_gcode = string[0x2000:]
aes = AESCipher("@xyzprinting.com@xyzprinting.com", mode=MODE_ECB, IV=chr(0)*16)
gcode = aes.decrypt(enc_gcode)
print gcode
import time, serial, sys, string, struct, binascii
import Padding
from io import BytesIO
from Crypto.Cipher.AES import AESCipher, MODE_ECB, MODE_CBC
# read gcode
file = open("flatcube/flatcube-1-layer.gcode", 'rb')
lines = file.readlines()
# encrypt header
key = "@xyzprinting.com"
iv = chr(0)*16
aes = AESCipher(key, mode=MODE_CBC, IV=iv)
header_text = "".join([line for line in lines if line.startswith(";")])
padded = Padding.appendPadding(header_text)
encrypted_header = aes.encrypt(padded)
# encrypt fulltext
key = "@xyzprinting.com@xyzprinting.com"
iv = chr(0)*16
aes = AESCipher(key, mode=MODE_ECB, IV=iv)
fulltext = "".join(lines)
padded = Padding.appendPadding(fulltext)
encrypted_fulltext = aes.encrypt(padded)
crc32 = binascii.crc32(encrypted_fulltext)
crcstr = struct.pack(">l", crc32)
# write encrypted header and encrypted fulltext
bio = BytesIO()
bio.write("3DPFNKG13WTW")
bio.write(struct.pack("8B", 1, 2, 0, 0, 0, 0, 18, 76))
bio.write(chr(0)*4684)
bio.write("TagEJ256")
bio.write(struct.pack("4B", 0, 0, 0, 68))
bio.write(crcstr)
bio.write((chr(0)*(68 - len(crcstr))))
bio.write(encrypted_header)
left = 8192 - bio.tell()
bio.write((chr(0)*left))
bio.write(encrypted_fulltext)
print bio.getvalue()
import time, serial, sys
import copy
import string
import os
from Crypto.Cipher.AES import AESCipher, MODE_ECB, MODE_CBC
import logging
import struct
log = logging.getLogger(__name__)
UploadCmd = "XYZv3/upload={filename},{size}{option}"
UploadDidFinishCmd = "XYZv3/uploadDidFinish"
ser = serial.Serial(
port="/dev/tty.usbmodem1411",
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
def write(data):
ser.write(data)
ser.flush()
def writeline(data):
write(data+"\n")
def readline():
return ser.readline()
def readlines(expect=None):
buf = ""
line = None
while line is None or line:
line = readline()
if line:
buf += line
if line.strip() == expect:
break
elif line.strip() == "E0":
return buf
return buf
def wait_for_ok(expect="ok"):
resp = readlines(expect=expect)
if not resp or resp.strip() != "ok":
raise Exception("Expected token not found: {}, got '{}' instead".format(expect,resp.strip()))
f = open("flatcube/flatcube-1-layer.3w", 'rb')
data = f.read()
size = os.fstat(f.fileno()).st_size
writeline(UploadCmd.format(filename="temp.gcode", size=size, option=""))
wait_for_ok();
chunks = (len(data)+8191) / 8192
prev = ""
blocksize = 8192
for n in range(0, chunks):
log.debug("Sending file chunk {}/{}".format(n, chunks))
chunk = struct.pack(">l", n) + struct.pack(">l", blocksize)
start = 8192*n
chunk += data[start:start+8192]
chunk += "\x00\x00\x00\x00"
write(chunk)
wait_for_ok()
write(UploadDidFinishCmd)
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment