CryptographicBuffer的使用技巧

来源:互联网 发布:centos无法用ssh指令 编辑:程序博客网 时间:2024/05/20 12:48

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 = CryptographicBuffer.GenerateRandom(length);    // Encode the buffer to a hexadecimal string (for display).    string randomHex = CryptographicBuffer.EncodeToHexString(buffer);    return 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);};}public uint GenerateRandomNumber(){    // 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转换

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);}

4.byte数组的复制

public void ByteArrayCopy(){    // 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的压缩与解压

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);}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);}