Last active
March 11, 2024 16:28
-
-
Save bazooka07/bed368d313e218fcba332cb2127c70b1 to your computer and use it in GitHub Desktop.
ESP8266 - encryption with LUA, Python 3 and PHP 7.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Nodemcu 2.2.0.0 - LUA version 5.1.4 | |
method = 'AES-CBC' | |
key = '1aec7447f7a919ef2910bd897bf869fe' -- md5('You are completely crazy') | |
values = '{"epoc":1528711193,"temperature":28.2,"humidity":41.1,"timestamp":"2018-06-11T09:59:53","client_id":"esp8266-1458415"}' | |
iv = 'tARGGrNw39xRlQ4D' -- length of 16 chars | |
print('length of values : '..#values) | |
print('key : '..key) | |
print('iv : '..iv) | |
buf = values..' ' -- 15 spaces | |
n = #buf - #buf % 16 -- divided by 16 | |
print('Encrypted values :') | |
print(crypto.toBase64(crypto.encrypt(method, key, buf:sub(1, n), iv))) | |
-- Result : | |
-- wq+4YetP0xCK8S0nPEK5gw3AdSk9T6Q0VX2yD8/8gMYIQ8urafkmfDvN6EPR6+1jjh8uzJQ+vjVweqyH+Sk/8vgRP5zzUADSGWRZ1r+f2tBOMjCmidJTOejWaHLFx1t62vp/qHNFFrPbVuSxBJeja3/UVi9k1lE9e5kQvNnO/6o= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
# http://php.net/manual/fr/function.openssl-encrypt.php | |
$key = '1aec7447f7a919ef2910bd897bf869fe'; # md5('You are completely crazy') | |
$values = '{"epoc":1528711193,"temperature":28.2,"humidity":41.1,"timestamp":"2018-06-11T09:59:53","client_id":"esp8266-1458415"}'; | |
$iv = 'tARGGrNw39xRlQ4D'; # length of 16 chars | |
# $method = 'aes-128-cbc'; | |
$method = 'aes-256-cbc'; | |
echo "Encryption with \"$method\" method\n"; | |
echo 'Length of values : '.strlen($values)."\n"; | |
echo "key : $key\n"; | |
echo "iv : $iv\n"; | |
$buf = $values.str_repeat(' ', 15); | |
$n = strlen($buf) - strlen($buf) % 16; # divided by 16 | |
echo "$n\n"; | |
echo "Encrypted values :\n"; | |
echo openssl_encrypt(substr($buf, 0, $n), $method, $key, OPENSSL_ZERO_PADDING, $iv); | |
echo "\n"; | |
# Results : | |
# with PHP 7.1.17-0ubuntu0.17.10.1 | |
# wq+4YetP0xCK8S0nPEK5gw3AdSk9T6Q0VX2yD8/8gMYIQ8urafkmfDvN6EPR6+1jjh8uzJQ+vjVweqyH+Sk/8vgRP5zzUADSGWRZ1r+f2tBOMjCmidJTOejWaHLFx1t62vp/qHNFFrPbVuSxBJeja3/UVi9k1lE9e5kQvNnO/6o= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!//usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from Crypto.Cipher import AES | |
import base64 | |
key = '1aec7447f7a919ef2910bd897bf869fe' # md5('You are completely crazy') | |
values = '{"epoc":1528711193,"temperature":28.2,"humidity":41.1,"timestamp":"2018-06-11T09:59:53","client_id":"esp8266-1458415"}' | |
iv = 'tARGGrNw39xRlQ4D' # length of 16 chars | |
print(''' | |
Length of values : %d | |
key : %s | |
iv : %s | |
''' % (len(values), key, iv)) | |
buf = values + ' ' | |
n = len(buf) - len(buf) % 16 | |
encoder = AES.new(key, mode=AES.MODE_CBC, IV=iv) | |
data = encoder.encrypt(buf[0:n]) | |
print('Encrypted values : ') | |
print(base64.b64encode(data)) | |
# Result : | |
# b'wq+4YetP0xCK8S0nPEK5gw3AdSk9T6Q0VX2yD8/8gMYIQ8urafkmfDvN6EPR6+1jjh8uzJQ+vjVweqyH+Sk/8vgRP5zzUADSGWRZ1r+f2tBOMjCmidJTOejWaHLFx1t62vp/qHNFFrPbVuSxBJeja3/UVi9k1lE9e5kQvNnO/6o=' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment