<p>Can anyone tell me what exactly does this Java code do?</p>

<pre><code>SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
byte[] bytes = new byte[20];
synchronized (random)
{
    random.nextBytes(bytes);
}

return Base64.encode(bytes);
</code></pre>

<hr>

<p>Step by step explanation will be useful so that I can recreate this code in VB. Thanks</p>

[{'answer_id': 72520, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>This creates a random number generator (SecureRandom). It then creates a byte array (byte[] bytes), length 20 bytes, and populates it with random data.</p>\n\n<p>This is then encoded using BASE64 and returned.</p>\n\n<p>So, in a nutshell,</p>\n\n<ol>\n<li>Generate 20 random bytes</li>\n<li>Encode using Base 64</li>\n</ol>\n'}, {'answer_id': 72531, 'author': 'Aidos', 'author_id': 12040, 'author_profile': 'https://Stackoverflow.com/users/12040', 'pm_score': 1, 'selected': False, 'text': "<p>It creates a SHA1 based random number generator (RNG), then Base64 encodes the next 20 bytes returned by the RNG.</p>\n\n<p>I can't tell you why it does this however without some more context :-).</p>\n"}, {'answer_id': 72588, 'author': 'Bill the Lizard', 'author_id': 1288, 'author_profile': 'https://Stackoverflow.com/users/1288', 'pm_score': 1, 'selected': False, 'text': '<p>This code gets a cryptographically strong random number that is 20 bytes in length, then Base64 encodes it.  There\'s a lot of Java library code here, so your guess is as good as mine as to how to do it in VB.</p>\n\n<pre><code>SecureRandom random = SecureRandom.getInstance("SHA1PRNG");\nbyte[] bytes = new byte[20];\nsynchronized (random) { random.nextBytes(bytes); }\nreturn Base64.encode(bytes);\n</code></pre>\n\n<p>The first line creates an instance of the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/security/SecureRandom.html" rel="nofollow noreferrer">SecureRandom</a> class.  This class provides a cryptographically strong pseudo-random number generator.</p>\n\n<p>The second line declares a byte array of length 20.</p>\n\n<p>The third line reads the next 20 random bytes into the array created in line 2.  It synchronizes on the SecureRandom object so that there are no conflicts from other threads that may be using the object.  It\'s not apparent from this code why you need to do this.</p>\n\n<p>The fourth line Base64 encodes the resulting byte array.  This is probably for transmission, storage, or display in a known format.</p>\n'}, {'answer_id': 72590, 'author': 'Eduardo Campañó', 'author_id': 12091, 'author_profile': 'https://Stackoverflow.com/users/12091', 'pm_score': 4, 'selected': True, 'text': '<p>Using code snippets you can get to something like this</p>\n\n<pre>\nDim randomNumGen As RandomNumberGenerator = RNGCryptoServiceProvider.Create()\nDim randomBytes(20) As Byte\nrandomNumGen.GetBytes(randomBytes)\nreturn Convert.ToBase64String(randomBytes)\n</pre>\n'}, {'answer_id': 72646, 'author': 'Erlend', 'author_id': 5746, 'author_profile': 'https://Stackoverflow.com/users/5746', 'pm_score': 0, 'selected': False, 'text': '<p>Basically the code above:</p>\n\n<ol>\n<li>Creates a secure random number generator (for VB see link below)</li>\n<li>Fills a bytearray of length 20 with random bytes</li>\n<li>Base64 encodes the result (you can probably use Convert.ToBase64String(...))</li>\n</ol>\n\n<p>You should find some help here:\n<a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx</a></p>\n'}]
['https://Stackoverflow.com/questions/72479', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12178/']