Encoding user information into a numeric format

8 views (last 30 days)
Tero on 3 May 2019
Commented: Tero on 6 May 2019
Hi all,
is there such an algorithm out there that takes in a plain text message, and returns a (short) string of integers? This would also need to be a reversable process. I am not looking for a strong ciphering algorithm, but rather a method to encode user information into a simple licensing number. The idea behind is to have this license number included in a digitally signed license file, from which I could then extract the user information to be displayed on a splash screen.
As such, the encoded string of numbers should not be very long - like max 10-15 digits, regardless of the length of the plain text message
Thanks in advance,
Walter Roberson
Walter Roberson on 3 May 2019
reversible hash are tricky to find.
We have to be very careful about how we talk about this. Your tags mentioned Caesar cipher which we cannot talk about for legal reasons. But we can talk about encoding data, and in one of those quirks of bad law we can talk about authentication methods.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 May 2019
Take the data and lz compress it. You can use a java gzip method. Now base 64 encode it. Result goes into your license file.
At runtime base 64 decode, unzip into memory, extract.
Tero on 6 May 2019
well, to be honest, I'm not really sure which solution I'm following at... I took the CompressLib from the File Exhange, and base 64 encoded the result. This works :)

Sign in to comment.

More Answers (1)

Jan on 3 May 2019
Edited: Jan on 4 May 2019
No, this cannot work. You can compress the text e.g. in ZIP format, but this cannot guarantee a length of 10 to 15 bytes for the output. It would be pure magic, if text can be compressed such efficiently in a reversible way.
You can create a text with the full user information and use an HMAC key to get a unique and secure vector:
YourKey = 'Hello,this is my secret key#3.14*'
Msg = sprintf(['Customer: Karl Heinz aus Rostock\n', ...
'Karlsonstraße 27']);
YourKey = uint8(YourKey(:));
Msg = uint8(Msg(:));
Method = 'MD5';
Block = 64; % 64 for: MD5, SHA-1, SHA-256, 128: SHA-384, SHA-512
Engine = java.security.MessageDigest.getInstance(Method);
% Encrypt key if it is longer than the block size:
if length(Key) > BlockSize % Alternatively: In every case
Key = typecast(Engine.digest, 'uint8');
% Padding
KeySize = numel(Key);
ipad(1:BlockSize) = uint8(54); % 0x36
ipad(1:KeySize) = bitxor(uint8(54), Key);
opad(1:BlockSize) = uint8(92); % 0x5c
opad(1:KeySize) = bitxor(uint8(92), Key);
% Calculate the hash:
Engine.update(uint8(Msg)); % Fails for empty Msg!
iHash = typecast(Engine.digest, 'uint8');
HMAC = typecast(Engine.digest, 'uint8');
% Output:
HMAC = reshape(HMAC, 1, []); % As UINT8 vector
% Or Hex: HMAC = sprintf('%.2x', double(HMAC));
% Or base64: HMAC = matlab.net.base64encode(HMAC);
% Or B64 = org.apache.commons.codec.binary.Base64;
% HMAC = char(B64.encode(HMAC)).';
% Shorter: as base64, then: HMAC(HMAC == '=') = [];
With the shortend base64 encoding this gives you 22 bytes. You cannot recreate the original message with this, but you can prove with your secret key, that the HMAC belongs to this specific text file and that the text file has not been modified.
I'm going to publish this method in the FileExchange soon.
By the way, the method for zipping some data:
import java.io.*;
import java.util.zip.*;
Msg = sprintf(['Customer: Karl Heinz aus Rostock\n', ...
'Karlsonstraße 27']);
ByteData = uint8(Msg(:)); % [EDITED, not TYPECAST()]
ByteStream = ByteArrayOutputStream();
ZIPStream = ZipOutputStream(ByteStream);
entry = ZipEntry('Value');
Byte = ByteStream.toByteArray();
You get 172 bytes for this input with 49 characters. ZIP has more advantages for longer input: For Msg = repmat(Msg, 1, 10) the output has 177 bytes, just 5 bytes more.
I assume the HMAC and the clear text message in a text file is better for your needs.
Walter Roberson
Walter Roberson on 6 May 2019
Put some sample data into a text file without any headers but with a field delimiter even if only newline. Use a short file name. gzip -9 it. Also gzip an empty file with the same length of file name. Subtract lengths to get an estimate of the encoding length of needed for in memory encoding like Jan showed Java methods for. If it is significantly longer than you are willing to store in the license file then you are out of luck. If it starts getting close then time to start testing with the Java code.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by