Borrar filtros
Borrar filtros

How to generate secure random numbers?

3 visualizaciones (últimos 30 días)
Xiang Xu
Xiang Xu el 1 de En. de 2024
Editada: Hassaan el 23 de Feb. de 2024
I use Bcrypt to generate secure random numbers in a C project. It seems that MATLAB 'rand' function only support some pseudorandom algorithm, which is not satisfying. Is there a way to generate secure random numbers with MATLAB (without interact with another programming language)? Will Bcrypt be introduced into MATLAB?
  1 comentario
Dyuman Joshi
Dyuman Joshi el 1 de En. de 2024
Editada: Dyuman Joshi el 2 de En. de 2024
" (without interact with another programming language) "
Depends on what you mean by interaction.
Secure Random Numbers as a programming concept seems to be only applied in Java, which is implemented below in MATLAB by calling a Java library.

Iniciar sesión para comentar.

Respuestas (3)

Hassaan
Hassaan el 1 de En. de 2024
Editada: Hassaan el 1 de En. de 2024
1. Java Security Libraries:
MATLAB doesn't directly run on the JVM, it includes a JVM to enable interaction with Java code and libraries and has built-in support for Java. You can use Java's cryptographic libraries to generate secure random numbers:
% Create a Java SecureRandom object
secureRandom = java.security.SecureRandom();
% Generate secure random numbers
numBytes = 16; % For 128-bit number
randomBytes = zeros(1, numBytes, 'uint8');
for i = 1:numBytes
randomBytes(i) = secureRandom.nextInt(256); % Generates a number between 0 and 255
end
% Convert each byte to a hexadecimal string representation
hexString = dec2hex(randomBytes);
% Concatenate the individual hex strings into one long string
hexString = strcat(hexString(:)');
disp(['Secure Random Hex: ', hexString]);
Secure Random Hex: 5DDF89C9703E37DD343BF18BD3FCFEDE
2. External Libraries via MEX:
If there's a specific cryptographic library or function you want to use (like Bcrypt), you can write a C/C++ program that uses this library and compile it to a MEX file that MATLAB can execute. This is a more advanced solution and requires familiarity with C/C++ and the MEX compilation process.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering
  5 comentarios
John D'Errico
John D'Errico el 2 de En. de 2024
Editada: John D'Errico el 2 de En. de 2024
While this solution may work for now, there is no assurance it will work forever. That is, Java may not be usable from MATLAB forever.
(While I hope that is not the case since I depend on Java for one tool of my own, I don't make the decisions.)
Adrián Lascurain
Adrián Lascurain el 22 de Feb. de 2024
Do you have any advice to guarantee access to java security library? I've seen that javaclasspath let you run certain functions of a java class object but I do not have much idea how to implement it.
Thanks beforehand.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 2 de En. de 2024
Will Bcrypt be introduced into MATLAB?
I very much doubt that Bcrypt will be included into MATLAB.
Is there a way to generate secure random numbers with MATLAB (without interact with another programming language)?
You can urlread() or equivalent to connect to a remote site that serves random numbers.

Hassaan
Hassaan el 2 de En. de 2024
Editada: Hassaan el 23 de Feb. de 2024
@Xiang Xu One of the new approach as pointed by @Walter Roberson [special thanks]. The demo usage can be:
% Example URL (replace with the service URL you want to use. For this demo i am using 'www.random.org')
randomNumberServiceURL = 'https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new';
randomNumberServiceURLHex = 'https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=16&format=plain&rnd=new';
% Retrieve a secure random number from the remote service
secureRandomNumber = webread(randomNumberServiceURL);
secureRandomNumberHex = webread(randomNumberServiceURLHex);
disp(['Secure Random: ', secureRandomNumber]);
Secure Random: 26
disp(['Secure Random Hex: ', secureRandomNumberHex]);
Secure Random Hex: 1a
Note:
  • But obviously you need to have internet access to excess this external server URL.
  • base=16 can be provided in the URL for Hex
  • base=10 can be provided in the URL for DEC
  • hex(dec_number) and int(hex_number, 16) can also be used for the respective conversion from one base to another
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering
  2 comentarios
Walter Roberson
Walter Roberson el 23 de Feb. de 2024
disp(['Secure Random Hex: ', secureRandomNumber]);
Are you sure the result is Hex? You coded base=10 in the URL.
Hassaan
Hassaan el 23 de Feb. de 2024
@Walter Roberson I missed on that. I have updated my answer. Thank you.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by