Windows 8 series (iv): Win8 RSA encryption Problems

Source: Internet
Author: User

I haven't published Win8 series of articles for a long time. I have accumulated some experience and problems during this period of time. I 'd like to share it with you.

 

It is inconvenient to use RSA encryption algorithms in win8. Unlike other platforms that import certificate files or N and E values, you need to import ASC code data. It is said that the same is true for Windows Phone. You need to parse the information in the key into an ASC code to form an array and use asyuncrickeyalgorithmprovider. importpublickey () to import the data. The related code is as follows: (nbytes is the ASC code data, type: byte [])

 

 

In Windows Phone, you can directly import the array generated by the key, but Win8 always prompts a failure, compilation is not successful, and ASN1 bad tag value met .. For example:

 

 

As Win8 currently has few references and few developers have encountered these problems, my colleagues can only refer to examples of RSA Data packets on other platforms, after searching in the last day, I finally passed the test. The solution is to add "82, 83, 65, 49, 0, 4" before the original key ASC code array, 0, 0, 3, 0, 0, 0,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, "This ASC code value, after converting this string of ASC code into characters is

This is equivalent to an information header.

In the face of the problem of no data tracing, it is not easy for us to solve this problem. If there is no such case, we do not know when to solve this problem.Although this problem has been solved for the time being, we have not found a fundamental solution to this problem. If this problem occurs again in the future, we do not know when to "try" it.

 

After all, I was worried that an error was reported after the latest Win8 system upgrade package was installed. At first, we thought it was caused by API changes. However, after reading the API documentation, we did not find a solution. Finally, we asked Microsoft for help and wanted to know that it was a system problem, it's still the API problem, or our own problem.

We once again spent nearly two days on this issue. Finally, with the help of @ Ma vegetables (Sina Weibo), we were on the right path. He found that there was a createkeypair method in the API, you can create keys of the specified length. Therefore, we use this method to create two keys at the same time, compare their similarities, and add them to our key array. Isn't it OK?

 

So we use the following code:

1             CryptographicKey keyPair = Algorithm.CreateKeyPair(1024);2             CryptographicKey keyPair2 = Algorithm.CreateKeyPair(1024);3             var blobOfPublicKey = keyPair.ExportPublicKey();4             var blobOfPublicKey2 = keyPair2.ExportPublicKey();5             byte[] newBuffer, newBuffer2;6             CryptographicBuffer.CopyToByteArray(blobOfPublicKey, out newBuffer);7             CryptographicBuffer.CopyToByteArray(blobOfPublicKey2, out newBuffer2);

Here, we need to explain why we have created 1024 instead of other lengths, we found that the length of our key after being converted to an ibuffer object is 162, and the length after converting the key of 128 to an ibuffer object is, which is close, therefore, there should be 38 "headers" and "tails ".

After viewing the differences between newbuffer and newbuffer2, we found that some array elements are the same in the header and tail. We add them to our key array and run the importpublickey () method again, execution passed. At this point, our RSA encryption problem is finally solved, and it is completely solved. If an error is reported later, we can take the same part of the above Code, get the "Header" and "tail ".

 

The above are the problems we encountered in our work and related solutions, and we hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.