How to generate secure random numbers?

3 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2024-1-1
编辑:Dyuman Joshi 2024-1-2
" (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.

请先登录,再进行评论。

回答(3 个)

Hassaan
Hassaan 2024-1-1
编辑:Hassaan 2024-1-1
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 个评论
John D'Errico
John D'Errico 2024-1-2
编辑:John D'Errico 2024-1-2
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 2024-2-22
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2024-1-2
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 2024-1-2
编辑:Hassaan 2024-2-23
@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 个评论
Walter Roberson
Walter Roberson 2024-2-23
disp(['Secure Random Hex: ', secureRandomNumber]);
Are you sure the result is Hex? You coded base=10 in the URL.
Hassaan
Hassaan 2024-2-23
@Walter Roberson I missed on that. I have updated my answer. Thank you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by