Need help to encrypt and decrypt the RS232 serial port data

9 views (last 30 days)
I am doing project on wireless smart meter using XBee. my aim is to transmit and receive smart meter data from one XBee to another XBee with encryption. So I need help to write code for encrypt at transmit side XBee(RS232 protocol), and code for decrypt at receive side XBee.

Accepted Answer

Walter Roberson
Walter Roberson on 20 May 2015
function send_encrypted(s, data_to_send)
%s is to be a serial object, data_to_send is numeric
data_as_bytes = typecast(data_to_send, 'uint8');
encrypted_bytes = encrypt_bytes(data_as_bytes);
cname = uint8(0 + class(data_to_send)); %chars to bytes
cnamehdr = [uint8(length(cname)), cname];
enchdr = uint16(length(encrypted_bytes));
header = [cnamehdr, enchdr];
fwrite(s, header);
fwrite(s, encrypted_bytes);
end
function data = receive_encrypted(s)
%s is the serial connection.
cnamelen = fread(s, 1, 'uint8');
cname = char(fread(s, cnamelen, 'uint8'));
numencbytes = fread(s, 1, 'uint16');
encbytes = fread(s, numencbytes, 'uint8');
data = typecast( decrypt_bytes(encbytes), cname); %turn it back to double
end
function enc = encrypt_bytes(bytes_to_encrypt)
%magic encryption goes here
enc = 255 - bytes_to_encrypt; %this is the encryption
end
function decr = decrypt_bytes(bytes_to_decrypt);
%magic decryption goes here
decr = 255 - bytes_to_decrypt; %this undoes the encryption
end
Extensions would be required to handle more than 65535 bytes of data per transmission, or to transparently handle strings during transmission, or if you wanted to send data with mixed data types.
By the way, you did know that the Zigbee support AES encryption in hardware, right?
  1 Comment
SINGARAVELAN ARUMUGAM
SINGARAVELAN ARUMUGAM on 21 May 2015
Thanks Mr.Walter Roberson I am using AES as well as Dynamic Secret Encryption(DSE) for more data protection. So before going to code in microcontroller I am trying Dynamic Secret Encryption in MATLAB. In DSE different Encryption Key will generate time to time. so no one can find the Encryption Key. adversary can't able to identify all the Key generated by DSE.

Sign in to comment.

More Answers (0)

Categories

Find more on Encryption / Cryptography in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!