How to generate secure random numbers?
6 ビュー (過去 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
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
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]);
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
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
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
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.
0 件のコメント
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]);
disp(['Secure Random Hex: ', secureRandomNumberHex]);
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
2024 年 2 月 23 日
disp(['Secure Random Hex: ', secureRandomNumber]);
Are you sure the result is Hex? You coded base=10 in the URL.
参考
カテゴリ
Help Center および File Exchange で Construct and Work with Object Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!