CryptographicBuffer CryptographicBuffer CryptographicBuffer CryptographicBuffer Class

Definition

Contains static methods that implement data management functionality common to cryptographic operations.

public : static class CryptographicBufferpublic static class CryptographicBufferPublic Static Class CryptographicBuffer// You can use this class in JavaScript.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

public void CryptographicBufferOverview()
{
    // 1. Generate random data.
    UInt32 buffLength = 32;
    IBuffer buffRnd = CryptographicBuffer.GenerateRandom(buffLength);
    String strRndData = CryptographicBuffer.EncodeToHexString(buffRnd);

    // 2. Generate a random number.
    UInt32 Rnd = CryptographicBuffer.GenerateRandomNumber();

    // 3. Decode a Base 64 string to a buffer and encode the buffer to a Base64 string.
    String strBase64v1 = "uiwyeroiugfyqcajkds897945234==";
    IBuffer buffFromBase64 = CryptographicBuffer.DecodeFromBase64String(strBase64v1);
    String strBase64v2 = CryptographicBuffer.EncodeToBase64String(buffFromBase64);

    // 4. Decode a hexadecimal string to a buffer and encode the buffer to a hexadecimal string.
    String strHexv1 = "30310AFF";
    IBuffer buffFromHex = CryptographicBuffer.DecodeFromHexString(strHexv1);
    String strHexv2 = CryptographicBuffer.EncodeToHexString(buffFromHex);

    // 5. Convert a strings to binary data and convert binary data to strings.
    String strIn = "Input String";
    IBuffer buffUTF16BE = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf16BE);
    String  strUTF16BE  = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf16BE, buffUTF16BE);
    IBuffer buffUTF16LE = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf16LE);
    String  strUTF16LE  = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf16LE, buffUTF16LE);
    IBuffer buffUTF8 = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf8);
    String  strUTF8  = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffUTF8);

    // 6. Create a buffer from a byte array and create a byte array from a buffer,
    byte[] arrByte = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    IBuffer buffFromByteArr = CryptographicBuffer.CreateFromByteArray(arrByte);
    byte[] arrByteNew;
    CryptographicBuffer.CopyToByteArray(buffFromByteArr, out arrByteNew);

    // 7. Compare two buffers. The following Boolean value is true because the code points are equal.
    String strHex = "30310aff";
    String strBase64 = "MDEK/w==";
    IBuffer buff1 = CryptographicBuffer.DecodeFromHexString(strHex);
    IBuffer buff2 = CryptographicBuffer.DecodeFromBase64String(strBase64);
    Boolean bVal = CryptographicBuffer.Compare(buff1, buff2);

}

Methods

Compare(IBuffer, IBuffer) Compare(IBuffer, IBuffer) Compare(IBuffer, IBuffer) Compare(IBuffer, IBuffer)

Compares two IBuffer objects.

public : static PlatForm::Boolean Compare(IBuffer object1, IBuffer object2)public static bool Compare(IBuffer object1, IBuffer object2)Public Static Function Compare(object1 As IBuffer, object2 As IBuffer) As bool// You can use this method in JavaScript.
Parameters
object1
IBuffer IBuffer IBuffer IBuffer

Buffer to be used for comparison.

object2
IBuffer IBuffer IBuffer IBuffer

Buffer to be used for comparison.

Returns
PlatForm::Boolean bool bool bool

True specifies that the buffers are equal. Two buffers are equal if each code point in one matches the corresponding code point in the other.

Examples


public void CompareBuffers()
{
    // Create a hexadecimal string.
    String strHex = "30310aff";

    // Create a Base64 string that is equivalent to strHex.
    String strBase64v1 = "MDEK/w==";

    // Create a Base64 string that is not equivalent to strHex.
    String strBase64v2 = "KEDM/w==";

    // Decode strHex to a buffer.
    IBuffer buff1 = CryptographicBuffer.DecodeFromHexString(strHex);

    // Decode strBase64v1 to a buffer.
    IBuffer buff2 = CryptographicBuffer.DecodeFromBase64String(strBase64v1);

    // Decode strBase64v2 to a buffer.
    IBuffer buff3 = CryptographicBuffer.DecodeFromBase64String(strBase64v2);

    // Compare the hexadecimal-decoded buff1 to the Base64-decoded buff2.
    // The code points in the two buffers are equal, and the Boolean value
    // is true.
    Boolean bVal_1 = CryptographicBuffer.Compare(buff1, buff2);

    // Compare the hexadecimal-decoded buff1 to the Base64-decoded buff3.
    // The code points in the two buffers are not equal, and the Boolean value
    // is false.
    Boolean bVal_2 = CryptographicBuffer.Compare(buff1, buff3);
}

ConvertBinaryToString(BinaryStringEncoding, IBuffer) ConvertBinaryToString(BinaryStringEncoding, IBuffer) ConvertBinaryToString(BinaryStringEncoding, IBuffer) ConvertBinaryToString(BinaryStringEncoding, IBuffer)

Converts a buffer to an encoded string.

public : static PlatForm::String ConvertBinaryToString(BinaryStringEncoding encoding, IBuffer buffer)public static string ConvertBinaryToString(BinaryStringEncoding encoding, IBuffer buffer)Public Static Function ConvertBinaryToString(encoding As BinaryStringEncoding, buffer As IBuffer) As string// You can use this method in JavaScript.
Parameters
buffer
IBuffer IBuffer IBuffer IBuffer

Data to be encoded.

Returns
PlatForm::String string string string

A string that contains the encoded data.

Examples

public void ConvertData()
{
    // Create a string to convert.
    String strIn = "Input String";

    // Convert the string to UTF16BE binary data.
    IBuffer buffUTF16BE = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf16BE);

    // Convert the string to UTF16LE binary data.
    IBuffer buffUTF16LE = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf16LE);

    // Convert the string to UTF8 binary data.
    IBuffer buffUTF8 = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf8);
}

ConvertStringToBinary(String, BinaryStringEncoding) ConvertStringToBinary(String, BinaryStringEncoding) ConvertStringToBinary(String, BinaryStringEncoding) ConvertStringToBinary(String, BinaryStringEncoding)

Converts a string to an encoded buffer.

public : static IBuffer ConvertStringToBinary(PlatForm::String value, BinaryStringEncoding encoding)public static IBuffer ConvertStringToBinary(String value, BinaryStringEncoding encoding)Public Static Function ConvertStringToBinary(value As String, encoding As BinaryStringEncoding) As IBuffer// You can use this method in JavaScript.
Parameters
value
PlatForm::String String String String

String to be encoded.

Returns

Encoded buffer.

Examples

public void ConvertData()
{
    // Create a string to convert.
    String strIn = "Input String";

    // Convert the string to UTF16BE binary data.
    IBuffer buffUTF16BE = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf16BE);

    // Convert the string to UTF16LE binary data.
    IBuffer buffUTF16LE = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf16LE);

    // Convert the string to UTF8 binary data.
    IBuffer buffUTF8 = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf8);
}

CopyToByteArray(IBuffer, Byte[]) CopyToByteArray(IBuffer, Byte[]) CopyToByteArray(IBuffer, Byte[]) CopyToByteArray(IBuffer, Byte[])

Copies a buffer to an array of bytes.

public : static void CopyToByteArray(IBuffer buffer, Byte[] value)public static void CopyToByteArray(IBuffer buffer, Byte[] value)Public Static Function CopyToByteArray(buffer As IBuffer, value As Byte[]) As void// You can use this method in JavaScript.
Parameters
buffer
IBuffer IBuffer IBuffer IBuffer

Input buffer.

value
Byte[] Byte[] Byte[] Byte[]

An array of bytes that contains the values copied from the input buffer. You must declare the array before calling this method and pass it by using the ref keyword. If the buffer for the input parameter is empty, then the value parameter will be returned as NULL.

Examples

public void ByteArrayCopy()
{
    // Initialize a byte array.
    byte[] arrByte = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    // Create a buffer from the byte array.
    IBuffer buffer = CryptographicBuffer.CreateFromByteArray(arrByte);

    // Encode the buffer into a hexadecimal string (for display);
    String strHex = CryptographicBuffer.EncodeToHexString(buffer);

    // Copy the buffer back into a new byte array.
    byte[] arrByteNew;
    CryptographicBuffer.CopyToByteArray(buffer, out arrByteNew);
}

CreateFromByteArray(Byte[]) CreateFromByteArray(Byte[]) CreateFromByteArray(Byte[]) CreateFromByteArray(Byte[])

Creates a buffer from an input byte array.

public : static IBuffer CreateFromByteArray(Byte[] value)public static IBuffer CreateFromByteArray(Byte[] value)Public Static Function CreateFromByteArray(value As Byte[]) As IBuffer// You can use this method in JavaScript.
Parameters
value
Byte[] Byte[] Byte[] Byte[]

An array of bytes used to create the buffer.

Returns

Output buffer.

Examples

public void ByteArrayCopy()
{
    // Initialize a byte array.
    byte[] arrByte = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    // Create a buffer from the byte array.
    IBuffer buffer = CryptographicBuffer.CreateFromByteArray(arrByte);

    // Encode the buffer into a hexadecimal string (for display);
    String strHex = CryptographicBuffer.EncodeToHexString(buffer);

    // Copy the buffer back into a new byte array.
    byte[] arrByteNew;
    CryptographicBuffer.CopyToByteArray(buffer, out arrByteNew);
}

DecodeFromBase64String(String) DecodeFromBase64String(String) DecodeFromBase64String(String) DecodeFromBase64String(String)

Decodes a string that has been base64 encoded.

public : static IBuffer DecodeFromBase64String(PlatForm::String value)public static IBuffer DecodeFromBase64String(String value)Public Static Function DecodeFromBase64String(value As String) As IBuffer// You can use this method in JavaScript.
Parameters
value
PlatForm::String String String String

Base64 encoded input string.

Returns

Output buffer that contains the decoded string.

Examples

public void EncodeDecodeBase64()
{
    // Define a Base64 encoded string.
    String strBase64 = "uiwyeroiugfyqcajkds897945234==";

    // Decoded the string from Base64 to binary.
    IBuffer buffer = CryptographicBuffer.DecodeFromBase64String(strBase64);

    // Encode the buffer back into a Base64 string.
    String strBase64New = CryptographicBuffer.EncodeToBase64String(buffer);
}

Remarks

Base64 processes data as 24-bit groups, mapping each group to four encoded 8-bit characters. Base64 encoding is sometimes referred to as 3-to-4 encoding. Each 6 bits of the 24-bit group is used as an index into a mapping table (the base64 alphabet) to obtain a character for the encoded data.

DecodeFromHexString(String) DecodeFromHexString(String) DecodeFromHexString(String) DecodeFromHexString(String)

Decodes a string that has been hexadecimal encoded.

public : static IBuffer DecodeFromHexString(PlatForm::String value)public static IBuffer DecodeFromHexString(String value)Public Static Function DecodeFromHexString(value As String) As IBuffer// You can use this method in JavaScript.
Parameters
value
PlatForm::String String String String

Encoded input string.

Returns

Output buffer that contains the decoded string.

Examples

public void EncodeDecodeHex()
{
    // Define a hexadecimal string.
    String strHex = "30310AFF";

    // Decode a hexadecimal string to binary.
    IBuffer buffer = CryptographicBuffer.DecodeFromHexString(strHex);

    // Encode the buffer back into a hexadecimal string.
    String strHexNew = CryptographicBuffer.EncodeToHexString(buffer);
}

EncodeToBase64String(IBuffer) EncodeToBase64String(IBuffer) EncodeToBase64String(IBuffer) EncodeToBase64String(IBuffer)

Encodes a buffer to a base64 string.

public : static PlatForm::String EncodeToBase64String(IBuffer buffer)public static string EncodeToBase64String(IBuffer buffer)Public Static Function EncodeToBase64String(buffer As IBuffer) As string// You can use this method in JavaScript.
Parameters
buffer
IBuffer IBuffer IBuffer IBuffer

Input buffer.

Returns
PlatForm::String string string string

Base64-encoded output string.

Examples

public void EncodeDecodeBase64()
{
    // Define a Base64 encoded string.
    String strBase64 = "uiwyeroiugfyqcajkds897945234==";

    // Decoded the string from Base64 to binary.
    IBuffer buffer = CryptographicBuffer.DecodeFromBase64String(strBase64);

    // Encode the buffer back into a Base64 string.
    String strBase64New = CryptographicBuffer.EncodeToBase64String(buffer);
}

Remarks

Base64 processes data as 24-bit groups, mapping each group to four encoded 8-bit characters. Base64 encoding is sometimes referred to as 3-to-4 encoding. Each 6 bits of the 24-bit group is used as an index into a mapping table (the base64 alphabet) to obtain a character for the encoded data.

EncodeToHexString(IBuffer) EncodeToHexString(IBuffer) EncodeToHexString(IBuffer) EncodeToHexString(IBuffer)

Encodes a buffer to a hexadecimal string.

public : static PlatForm::String EncodeToHexString(IBuffer buffer)public static string EncodeToHexString(IBuffer buffer)Public Static Function EncodeToHexString(buffer As IBuffer) As string// You can use this method in JavaScript.
Parameters
buffer
IBuffer IBuffer IBuffer IBuffer

Input buffer.

Returns
PlatForm::String string string string

Hexadecimal encoded output string.

Examples

public void EncodeDecodeHex()
{
    // Define a hexadecimal string.
    String strHex = "30310AFF";

    // Decode a hexadecimal string to binary.
    IBuffer buffer = CryptographicBuffer.DecodeFromHexString(strHex);

    // Encode the buffer back into a hexadecimal string.
    String strHexNew = CryptographicBuffer.EncodeToHexString(buffer);
}

GenerateRandom(UInt32) GenerateRandom(UInt32) GenerateRandom(UInt32) GenerateRandom(UInt32)

Creates a buffer that contains random data.

public : static IBuffer GenerateRandom(unsigned int length)public static IBuffer GenerateRandom(UInt32 length)Public Static Function GenerateRandom(length As UInt32) As IBuffer// You can use this method in JavaScript.
Parameters
length
unsigned int UInt32 UInt32 UInt32

Length, in bytes, of the buffer to create.

Returns

Output buffer that contains the random data.

Examples


using Windows.Security.Cryptography;
using Windows.Storage.Streams;

namespace Random
{
    sealed partial class GenerateRandomDataApp : Application
    {
        public GenerateRandomDataApp()
        {
            // Initialize the application.
            this.InitializeComponent();

            // Create a buffer that contains random data.
            String strRndHex = this.GenerateRndData();

            // Create a random integer.
            UInt32 uRnd = this.GenerateRndNumber();
        }

        public String GenerateRndData()
        {
            // Define the length, in bytes, of the buffer.
            UInt32 length = 32;

            // Generate random data and copy it to a buffer.
            IBuffer buffer = CryptographicBuffer.GenerateRandom(length);

            // Encode the buffer to a hexadecimal string (for display).
            String hexRnd = CryptographicBuffer.EncodeToHexString(buffer);

            return hexRnd;
        }

        public UInt32 GenerateRndNumber()
        {
            // Generate a random number.
            UInt32 Rnd = CryptographicBuffer.GenerateRandomNumber();

            return Rnd;
        }
    }
}

GenerateRandomNumber() GenerateRandomNumber() GenerateRandomNumber() GenerateRandomNumber()

Creates a random number.

public : static unsigned int GenerateRandomNumber()public static uint GenerateRandomNumber()Public Static Function GenerateRandomNumber() As uint// You can use this method in JavaScript.
Returns
unsigned int uint uint uint

Integer that contains the random data.

Examples


using Windows.Security.Cryptography;
using Windows.Storage.Streams;

namespace Random
{
    sealed partial class GenerateRandomDataApp : Application
    {
        public GenerateRandomDataApp()
        {
            // Initialize the application.
            this.InitializeComponent();

            // Create a buffer that contains random data.
            String strRndHex = this.GenerateRndData();

            // Create a random integer.
            UInt32 uRnd = this.GenerateRndNumber();
        }

        public String GenerateRndData()
        {
            // Define the length, in bytes, of the buffer.
            UInt32 length = 32;

            // Generate random data and copy it to a buffer.
            IBuffer buffer = CryptographicBuffer.GenerateRandom(length);

            // Encode the buffer to a hexadecimal string (for display).
            String hexRnd = CryptographicBuffer.EncodeToHexString(buffer);

            return hexRnd;
        }

        public UInt32 GenerateRndNumber()
        {
            // Generate a random number.
            UInt32 Rnd = CryptographicBuffer.GenerateRandomNumber();

            return Rnd;
        }
    }
}