본문 바로가기

카테고리 없음

A A Randomly Generated 256-bit Binary Key



WARNING: do not actually execute this. You will not be able to recover the files. This is intended to encrypt every bit of data in a unix filesystem using 256-bit AES with a self-destructing, randomly generated key. Jan 02, 2017 For every Bitcoin address you create, a private key is randomly generated by the Bitcoin software ( the private key is simply a random 256 bit number ). Then a mathematical process is used to create a public key from the private key. Together these two pieces of data form a cryptographic key pair.

Encrypt, decrypt and generate a key in C# using AES256.
encryption.cs
#regionEncryption
/// <summary>
/// Generate a private key
/// From : www.chapleau.info/blog/2011/01/06/usingsimplestringkeywithaes256encryptioninc.html
/// </summary>
privatestaticstringGenerateKey(intiKeySize)
{
RijndaelManagedaesEncryption=newRijndaelManaged();
aesEncryption.KeySize=iKeySize;
aesEncryption.BlockSize=128;
aesEncryption.Mode=CipherMode.CBC;
aesEncryption.Padding=PaddingMode.PKCS7;
aesEncryption.GenerateIV();
stringivStr=Convert.ToBase64String(aesEncryption.IV);
aesEncryption.GenerateKey();
stringkeyStr=Convert.ToBase64String(aesEncryption.Key);
stringcompleteKey=ivStr+','+keyStr;
returnConvert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(completeKey));
}
/// <summary>
/// Encrypt
/// From : www.chapleau.info/blog/2011/01/06/usingsimplestringkeywithaes256encryptioninc.html
/// </summary>
privatestaticstringEncrypt(stringiPlainStr, stringiCompleteEncodedKey, intiKeySize)
{
RijndaelManagedaesEncryption=newRijndaelManaged();
aesEncryption.KeySize=iKeySize;
aesEncryption.BlockSize=128;
aesEncryption.Mode=CipherMode.CBC;
aesEncryption.Padding=PaddingMode.PKCS7;
aesEncryption.IV=Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(iCompleteEncodedKey)).Split(',')[0]);
aesEncryption.Key=Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(iCompleteEncodedKey)).Split(',')[1]);
byte[] plainText=ASCIIEncoding.UTF8.GetBytes(iPlainStr);
ICryptoTransformcrypto=aesEncryption.CreateEncryptor();
byte[] cipherText=crypto.TransformFinalBlock(plainText, 0, plainText.Length);
returnConvert.ToBase64String(cipherText);
}
/// <summary>
/// Decrypt
/// From : www.chapleau.info/blog/2011/01/06/usingsimplestringkeywithaes256encryptioninc.html
/// </summary>
privatestaticstringDecrypt(stringiEncryptedText, stringiCompleteEncodedKey, intiKeySize)
{
RijndaelManagedaesEncryption=newRijndaelManaged();
aesEncryption.KeySize=iKeySize;
aesEncryption.BlockSize=128;
aesEncryption.Mode=CipherMode.CBC;
aesEncryption.Padding=PaddingMode.PKCS7;
aesEncryption.IV=Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(iCompleteEncodedKey)).Split(',')[0]);
aesEncryption.Key=Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(iCompleteEncodedKey)).Split(',')[1]);
ICryptoTransformdecrypto=aesEncryption.CreateDecryptor();
byte[] encryptedBytes=Convert.FromBase64CharArray(iEncryptedText.ToCharArray(), 0, iEncryptedText.Length);
returnASCIIEncoding.UTF8.GetString(decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length));
}
#endregion

commented Jun 6, 2014

hi fairly new to the cryptography..
when im implementing the above code im getting a error while decrypting.
'Padding is invalid and cannot be removed.'

please suggest a resolution
thanks and regards

commented Oct 9, 2017
edited

How-to save -safely- the private key ? Windows registry ? in disk ?

I use ASP.NET applications.

Test your code

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

[ aws . kms ]

Randomly

Description¶

Returns a random byte string that is cryptographically secure.

By default, the random byte string is generated in AWS KMS. To generate the byte string in the AWS CloudHSM cluster that is associated with a custom key store , specify the custom key store ID.

For more information about entropy and random number generation, see the AWS Key Management Service Cryptographic Details whitepaper.

See also: AWS API Documentation

See 'aws help' for descriptions of global parameters.

Synopsis¶

Options¶

--number-of-bytes (integer)

--custom-key-store-id (string)

Generates the random byte string in the AWS CloudHSM cluster that is associated with the specified custom key store . To find the ID of a custom key store, use the DescribeCustomKeyStores operation.

--cli-input-json (string)Performs service operation based on the JSON string provided. The JSON string follows the format provided by --generate-cli-skeleton. If other arguments are provided on the command line, the CLI values will override the JSON-provided values. It is not possible to pass arbitrary binary values using a JSON-provided value as the string will be taken literally.

--generate-cli-skeleton (string)Prints a JSON skeleton to standard output without sending an API request. If provided with no value or the value input, prints a sample input JSON that can be used as an argument for --cli-input-json. If provided with the value output, it validates the command inputs and returns a sample output JSON for that command.

See 'aws help' for descriptions of global parameters.

Examples¶

Example 1: To generate a 256-bit random number

A A Randomly Generated 256-bit Binary Key Sheet

The following generate-random example generates a 256-bit (32-byte) random number.

When you run this command, you must use the number-of-bytes parameter to specify the length of the random number in bytes.

You don't specify a CMK when you run this command. Unless you specify a custom key store, AWS KMS generates the random number. It is not associated with any particular CMK.

In the output, the random number is in the Plaintext field.

Example 2: To generate a 256-bit random number and save it to a file (Linux or macOs)

The following example uses the generate-random command to generate a 256-bit (32-byte), base64-encoded random byte string on a Linix or macOS computer. The example decodes the byte string and saves it in the ExampleRandom file.

When you run this command, you must use the number-of-bytes parameter to specify the length of the random number in bytes.

You don't specify a CMK when you run this command. Unless you specify a custom key store, AWS KMS generates the random number. It is not associated with any particular CMK.

Of the is a good, freely available source. Hmac sha256 secret key generator java.

A a randomly generated 256-bit binary keyboard
  • The --number-of-bytes parameter with a value of 32 requests a 32-byte (256-bit) string.

  • The --output parameter with a value of text directs the AWS CLI to return the output as text, instead of JSON.

  • https://flucinfoschi.tistory.com/1. The --query parameter extracts the value of the Plaintext property from the response.

  • The pipe operator ( | ) sends the output of the command to the base64 utility, which decodes the extracted output.

  • The redirection operator (>) saves the decoded byte string to the ExampleRandom file.

    https://flucinfoschi.tistory.com/2. aws kms generate-random --number-of-bytes 32 --output text --query Plaintext | base64 --decode > ExampleRandom

This command produces no output.

Random 256 bit key generator

Example 3: To generate a 256-bit random number and save it to a file(Windows Command Prompt)

The following example uses the generate-random command to generate a 256-bit (32-byte), base64-encoded random byte string. The example decodes the byte string and saves it in the ExampleRandom.base64 file.

This example is the same as the previous example, except that it uses the certutil utility in Windows to base64-decode the random byte string before saving it in a file.

The first command generates the base64-encoded random byte string and saves it in a temporary file, ExampleRandom.base64. The second command uses the certutil-decode command to decode the base64-encoded byte string in the ExampleRandom.base64 file. Then, it saves the decoded byte string in the ExampleRandom file.

Output:

Openssl Generate 256 Bit Key

For more information, see GenerateRandom in the AWS Key Management Service API Reference.

Output¶

Plaintext -> (blob)

A A Randomly Generated 256-bit Binary Keyboard

The random byte string. When you use the HTTP API or the AWS CLI, the value is Base64-encoded. Otherwise, it is not Base64-encoded.