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!