필터 지우기
필터 지우기

How to generate secure random numbers?

조회 수: 3 (최근 30일)
Xiang Xu
Xiang Xu 2024년 1월 1일
편집: Hassaan 2024년 2월 23일
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