Encoding user information into a numeric format
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Tero
 el 3 de Mayo de 2019
  
    
    
    
    
    Comentada: Tero
 el 6 de Mayo de 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,
Tero
2 comentarios
  Rik
      
      
 el 3 de Mayo de 2019
				
      Editada: Rik
      
      
 el 3 de Mayo de 2019
  
			You could use a hash, or even part of a hash. That will not be easily reversible, and if you only encode 10 decimal digits, you can only store 10^10 unique information elements. That sounds like a lot, but for a 26 letter alphabet, that allows only 7 characters to be stored (26^8>10^10).
  Walter Roberson
      
      
 el 3 de Mayo de 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.
Respuesta aceptada
  Walter Roberson
      
      
 el 3 de Mayo de 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.
5 comentarios
  Walter Roberson
      
      
 el 6 de Mayo de 2019
				The Java methods that Jan showed are a cleaner implementation. However, gzip to an external file is certainly an easier implementation.
Más respuestas (1)
  Jan
      
      
 el 3 de Mayo de 2019
        
      Editada: Jan
      
      
 el 4 de Mayo de 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
   Engine.update(uint8(Key));
   Key = typecast(Engine.digest, 'uint8');
end
% 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(ipad);
Engine.update(uint8(Msg));   % Fails for empty Msg!
iHash = typecast(Engine.digest, 'uint8');
Engine.update(opad);
Engine.update(iHash);
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);
ZIPStream.setLevel(9);
entry      = ZipEntry('Value');
entry.setSize(numel(ByteData));
ZIPStream.putNextEntry(entry);
ZIPStream.write(ByteData);
ZIPStream.closeEntry();
ZIPStream.close();
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.
5 comentarios
  Walter Roberson
      
      
 el 6 de Mayo de 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.
Ver también
Categorías
				Más información sobre C Shared Library Integration en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



