publicstringGenerateRandomData()
{
// Define the length, in bytes, of the buffer.uint 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 randomHex = CryptographicBuffer.EncodeToHexString(buffer);
returnpublicvoidCompareBuffers()
{
// 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);
};
}
publicuintGenerateRandomNumber()
{
// Generate a random number.uint random = CryptographicBuffer.GenerateRandomNumber();
return random;
}
2.比较缓存
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);
}
3.strings 和 binary转换
publicvoidConvertData()
{
// 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);
}
4.byte数组的复制
publicvoidByteArrayCopy()
{
// Initialize a byte array.byte[] bytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Create a buffer from the byte array.
IBuffer buffer = CryptographicBuffer.CreateFromByteArray(bytes);
// Encode the buffer into a hexadecimal string (for display);string hex = CryptographicBuffer.EncodeToHexString(buffer);
// Copy the buffer back into a new byte array.byte[] newByteArray;
CryptographicBuffer.CopyToByteArray(buffer, out newByteArray);
}
5.base64的压缩与解压
publicvoidEncodeDecodeBase64()
{
// 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);
}
publicvoidEncodeDecodeHex()
{
// 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);
}
1.创建随机数public string GenerateRandomData(){ // Define the length, in bytes, of the buffer. uint length = 32; // Generate random data and copy it to a buffer. IBuffer buffer = Cryptograph