java代码的等效vb代码

时间:2022-09-01 08:08:06

Can anyone tell me what exactly does this Java code do?

谁能告诉我这个Java代码到底做了什么?

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

return Base64.encode(bytes);

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

一步一步的解释将是有用的,以便我可以在VB中重新创建此代码。谢谢

5 个解决方案

#1


5  

Using code snippets you can get to something like this

使用代码片段,你可以得到类似的东西

Dim randomNumGen As RandomNumberGenerator = RNGCryptoServiceProvider.Create()
Dim randomBytes(20) As Byte
randomNumGen.GetBytes(randomBytes)
return Convert.ToBase64String(randomBytes)

#2


3  

This creates a random number generator (SecureRandom). It then creates a byte array (byte[] bytes), length 20 bytes, and populates it with random data.

这将创建一个随机数生成器(SecureRandom)。然后它创建一个字节数组(byte []字节),长度为20个字节,并用随机数据填充它。

This is then encoded using BASE64 and returned.

然后使用BASE64对其进行编码并返回。

So, in a nutshell,

所以,简而言之,

  1. Generate 20 random bytes
  2. 生成20个随机字节

  3. Encode using Base 64
  4. 使用Base 64进行编码

#3


1  

It creates a SHA1 based random number generator (RNG), then Base64 encodes the next 20 bytes returned by the RNG.

它创建一个基于SHA1的随机数生成器(RNG),然后Base64对RNG返回的下一个20个字节进行编码。

I can't tell you why it does this however without some more context :-).

我不能告诉你它为什么这样做但是没有更多的背景:-)。

#4


1  

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.

此代码获得一个长度为20字节的加密强随机数,然后Base64对其进行编码。这里有很多Java库代码,所以你的猜测和我的猜测一样好。

SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
byte[] bytes = new byte[20];
synchronized (random) { random.nextBytes(bytes); }
return Base64.encode(bytes);

The first line creates an instance of the SecureRandom class. This class provides a cryptographically strong pseudo-random number generator.

第一行创建SecureRandom类的实例。该类提供了加密强的伪随机数生成器。

The second line declares a byte array of length 20.

第二行声明一个长度为20的字节数组。

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.

第三行将接下来的20个随机字节读入第2行创建的数组中。它在SecureRandom对象上进行同步,以便不会与可能正在使用该对象的其他线程发生冲突。从这段代码中可以看出为什么你需要这样做。

The fourth line Base64 encodes the resulting byte array. This is probably for transmission, storage, or display in a known format.

第四行Base64对生成的字节数组进行编码。这可能用于以已知格式传输,存储或显示。

#5


0  

Basically the code above:

基本上上面的代码:

  1. Creates a secure random number generator (for VB see link below)
  2. 创建一个安全的随机数生成器(对于VB,请参阅下面的链接)

  3. Fills a bytearray of length 20 with random bytes
  4. 用随机字节填充长度为20的bytearray

  5. Base64 encodes the result (you can probably use Convert.ToBase64String(...))
  6. Base64对结果进行编码(你可以使用Convert.ToBase64String(...))

You should find some help here: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx

你应该在这里找到一些帮助:http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx

#1


5  

Using code snippets you can get to something like this

使用代码片段,你可以得到类似的东西

Dim randomNumGen As RandomNumberGenerator = RNGCryptoServiceProvider.Create()
Dim randomBytes(20) As Byte
randomNumGen.GetBytes(randomBytes)
return Convert.ToBase64String(randomBytes)

#2


3  

This creates a random number generator (SecureRandom). It then creates a byte array (byte[] bytes), length 20 bytes, and populates it with random data.

这将创建一个随机数生成器(SecureRandom)。然后它创建一个字节数组(byte []字节),长度为20个字节,并用随机数据填充它。

This is then encoded using BASE64 and returned.

然后使用BASE64对其进行编码并返回。

So, in a nutshell,

所以,简而言之,

  1. Generate 20 random bytes
  2. 生成20个随机字节

  3. Encode using Base 64
  4. 使用Base 64进行编码

#3


1  

It creates a SHA1 based random number generator (RNG), then Base64 encodes the next 20 bytes returned by the RNG.

它创建一个基于SHA1的随机数生成器(RNG),然后Base64对RNG返回的下一个20个字节进行编码。

I can't tell you why it does this however without some more context :-).

我不能告诉你它为什么这样做但是没有更多的背景:-)。

#4


1  

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.

此代码获得一个长度为20字节的加密强随机数,然后Base64对其进行编码。这里有很多Java库代码,所以你的猜测和我的猜测一样好。

SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
byte[] bytes = new byte[20];
synchronized (random) { random.nextBytes(bytes); }
return Base64.encode(bytes);

The first line creates an instance of the SecureRandom class. This class provides a cryptographically strong pseudo-random number generator.

第一行创建SecureRandom类的实例。该类提供了加密强的伪随机数生成器。

The second line declares a byte array of length 20.

第二行声明一个长度为20的字节数组。

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.

第三行将接下来的20个随机字节读入第2行创建的数组中。它在SecureRandom对象上进行同步,以便不会与可能正在使用该对象的其他线程发生冲突。从这段代码中可以看出为什么你需要这样做。

The fourth line Base64 encodes the resulting byte array. This is probably for transmission, storage, or display in a known format.

第四行Base64对生成的字节数组进行编码。这可能用于以已知格式传输,存储或显示。

#5


0  

Basically the code above:

基本上上面的代码:

  1. Creates a secure random number generator (for VB see link below)
  2. 创建一个安全的随机数生成器(对于VB,请参阅下面的链接)

  3. Fills a bytearray of length 20 with random bytes
  4. 用随机字节填充长度为20的bytearray

  5. Base64 encodes the result (you can probably use Convert.ToBase64String(...))
  6. Base64对结果进行编码(你可以使用Convert.ToBase64String(...))

You should find some help here: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx

你应该在这里找到一些帮助:http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx