The rdEncrypt128 function will encrypt data by using Rijndael encryption algorithm with a 128 bit key.
The function encrypts the data in blocks of 16 bytes. If the data is not a multiple of 16, the result of the operation is undefined.
Input:
key : PTR
Array containing the key used to encrypt the data.
Length of data must be 16 bytes = 128 bits.
buf_in : PTR
Buffer containing the raw data to encrypt.
buf_out : PTR
Buffer where the encrypted data is stored.
out_len : INT
Size of "buf_out" in number of bytes.
blk : INT
The number of block from the raw data to encrypt. Each block is 16 bytes long.
Returns: INT
0
|
Data is encrypted.
|
-1
|
Input buffer and output buffer cannot be the same.
|
-2
|
Invalid number of blocks.
|
-3
|
Encryption key is missing.
|
-4
|
Input buffer is missing.
|
-5
|
Output buffer is missing.
|
-6
|
"buf_out" buffer too small for decrypted data.
|
Declaration:
FUNCTION rdEncrypt128 : INT;
VAR_INPUT
key : PTR;
buf_in : PTR;
buf_out : PTR;
out_len : INT;
blk : INT;
END_VAR;
Example:
INCLUDE rtcu.inc
VAR
key : ARRAY [1..16] OF SINT;
src : ARRAY [1..64] OF SINT;
crypt : ARRAY [1..64] OF SINT;
dst : ARRAY [1..64] OF SINT;
END_VAR;
PROGRAM example;
VAR
len : INT;
str : STRING;
END_VAR;
key[01] := 16#F4; key[02] := 16#98; key[03] := 16#D8; key[04] := 16#16;
key[05] := 16#22; key[06] := 16#4F; key[07] := 16#47; key[08] := 16#3F;
key[09] := 16#91; key[10] := 16#85; key[11] := 16#96; key[12] := 16#B8;
key[13] := 16#DD; key[14] := 16#57; key[15] := 16#51; key[16] := 16#BE;
str := "This is a test of encryption";
DebugMsg(message := "Original= " + str);
strToMemory(dst := ADDR(src[2]), str := str, len := strLen(str := str));
src[1] := SINT(strLen(str := str));
len := (src[1] + 1) / 16;
IF (src[1] + 1) MOD 16 > 0 THEN len := len + 1; END_IF;
rdEncrypt128(key := ADDR(key), buf_in := ADDR(src), buf_out := ADDR(crypt), out_len := 64, blk := len);
...
rdDecrypt128(key := ADDR(key), buf_in := ADDR(crypt), buf_out := ADDR(dst), out_len := 64, blk := len);
DebugMsg(message := "Decrypted= " + strFromMemory(src := ADDR(dst[2]), len := dst[1]));
END_PROGRAM;
|