Menu
- Powershell Generate Random Number
- Powershell Generate Random Aes Key Generator
- Powershell Generate Random Text
- Powershell Generate Random Filename
I am trying to generate machine key to share between a few machine, after a quick google search i found this article KB 2915218, Appendix A. I copied the code and save as.ps1 extension which i believe is power shell extension. An AES-128 expects a key of 128 bit, 16 byte. To generate such a key, use OpenSSL as: openssl rand 16 myaes.key AES-256 expects a key of 256 bit, 32 byte. To generate such a key, use OpenSSL as: openssl rand 16 myaes.key AES-256 expects a key of 256 bit, 32 byte. Apr 21, 2016 In the below example, I’m generating a random AES Key to use: To re-read the password, the following is done: In that scenario, the AES Key is your secret, and anyone who knows the AES Key can decrypt your password, so it’s not a great idea to simply embed the AES key into your script.
-->Creating and managing keys is an important part of the cryptographic process. Symmetric algorithms require the creation of a key and an initialization vector (IV). The key must be kept secret from anyone who should not decrypt your data. The IV does not have to be secret, but should be changed for each session. Asymmetric algorithms require the creation of a public key and a private key. The public key can be made public to anyone, while the private key must known only by the party who will decrypt the data encrypted with the public key. This section describes how to generate and manage keys for both symmetric and asymmetric algorithms.
Symmetric Keys
The symmetric encryption classes supplied by the .NET Framework require a key and a new initialization vector (IV) to encrypt and decrypt data. Whenever you create a new instance of one of the managed symmetric cryptographic classes using the parameterless constructor, a new key and IV are automatically created. Anyone that you allow to decrypt your data must possess the same key and IV and use the same algorithm. Generally, a new key and IV should be created for every session, and neither the key nor IV should be stored for use in a later session.
To communicate a symmetric key and IV to a remote party, you would usually encrypt the symmetric key by using asymmetric encryption. Sending the key across an insecure network without encrypting it is unsafe, because anyone who intercepts the key and IV can then decrypt your data. For more information about exchanging data by using encryption, see Creating a Cryptographic Scheme.
![Aes Aes](/uploads/1/2/6/4/126422390/174787093.png)
The following example shows the creation of a new instance of the TripleDESCryptoServiceProvider class that implements the TripleDES algorithm.
When the previous code is executed, a new key and IV are generated and placed in the Key and IV properties, respectively.
Sometimes you might need to generate multiple keys. In this situation, you can create a new instance of a class that implements a symmetric algorithm and then create a new key and IV by calling the GenerateKey and GenerateIV methods. The following code example illustrates how to create new keys and IVs after a new instance of the symmetric cryptographic class has been made.
When the previous code is executed, a key and IV are generated when the new instance of TripleDESCryptoServiceProvider is made. Another key and IV are created when the GenerateKey and GenerateIV methods are called.
Asymmetric Keys
The .NET Framework provides the RSACryptoServiceProvider and DSACryptoServiceProvider classes for asymmetric encryption. These classes create a public/private key pair when you use the parameterless constructor to create a new instance. Asymmetric keys can be either stored for use in multiple sessions or generated for one session only. While the public key can be made generally available, the private key should be closely guarded.
A public/private key pair is generated whenever a new instance of an asymmetric algorithm class is created. After a new instance of the class is created, the key information can be extracted using one of two methods:
- The ToXmlString method, which returns an XML representation of the key information.
- The ExportParameters method, which returns an RSAParameters structure that holds the key information.
Both methods accept a Boolean value that indicates whether to return only the public key information or to return both the public-key and the private-key information. An RSACryptoServiceProvider class can be initialized to the value of an RSAParameters structure by using the ImportParameters method.
Asymmetric private keys should never be stored verbatim or in plain text on the local computer. If you need to store a private key, you should use a key container. For more on how to store a private key in a key container, see How to: Store Asymmetric Keys in a Key Container.
The following code example creates a new instance of the RSACryptoServiceProvider class, creating a public/private key pair, and saves the public key information to an RSAParameters structure.
See also
--> Gets a random number, or selects objects randomly from a collection.
Syntax
Description
The
Get-Random
cmdlet gets a randomly selected number. If you submit a collection of objects toGet-Random
, it gets one or more randomly selected objects from the collection.Without parameters or input, a
Get-Random
command returns a randomly selected 32-bit unsignedinteger between 0 (zero) and Int32.MaxValue (0x7FFFFFFF
, 2,147,483,647
).You can use the parameters of
Get-Random
to specify a seed number, minimum and maximum values, andthe number of objects returned from a submitted collection.Examples
Example 1: Get a random integer
This command gets a random integer between 0 (zero) and Int32.MaxValue.
Example 2: Get a random integer between 0 and 99
Example 3: Get a random integer between -100 and 99
Example 4: Get a random floating-point number
This command gets a random floating-point number greater than or equal to 10.7 and less than 20.92.
Example 5: Get a random integer from an array
This command gets a randomly selected number from the specified array.
Example 6: Get several random integers from an array
This command gets three randomly selected numbers in random order from an array.
Example 7: Randomize an entire collection
This command returns the entire collection in random order.
The value of the Count parameter is the MaxValue static property of integers.
To return an entire collection in random order, enter any number that is greater than or equal tothe number of objects in the collection.
Example 8: Get a random non-numeric value
This command returns a random value from a non-numeric collection.
Example 9: Use the SetSeed parameter
This example shows the effect of using the SetSeed parameter.
Because SetSeed produces non-random behavior, it's typically used only to reproduce results,such as when debugging or analyzing a script.
Example 10: Get random files
These commands get a randomly selected sample of 50 files from the
C:
drive of the local computer.Example 11: Roll fair dice
This example rolls a fair die 1200 times and counts the outcomes. The first command,
For-EachObject
repeats the call to Get-Random
from the piped in numbers (1-6). The results are grouped by theirvalue with Group-Object
and formatted as a table with Select-Object
.Example 12: Use the Count parameter
You can now use the Count parameter without piping objects to
Get-Random
.The following example gets three random numbers less than 10.Example 13: Use the InputObject parameter with an empty string or $null
In this example, the InputObject parameter specifies an array that contains an empty string(
'
) and $null
.Get-Random
will return either a
, empty string, or $null
. The empty sting displays as a blankline and $null
returns to a PowerShell prompt.Parameters
Specifies the number of random objects or numbers to return. The default is 1.
When used with
InputObject
, if the value of Count exceeds the number of objects in thecollection, Get-Random
returns all of the objects in random order.Type: | Int32 |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies a collection of objects.
Get-Random
gets randomly selected objects in random order fromthe collection up to the number specified by Count. Enter the objects, a variable that containsthe objects, or a command or expression that gets the objects. You can also pipe a collection ofobjects to Get-Random
.Beginning in PowerShell 7, the InputObject parameter accepts arrays that can contain an emptystring or
$null
. The array can be sent down the pipeline or as an InputObject parameter value.Type: | Object[] |
Position: | 0 |
Default value: | None |
Accept pipeline input: | True (ByValue) |
Accept wildcard characters: | False |
Specifies a maximum value for the random number.
Get-Random
returns a value that is less than themaximum (not equal). Enter an integer, a double-precision floating-point number, or an object thatcan be converted to an integer or double, such as a numeric string ('100').The value of Maximum must be greater than (not equal to) the value of Minimum. If the valueof Maximum or Minimum is a floating-point number,
Get-Random
returns a randomly selectedfloating-point number.On a 64-bit computer, if the value of Minimum is a 32-bit integer, the default value ofMaximum is Int32.MaxValue.
If the value of Minimum is a double (a floating-point number), the default value of Maximumis Double.MaxValue. Otherwise, the default value is Int32.MaxValue.
Type: | Object |
Position: | 0 |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Powershell Generate Random Number
Specifies a minimum value for the random number. Enter an integer, a double-precision floating-pointnumber, or an object that can be converted to an integer or double, such as a numeric string('100'). The default value is 0 (zero).
The value of Minimum must be less than (not equal to) the value of Maximum. If the value ofMaximum or Minimum is a floating-point number,
Get-Random
returns a randomly selectedfloating-point number.Type: | Object |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies a seed value for the random number generator. This seed value is used for the currentcommand and for all subsequent
Get-Random
commands in the current session until you useSetSeed again or close the session. You can't reset the seed to its default value.The SetSeed parameter is not required. By default,
Get-Random
uses theRandomNumberGenerator()method to generate a seed value. Because SetSeed results in non-random behavior, it's typicallyused only when trying to reproduce behavior, such as when debugging or analyzing a script thatincludes Get-Random
commands.Type: | Int32 |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
You can pipe one or more objects.
Get-Random
selects values randomly from the piped objects.Outputs
System.Int32, System.Int64, System.Double
Get-Random
returns an integer or floating-point number, or an object selected randomly from asubmitted collection.Notes
Get-Random
sets a default seed for each session based on the system time clock when the sessionstarts.Get-Random
does not alway return the same data type as the input value. The following table showsthe output type for each of the numeric input types.Powershell Generate Random Aes Key Generator
Input Type | Output Type |
---|---|
SByte | Double |
Byte | Double |
Int16 | Double |
UInt16 | Double |
Int32 | Int32 |
UInt32 | Double |
Int64 | Int64 |
UInt64 | Double |
Double | Double |
Single | Double |
Powershell Generate Random Text
Beginning in Windows PowerShell 3.0,
Get-Random
supports 64-bit integers. In Windows PowerShell2.0, all values are cast to System.Int32.Powershell Generate Random Filename
Beginning in PowerShell 7, the InputObject parameter in the RandomListItemParameterSetparameter set accepts arrays that contain an empty string or
$null
. In earlier PowerShellversions, only the Maximum parameter in the RandomNumberParameterSet parameter set acceptedan empty string or $null
.