CryptographicBuffer CryptographicBuffer CryptographicBuffer CryptographicBuffer CryptographicBuffer Class

Definition

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

public : static class CryptographicBuffer
static struct winrt::Windows::Security::Cryptography::CryptographicBuffer
public static class CryptographicBuffer
Public Shared Class CryptographicBuffer
// This class does not provide a public constructor.
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) Compare(IBuffer, IBuffer)

Compares two IBuffer objects.

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

Converts a buffer to an encoded string.

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

Converts a string to an encoded buffer.

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

Copies a buffer to an array of bytes.

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

Creates a buffer from an input byte array.

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

Decodes a string that has been base64 encoded.

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

Decodes a string that has been hexadecimal encoded.

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

Encodes a buffer to a base64 string.

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

Encodes a buffer to a hexadecimal string.

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

Creates a buffer that contains random data.

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

Creates a random number.