Read string of numbers into a grid/matrix [closed] - c#
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can anyone tell me how can I read this grid into arrays like a[i][j] ? I searched on google but I can't seem to find anything useful.Thank you very much for helping!
static void Main(string[] args)
{
String grid = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08" +
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00" +
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65" +
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91" +
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80" +
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50" +
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70" +
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21" +
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72" +
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95" +
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92" +
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57" +
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58" +
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40" +
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66" +
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69" +
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36" +
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16" +
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54" +
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
int[] a = new int[20];
for(int i=0;i<20;i++)
for (int j = 1; j < 20; j++)
{
}
}
As suggested in the comments you can simply separate your numbers and use split string. for example:
private static void Main(string[] args)
{
String grid = "08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08," +
"49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00," +
"81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65," +
"52,70,95,23,04,60,11,42,69,24,68,56,01,32,56,71,37,02,36,91," +
"22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80," +
"24,47,32,60,99,03,45,02,44,75,33,53,78,36,84,20,35,17,12,50," +
"32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70," +
"67,26,20,68,02,62,12,20,95,63,94,39,63,08,40,91,66,49,94,21," +
"24,55,58,05,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72," +
"21,36,23,09,75,00,76,44,20,45,35,14,00,61,33,97,34,31,33,95," +
"78,17,53,28,22,75,31,67,15,94,03,80,04,62,16,14,09,53,56,92," +
"16,39,05,42,96,35,31,47,55,58,88,24,00,17,54,24,36,29,85,57," +
"86,56,00,48,35,71,89,07,05,44,44,37,44,60,21,58,51,54,17,58," +
"19,80,81,68,05,94,47,69,28,73,92,13,86,52,17,77,04,89,55,40," +
"04,52,08,83,97,35,99,16,07,97,57,32,16,26,26,79,33,27,98,66," +
"88,36,68,87,57,62,20,72,03,46,33,67,46,55,12,32,63,93,53,69," +
"04,42,16,73,38,25,39,11,24,94,72,18,08,46,29,32,40,62,76,36," +
"20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,04,36,16," +
"20,73,35,29,78,31,90,01,74,31,49,71,48,86,81,16,23,57,05,54," +
"01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,01,89,19,67,48";
var splitstring = grid.Split(',');
var a = new int[20,20];
const int rowCount = 19; //counts 0 as 1
var rowIndex = 0;
var colIndex = 0;
foreach (var s in splitstring)
{
if (rowIndex > rowCount)
{
rowIndex = 0;
colIndex++;
}
a[colIndex, rowIndex] = Int32.Parse(s);
rowIndex++;
}
}
Note the Int32.Parse(s) will throw an exception if the parse fails. You can instead use an Int32.TryParse and use the out value for your result. Depends on what you want to do.
Consider add a space to the end of each "line" such as the following:
String grid = "08 02 .. 91 08 " +
"01 70 .. 67 48 ";
// ^-- add space here
This will allow the string to be converted trivially into a 1D array of strings with string.Split.
string grid = "08 02 .. 91 08"; // every number is space-separated now
string[] gridArray = grid.Split(" "); // -> ["08", "02", .. "91", "08"]
(Even without ensuring the extra spaces, a 1D array can be achieved using a Regular Expression split: var gridArray = Regex.Split(grid, "(?:\s|(?<=\d{2})(?=\d{2}))"), but I recommend "normalizing" the input string literals if possible.)
And each index in the resulting 1D array can be accessed as so, where columns represents the number of columns of the super-imposed matrix, or the "width" of each row.
int columns = 20;
int gridIndex = j * columns + i; // where j is a *row* and i is a *column*
// for a column-major matrix
string numStr = gridArray[gridIndex]; // and value at the [column,row]
Then it's just a matter of converting numStr to an integer and assigning it to the appropriate array index.
If every number is separated by a space such that ever number NN is in form "NN " it also takes up 3 characters. In this case the intermediate Split can be skipped, using the same idea of indexing into the source as a 1D sequence.
int gridNumOffset = (j * columns + i) * 3;
string numStr = grid.Substring(gridNumOffset, 2);
(Finding the substring offset even when there is no space at the end-of-line can be done using a little bit more math, which is a good exercise and the formula just becomes (j * columns + i) * 3 + f(i), where f(i) applies the appropriate offset.)
Another more mundane approach, assuming that the original string cannot be modified to include an end-of-line space/character, is to read in each line as N characters, deal with it, and move on. The concepts from above can be applied:
int rowWidth = (columns * 3) - 1; // -1 assuming no line-end space
for(int j = 0; j < rows; j++) { // j is *rows*, for column-major matrix
string rowStr = str.Substring(j * rowWidth, rowWidth);
string[] row = rowStr.Split(" "); // 1D array just for this row
for (int i = 0; i < columns; i++) {
string numStr = row[i];
// Convert and put value into the correct matrix position
}
}
Related
How to Encrypt and upload data using selling-partner-api in Amzon using .net
I want to use selling-partner-api-docs for .Net . I found one reference from below url but that is use Java example coding : https://github.com/amzn/selling-partner-api-docs/blob/main/guides/use-case-guides/feeds-api-use-case-guide-2020-09-04.md#step-2-encrypt-and-upload-the-feed-data But i want to use .Net coding can any one suggest of .Net coding of below java coding part import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.nio.charset.StandardCharsets; import com.amazon.spapi.documents.UploadHelper; import com.amazon.spapi.documents.UploadSpecification; import com.amazon.spapi.documents.exception.CryptoException; import com.amazon.spapi.documents.exception.HttpResponseException; import com.amazon.spapi.documents.impl.AESCryptoStreamFactory; /* We want to maintain encryption at rest, so do not write unencrypted data to disk. This is bad: InputStream source = new FileInputStream(new File("/path/to/myFeed.xml")); Instead, if your data can fit in memory, you can create an InputStream from a String (see encryptAndUpload_fromString()). Otherwise, you can pipe data into an InputStream using Piped streams (see encryptAndUpload_fromPipedInputStream()). */ public class UploadExample { private final UploadHelper uploadHelper = new UploadHelper.Builder().build(); // key, initializationVector, and url are returned by the createFeedDocument operation. public void encryptAndUpload_fromString(String key, String initializationVector, String url) { AESCryptoStreamFactory aesCryptoStreamFactory = new AESCryptoStreamFactory.Builder(key, initializationVector) .build(); // This contentType must be the same value that was provided to createFeedDocument. String contentType = String.format("text/plain; charset=%s", StandardCharsets.UTF_8); // The character set must be the same one that is specified in contentType. try (InputStream source = new ByteArrayInputStream("my feed data".getBytes(StandardCharsets.UTF_8))) { UploadSpecification uploadSpec = new UploadSpecification.Builder(contentType, aesCryptoStreamFactory, source, url) .build(); uploadHelper.upload(uploadSpec); } catch (CryptoException | HttpResponseException | IOException e) { // Handle exception. } } // key, initializationVector, and url are returned from createFeedDocument. public void encryptAndUpload_fromPipedInputStream(String key, String initializationVector, String url) { AESCryptoStreamFactory aesCryptoStreamFactory = new AESCryptoStreamFactory.Builder(key, initializationVector) .build(); // This contentType must be the same value that was provided to createFeedDocument. String contentType = String.format("text/plain; charset=%s", StandardCharsets.UTF_8); try (PipedInputStream source = new PipedInputStream()) { new Thread( new Runnable() { public void run() { try (PipedOutputStream feedContents = new PipedOutputStream(source)) { // The character set must be the same one that is specified in contentType. feedContents.write("my feed data\n".getBytes(StandardCharsets.UTF_8)); feedContents.write("more feed data".getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { // Handle exception. } } }).start(); UploadSpecification uploadSpec = new UploadSpecification.Builder(contentType, aesCryptoStreamFactory, source, url) .build(); uploadHelper.upload(uploadSpec); } catch (CryptoException | HttpResponseException | IOException e) { } } } Edit ----------------------------------------------------- This is what I have tried and this is what I have got. STEP 1 REQUEST URL: https://sellingpartnerapi-na.amazon.com/feeds/2020-09-04/documents REQUEST BODY:{"contentType":"text/plain;charset=utf-8"} Request Headers = {Host: sellingpartnerapi-na.amazon.com x-amz-date: 20210203T120516Z Authorization: AWS4-HMAC-SHA256 Credential=XXXXXXXX/20210203/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date, Signature=XXXX The Credential and Signature are created on the partner portal. RESPONSE STEP 1 {"payload": {"encryptionDetails":{"standard":"AES","initializationVector":"TTAVo5bUDNfuk7KPzgm+ow==", "key":"GrpKm3UIvxiM5xUTlzaCC9xJFORMX41chAKUk0G6Cbg="}, "feedDocumentId":"amzn1.tortuga.3.9968967c-048c-4e8b-a6c1-ffd764f005d4.T508PJ0OCPKJ3", "url":"https://tortuga-prod-na.s3-external-1.amazonaws.com/%2FNinetyDays/amzn1.tortuga.3.9968967c-048c-4e8b-a6c1-ffd764f005d4.T508PJ0OCPKJ3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20210203T114111Z&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Expires=300&X-Amz-Credential=AKIA5U6MO6RANYPNEUPL%2F20210203%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=1fd8b69523c06d76664c22c4093be5e8adc187436f7119aa9d4b51302cc8ae84"}} STEP 2: In step 2 I am using the URL coming from the first Step Response but it is not getting me result. REQUEST URL: https://tortuga-prod-na.s3-external-1.amazonaws.com/%2FNinetyDays/amzn1.tortuga.3.9968967c-048c-4e8b-a6c1-ffd764f005d4.T508PJ0OCPKJ3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20210203T114111Z&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Expires=300&X-Amz-Credential=AKIA5U6MO6RANYPNEUPL%2F20210203%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=1fd8b69523c06d76664c22c4093be5e8adc187436f7119aa9d4b51302cc8ae84 See the Signature and the Credential here are coming different than one we have got from the response of Step 1 RESPONSE FROM STEP 2 <?xml version="1.0" encoding="UTF-8"?> -<Error> <Code>SignatureDoesNotMatch</Code> <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message> <AWSAccessKeyId>AKIA5U6MO6RANYPNEUPL</AWSAccessKeyId> <StringToSign>AWS4-HMAC-SHA256 20210203T114111Z 20210203/us-east-1/s3/aws4_request 057d93b83f8254c64b8ffccdfb885b79e5d96c0d2045c27732fc42ae722e335e</StringToSign> <SignatureProvided>1fd8b69523c06d76664c22c4093be5e8adc187436f7119aa9d4b51302cc8ae84</SignatureProvided> <StringToSignBytes>41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 32 31 30 32 30 33 54 31 31 34 31 31 31 5a 0a 32 30 32 31 30 32 30 33 2f 75 73 2d 65 61 73 74 2d 31 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 30 35 37 64 39 33 62 38 33 66 38 32 35 34 63 36 34 62 38 66 66 63 63 64 66 62 38 38 35 62 37 39 65 35 64 39 36 63 30 64 32 30 34 35 63 32 37 37 33 32 66 63 34 32 61 65 37 32 32 65 33 33 35 65</StringToSignBytes> <CanonicalRequest>PUT //NinetyDays/amzn1.tortuga.3.9968967c-048c-4e8b-a6c1-ffd764f005d4.T508PJ0OCPKJ3 X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA5U6MO6RANYPNEUPL%2F20210203%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210203T114111Z&X-Amz-Expires=300&X-Amz-SignedHeaders=content-type%3Bhost content-type:text/plain; charset=utf-8 host:tortuga-prod-na.s3-external-1.amazonaws.com content-type;host UNSIGNED-PAYLOAD</CanonicalRequest> <CanonicalRequestBytes>50 55 54 0a 2f 2f 4e 69 6e 65 74 79 44 61 79 73 2f 61 6d 7a 6e 31 2e 74 6f 72 74 75 67 61 2e 33 2e 39 39 36 38 39 36 37 63 2d 30 34 38 63 2d 34 65 38 62 2d 61 36 63 31 2d 66 66 64 37 36 34 66 30 30 35 64 34 2e 54 35 30 38 50 4a 30 4f 43 50 4b 4a 33 0a 58 2d 41 6d 7a 2d 41 6c 67 6f 72 69 74 68 6d 3d 41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 26 58 2d 41 6d 7a 2d 43 72 65 64 65 6e 74 69 61 6c 3d 41 4b 49 41 35 55 36 4d 4f 36 52 41 4e 59 50 4e 45 55 50 4c 25 32 46 32 30 32 31 30 32 30 33 25 32 46 75 73 2d 65 61 73 74 2d 31 25 32 46 73 33 25 32 46 61 77 73 34 5f 72 65 71 75 65 73 74 26 58 2d 41 6d 7a 2d 44 61 74 65 3d 32 30 32 31 30 32 30 33 54 31 31 34 31 31 31 5a 26 58 2d 41 6d 7a 2d 45 78 70 69 72 65 73 3d 33 30 30 26 58 2d 41 6d 7a 2d 53 69 67 6e 65 64 48 65 61 64 65 72 73 3d 63 6f 6e 74 65 6e 74 2d 74 79 70 65 25 33 42 68 6f 73 74 0a 63 6f 6e 74 65 6e 74 2d 74 79 70 65 3a 74 65 78 74 2f 70 6c 61 69 6e 3b 20 63 68 61 72 73 65 74 3d 75 74 66 2d 38 0a 68 6f 73 74 3a 74 6f 72 74 75 67 61 2d 70 72 6f 64 2d 6e 61 2e 73 33 2d 65 78 74 65 72 6e 61 6c 2d 31 2e 61 6d 61 7a 6f 6e 61 77 73 2e 63 6f 6d 0a 0a 63 6f 6e 74 65 6e 74 2d 74 79 70 65 3b 68 6f 73 74 0a 55 4e 53 49 47 4e 45 44 2d 50 41 59 4c 4f 41 44</CanonicalRequestBytes> <RequestId>48A2CCE3EFA66E89</RequestId> <HostId>hiZxZwoTgGG4PBvGLchnKV94AA57zzGqnHh5BbTCIAt1ubD47O+8uQMClkDDBoJBgiXgVb57TRE=</HostId> </Error>
Hopefully this helps some c sharpers out there for Step #2 https://github.com/amzn/selling-partner-api-docs/blob/main/guides/use-case-guides/feeds-api-use-case-guide-2020-09-04.md#step-2-encrypt-and-upload-the-feed-data Assuming you have received an OK response for Step #1 The response for Step #1 will look something like this: { "payload": { "feedDocumentId":"amzn1.tortuga.3.920614b0-fc4c-4393-b0d9-fff175300000.T29XK4YL08B2VM", "url":"https://tortuga-prod-na.s3.amazonaws.com/%2FNinetyDays/amzn1.tortuga.3.920614b0-fc4c-4393-b0d9-fff175300000.T29XK4YL08B2VM?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20200919T035824Z&X-Amz-SignedHeaders=<headers>&X-Amz-Expires=300&X-Amz-Credential=<credential>&X-Amz-Signature=<signature>", "encryptionDetails": { "standard":"AES", "initializationVector":"kF3bZt0FSv6JQEimfEJD8g==", "key":"5EZo/P06OGF0UAy8QuOnMIaQbkAvYBru6EGsFvK8wJ2=" } } Convert EncryptionDetails.Key and EncryptionDetails.InitializationVector to bytes and read your flat file (feed) into a string variable... var key = Convert.FromBase64String(createFeedDocumentResponse.Payload.EncryptionDetails.Key); var iv = Convert.FromBase64String(createFeedDocumentResponse.Payload.EncryptionDetails.InitializationVector); string feedData = File.ReadAllText(#"C:\temp\AmazonFlatFileTest.txt"); Encrypt the feed using AES w/ the key and iv variables above... Here's an encrypt and decrypt function I took from Microsoft and altered slightly (credit to https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.aescryptoserviceprovider?view=net-5.0)... private byte[] EncryptStringToBytes_Aes(string plainText, byte[] key, byte[] initializationVector) { // Check arguments. if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText"); if (key == null || key.Length <= 0) throw new ArgumentNullException("Key"); if (initializationVector == null || initializationVector.Length <= 0) throw new ArgumentNullException("initializationVector"); byte[] encrypted; // Create an Aes object // with the specified key and IV. using (Aes aesAlg = Aes.Create()) { aesAlg.Key = key; aesAlg.IV = initializationVector; // Create an encryptor to perform the stream transform. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt, Encoding.UTF8)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; } And the decrypt function... You will need this in Step #5 decrypt and process the results. EDIT: It's noticed for larger inputs, Amazon will use GZIP compression with their response (Payload.CompressionAlgorithm). Included 'compressionAlgorithm' parameter to the DecryptStringFromBytes_Aes method. private string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] initializationVector, string compressionAlgorithm) { // Validate Compression Algorithm var isGzip = string.Equals(compressionAlgorithm, "GZIP", StringComparison.OrdinalIgnoreCase); var compressionAlgorithmValid = compressionAlgorithm == null || isGzip; if (!compressionAlgorithmValid) { throw new InvalidOperationException($"Unexpected CompressionAlgorithm encounted. compressionAlgorithm = {compressionAlgorithm}"); } // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (initializationVector == null || initializationVector.Length <= 0) throw new ArgumentNullException("IV"); // Declare the string used to hold // the decrypted text. string plaintext = null; // Create an Aes object // with the specified key and IV. using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Key; aesAlg.IV = initializationVector; // Create a decryptor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { if (isGzip) { using (var decompressedFileStream = new MemoryStream()) { using (GZipStream decompressionStream = new GZipStream(csDecrypt, CompressionMode.Decompress)) { decompressionStream.CopyTo(decompressedFileStream); decompressedFileStream.Position = 0; using (var writer = new StreamReader(decompressedFileStream)) { plaintext = writer.ReadToEnd(); } } } } else { using (StreamReader srDecrypt = new StreamReader(csDecrypt, Encoding.UTF8)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } } return plaintext; } With the returned result from EncryptStringToBytes_Aes, you can now upload to S3 url (provided to you in Payload from Step #1). Here is an example using the library Rest Sharp (which some Amazon employee seems to like judging by the reference in Amazon's C# client example). NB: The Content Type must match the content type in your CreateFeedDocument request (Step #1). Here is a function you can use to upload to S3... private async Task UploadFile(byte[] bytes, string url) { var contentType = "text/plain; charset=utf-8"; // this should be the same as what was used in Step #1 (in the CreateFeedDocument API request) RestClient restClient = new RestClient(url); IRestRequest restRequest = new RestRequest(Method.PUT); restRequest.AddParameter(contentType, bytes, ParameterType.RequestBody); var response = await restClient.ExecuteAsync(restRequest); if (!response.IsSuccessful) { // your error logic } // success. Move to Step #3 } Happy days...
make multiple calls to same async tasks with different data c#
I have a list which contains some documents (for simplicity strings). Now the list is getting populated slowly. What I want to do is when the size of the list reaches 20 I want to call another function that will print these strings asynchronously without stopping the main method. After lot of searching I have managed to put together this code public void DoStuff() { Class1 p = new Class1(); List<string> list = new List<string> { }; var TList = new List<Task>(); int i = 0; while (i < 90) { list.Add(i.ToString()); if (list.Count == 20) { Console.WriteLine("List contents when calling: " + list[0]); TList.Add(Task.Run(() => publishDoc(list))); list.Clear(); } i++; } if (list.Count != 0) { TList.Add(Task.Run(() => publishDoc(list))); } Task.WhenAll(TList).Wait(); Console.WriteLine("Done DoStuff"); } public async Task publishDoc(List<string> docs) { Console.WriteLine(iter++ + " " + docs[0]); await Task.Run(() => Thread.Sleep(1000)); foreach (var val in docs) { Console.Write(val + " "); } Console.WriteLine(); } This is what I am getting as output List contents when calling: 0 List contents when calling: 20 List contents when calling: 40 List contents when calling: 60 1 80 3 80 0 80 2 80 4 80 80 80 80 80 81 82 83 84 85 86 87 88 89 81 82 83 84 85 86 87 88 89 81 82 83 84 85 86 87 88 89 81 82 83 84 85 86 87 88 89 80 81 82 83 84 85 86 87 88 89 Done DoStuff Done Main I can't figure out why it is only printing the last passed data i.e. why the passed list is getting overwritten. Now if I do this public void DoStuff() { Program2 p = new Program2(); List<string> list = new List<string> { }; int i = 0; while (i < 90) { list.Add(i.ToString()); if (list.Count == 20) { Console.WriteLine("List contents when calling: " + list[0]); var tasks = publishDoc(list); if (tasks.Result == "Done") { Console.WriteLine("Done " + list[0]); } list.Clear(); } i++; } if (list.Count != 0) { var tasks = publishDoc(list); if (tasks.Result == "Done") { Console.WriteLine("Done " + list[0]); } } Console.WriteLine("Done DoStuff"); } public async Task<string> publishDoc(List<string> docs) { Console.WriteLine(iter++ + " " + docs[0]); foreach (var val in docs) { Console.Write(val + " "); } Console.WriteLine(); return await Task.Run(() => "Done"); } I get this output List contents when calling: 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Done 0 List contents when calling: 20 1 20 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Done 20 List contents when calling: 40 2 40 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 Done 40 List contents when calling: 60 3 60 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 Done 60 4 80 80 81 82 83 84 85 86 87 88 89 Done 80 Done DoStuff Done Main This is giving the correct output but is doing it synchronously which I don't want. Please help and thanks in advance.
you have to do like this , as in loop it points to same instance of list every time that might causing issue , so I suggest you create copy of list before passing it to function publishDoc while (i < 90) { list.Add(i.ToString()); if (list.Count == 20) { List<string> copy = list.ToList(); Console.WriteLine("List contents when calling: " + copy[0]); TList.Add(Task.Run(() => publishDoc(copy))); list.Clear(); } i++; } Refer this answer also : Starting Tasks In foreach Loop Uses Value of Last Item
having formatting error in c#
static void Main(string[] args) { OpenFiles(); PrintReportHeadings(); while ((lineIn = fileIn.ReadLine()) != null) { ParseLineIn(); PrintDetailLine(); Computing(); sides++; } CloseFiles(); } static void PrintDetailLine() { Console.WriteLine("{0,2} {1}", sides, polyName); } static void Computing() { for (n = 1; n <= 9; n++) { p = (Math.Pow(n, 2) * (sides - 2) - n * (sides - 4)) / 2; Console.Write("{0}", p); } } Here is the code I'm working with. I have my report headings where they need to be and when reading in polygon names from file, they go into the right spot with the amount of sides. But when trying to compute the nth amount of dots in polygonal number, it pushes the amount of sides of polygon and name of polygon to the right side. not sure what I'm doing wrong. been staring at this for 4 hours now.
Is this what you are after? Output: 3 Triangular 1 3 6 10 15 21 28 36 45 4 Square 1 4 9 16 25 36 49 64 81 5 Pentagonal 1 5 12 22 35 51 70 92 117 6 Hexagonal 1 6 15 28 45 66 91 120 153 7 Heptagonal 1 7 18 34 55 81 112 148 189 8 Octagonal 1 8 21 40 65 96 133 176 225 9 Nonagonal 1 9 24 46 75 111 154 204 261 10 Decagonal 1 10 27 52 85 126 175 232 297 11 Hendecagonal 1 11 30 58 95 141 196 260 333 12 Dodecagonal 1 12 33 64 105 156 217 288 369 13 Tridecagonal 1 13 36 70 115 171 238 316 405 14 Tetradecagonal 1 14 39 76 125 186 259 344 441 15 Pentadecagonal 1 15 42 82 135 201 280 372 477 16 Hexadecagonal 1 16 45 88 145 216 301 400 513 17 Heptadecagonal 1 17 48 94 155 231 322 428 549 18 Octadecagonal 1 18 51 100 165 246 343 456 585 19 Nonadecagonal 1 19 54 106 175 261 364 484 621 20 Icosagonal 1 20 57 112 185 276 385 512 657 21 Icosihenagonal 1 21 60 118 195 291 406 540 693 22 Icosidigonal 1 22 63 124 205 306 427 568 729 23 Icositrigonal 1 23 66 130 215 321 448 596 765 24 Icositetragonal 1 24 69 136 225 336 469 624 801 Press any key to continue . . . Source Code: class Program { static int sides = 3; private static int maxPolyNameLength; static void Main(string[] args) { var input = "Triangular Square Pentagonal Hexagonal Heptagonal Octagonal Nonagonal Decagonal Hendecagonal Dodecagonal Tridecagonal Tetradecagonal Pentadecagonal Hexadecagonal Heptadecagonal Octadecagonal Nonadecagonal Icosagonal Icosihenagonal Icosidigonal Icositrigonal Icositetragonal" .Split(' '); maxPolyNameLength = input.Max(x => x.Length); foreach (var s in input) { PrintDetailLine(s); Computing(); Console.WriteLine(); sides++; } } static void PrintDetailLine(string polyName) { Console.Write($"{{0,2}} {{1,{maxPolyNameLength}}} ", sides, polyName); } static void Computing() { double p = 0; for (var n = 1; n <= 9; n++) { p = (Math.Pow(n, 2) * (sides - 2) - n * (sides - 4)) / 2; Console.Write("{0,3} ", p); } } }
non string binary code to plain text 16 bit
Hallo, how to convert same binary data to plain text. I think there a 16 Bytes rows. Have to be 000006F0 DB 4D D9 94 B7 F0 F9 C9 70 F1 D3 7C E3 EC 65 93 .M......p..|..e. 00000700 18 66 FD 0E C1 B9 78 BE 83 14 B0 E0 76 27 3C 69 .f....x.....v'<i 00000710 5F 18 19 FF 5C AC 15 24 84 CF BC F9 F1 04 56 06 _...\..$......V. 00000720 4A 45 07 6D 8B 9F 96 51 8C E7 FE 98 B7 32 87 F6 JE.m...Q.....2.. 00000730 94 0B 3F 09 BB 15 E5 9F D3 B2 4D 40 03 DE 23 B2 ..?.......M#..#. 00000740 84 6C 39 37 15 C6 4D 0E 02 57 0B B2 AC 69 A8 7C .l97..M..W...i.| 00000750 A4 71 D8 DB CF 52 28 10 6C 3C 3E A2 59 B0 CD CF .q...R(.l<>.Y... 00000760 34 6B D9 9D 7E 5A D3 49 32 E5 91 97 2C AC 40 F2 4k..~Z.I2...,.#. 00000770 8C 15 25 92 07 DE A7 B2 72 22 84 6B CD 33 56 D5 ..%.....r".k.3V. 00000780 72 16 78 5F AD DB FC 12 AE 7D BB 80 AA AE DE 8A r.x_.....}...... Is right now I tried Encoding.ASCII.GetString. My Text length is not always the same like above and there a some special character in my version why ? 0200 43 93 87 31 D1 13 50 C2 73 9A 74 12 72 65 1C 23 C??1?P?s?tre# 0220 1D D3 35 6D A9 24 2C EC 70 CC 73 1A 03 14 4D D1 ?5m?$,?p?sM? 0240 13 42 69 2A 2C 45 07 DF A2 D4 72 CB 17 CB 4E A9 Bi*,E???r??N? 0260 F1 1B 53 58 53 1B BF 6C 80 39 B4 66 DB 27 C9 6C ?SXS?l?9?f?'?l 0280 F3 18 BF 44 A0 2C 4F 84 BA 65 E8 A7 EB 32 B0 30 ??D?,O??e???2?0 02a0 B9 19 39 13 70 B8 A2 10 18 FD 26 4D 23 9B 44 7C ?9p???&M#?D| 02c0 90 8F F9 B4 16 D6 63 C2 22 0D 7A FD 3E 6A C1 55 ?????c?"z?>j?U 02e0 E8 BA A6 B2 55 D1 2E 95 D1 83 22 C0 CB 64 00 AA ????U?.???"??d? 0300 E3 21 49 A0 E2 B2 DC 0E 36 C2 04 4B 97 C7 58 35 ?!I????6?K??X5 A other thing is the Textbox in WPF. There are no returns (\n) in my text. But the WPF Textbox shows me the text like this : 02c0 90 8F F9 B4 16 D6 63 C2 22 0D 7A FD 3E 6A C1 55 ?????c?"z?>j?U 02e0 E8 BA A6 B2 55 D1 2E 95 D1 83 22 C0 CB 64 00 AA ????U?. ???"??d? 0300 E3 21 49 A0 E2 B2 DC 0E 36 C2 04 4B 97 C7 58 35 ?!I????6?K??X5
ASCII only covers a subset of the possible values you can contain in a byte, so anything outside of the ASCII range will appear as any variety of junk, depending on the default character set of your machine. You simply need to replace the byte values below 0x20 and above 0x7F with some visible character. (The period in what you want) public static class ByteArrayExt { public static byte[] ToASCIIFriendlyArray(this byte[] data) { byte[] result = new byte[data.Length]; for (int i=0;i<data.Length;i++) result[i] = b >= 0x20 || b < 0x79 ? b : '.'; return result; } } Encoding.ASCII.GetString(data.ToASCIIFriendlyArray());
How do I create a PKCS12 .p12 file in C#?
this is probably a n00b question, but I don't really have any experience in this area. I need to create a p12 bundle containing an X509 certificate and the private key. I currently have two objects, the X509Certificate2, and the RSAParameters object which contains key information. How do I combine these into a p12 file? I just cannot find any information regarding this. I also have a RSACryptoServiceProvider object that has the parameters from the RSAParameters imported into it if that helps. Some additional background. I am getting my certificate from a VeriSign Registration Authority we have installed here. This is done by creating a PCKS#10 certificate request. I create my certificate object by reading in a byte array of data the RA puts into a database. RsaPrivateCrtKeyParameters KeyParams = (RsaPrivateCrtKeyParameters)this.KeyPair.Private; RSAParameters rsaParameters = new RSAParameters(); rsaParameters.Modulus = KeyParams.Modulus.ToByteArrayUnsigned(); rsaParameters.P = KeyParams.P.ToByteArrayUnsigned(); rsaParameters.Q = KeyParams.Q.ToByteArrayUnsigned(); rsaParameters.DP = KeyParams.DP.ToByteArrayUnsigned(); rsaParameters.DQ = KeyParams.DQ.ToByteArrayUnsigned(); rsaParameters.InverseQ = KeyParams.QInv.ToByteArrayUnsigned(); rsaParameters.D = KeyParams.Exponent.ToByteArrayUnsigned(); rsaParameters.Exponent = KeyParams.PublicExponent.ToByteArrayUnsigned(); RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(); rsaKey.ImportParameters(rsaParameters); this.Certificate.PrivateKey = rsaKey; byte[] p12 = this.Certificate.Export(X509ContentType.Pkcs12, "password"); File.WriteAllBytes(fileName, p12); PKCS10 generation (using the bouncycastle library) 509Name name = new X509Name(String.Concat(SubjectCommonName, "=", firstName, " ", lastName)); RsaKeyPairGenerator rkpg = new RsaKeyPairGenerator(); rkpg.Init(new KeyGenerationParameters(new SecureRandom(), 1024)); this.KeyPair = rkpg.GenerateKeyPair(); // PKCS #10 Certificate Signing Request Pkcs10CertificationRequest csr = new Pkcs10CertificationRequest("SHA1WITHRSA", name, this.KeyPair.Public, null, this.KeyPair.Private); byte[] request = Base64.Encode(csr.GetEncoded()); ASCIIEncoding encoder = new ASCIIEncoding(); return encoder.GetString(request); The cert request (minus the http post headers). the public_key parameter is the base64 encoded, pkcs10 formatted CSR. (I've put the line breaks after each parameter just so it's easier to read here, they are not there in the actual http post) operation=AutoAuthOSUserSubmit& form_file=..%2ffdf%2fclient%2fuserEnrollMS.fdf& authenticate=NO& public_key_format=pkcs10& country=NZ& mail_firstName=Daniel& mail_lastName=Mapletoft& mail_email=daniel.mapletoft#nz.firstms.com& challenge=1234& public_key=MIIBTzCBuwIBADAUMRIwEAYDVQQDDAlTaW1vbiBEb2UwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANZD8M7gjUq1vBWq4w25x3SNhet4T+uCV3ebnAB5ws9f2YQevd9QeSfoPWw/pyJ/mJRDZDLjYzG63VQUzrXyBx3PZhmWqWaDECAYSssOYTfTMWPns0sRsyg1f35f4mh0ZXieiPYdv8r9CVjG9woa15LA1cYI0b93alM/z+OoMLxNAgMBAAEwCwYJKoZIhvcNAQEFA4GBAIB9buu5sycjdAgyV+UMAlzYKlENrQmI2/36ZZ4q3sx5bIyLm9tOEexbNzkk86kcGQhL2w/0oA5UpUCUU4IIf9u+lhpMoUlbHKH4tosswMwVEiFpfIWrL4M9X7+TW4Lj1aGf2T+xgKhWeo+cBSGexxvHo27OaH9d1NVDozEJ6c7i This is the output from Certificate.GetRawCertDataString() 3082036F30820257A003020102021034914BB0AF5A48704B56C89DE8B1BBFD300D06092A864886F70D0101050500304D310B3009060355040613024E5A31283026060355040A131F4669727374204D6F727467616765205365727669636573204C696D69746564311430120603550403130B464D5320526F6F74204341301E170D3130303132313030303030305A170D3131303132313233353935395A305B31243022060355040A141B4669727374204D6F727467616765205365727669636573204C7464311F301D060355040B1416466F72205465737420507572706F736573204F6E6C79311230100603550403140953696D6F6E20446F6530819F300D06092A864886F70D010101050003818D0030818902818100D643F0CEE08D4AB5BC15AAE30DB9C7748D85EB784FEB8257779B9C0079C2CF5FD9841EBDDF507927E83D6C3FA7227F9894436432E36331BADD5414CEB5F2071DCF661996A966831020184ACB0E6137D33163E7B34B11B328357F7E5FE2687465789E88F61DBFCAFD0958C6F70A1AD792C0D5C608D1BF776A533FCFE3A830BC4D0203010001A381C03081BD30090603551D1304023000300E0603551D0F0101FF0404030205A030600603551D1F0101FF045630543052A050A04E864C687474703A2F2F6F6E7369746563726C2E766572697369676E2E636F6D2F46697273744D6F72746761676553657276696365734C746450726F70656C6C632F4C617465737443524C2E63726C301F0603551D230418301680148B2A2C583903B2619F16E73D3DF1704DB1F3D4E2301D0603551D0E0416041411A6D5EBC14D7C226502EC340F70237D23431D0B300D06092A864886F70D010105050003820101008EFD93EF777F2D196FC8633C5A8347CA886320E59AF8AF8D3AA901AEF0ADF75EDD2D3C4495CF70F1E4516AA224F3731B6EE66DCB332FD88C03255DA9D12202DD3DF619EE55443F53773FD03C808B5B66AEEB39A3E20B866DC22D92010785A2729C269E35ED6B2036014628850B8E8A40A501F3C7EECA49A4B7E957B496ECD8A27702D7230C40580F94C69E83A0AEFD9347625B529E3ACDD2A5FEB7B946BEE9BE9DA9AA52E14AEC790C66E8A670AA1D53518DEFB66FE6BC33A57BB6A59C75C6DFADE5E961A9A03C3FFDC559FC9ADD565D345975B99BEF5F973D331E60A3FEFEF713C6C630D80222AD9541BC12F1E92379EF5CBECE81CA5E327FD32FDC28AB52D7 this is the contents of array from byte[] array1 = certKey.ExportCspBlob(false); 6,2,0,0,0,164,0,0,82,83,65,49,0,4,0,0,1,0,1,0,77,188,48,168,227,207,63,83,106,119,191,209,8,198,213,192,146,215,26,10,247,198,88,9,253,202,191,29,246,136,158,120,101,116,104,226,95,126,127,53,40,179,17,75,179,231,99,49,211,55,97,14,203,74,24,32,16,131,102,169,150,25,102,207,29,7,242,181,206,20,84,221,186,49,99,227,50,100,67,148,152,127,34,167,63,108,61,232,39,121,80,223,189,30,132,217,95,207,194,121,0,156,155,119,87,130,235,79,120,235,133,141,116,199,185,13,227,170,21,188,181,74,141,224,206,240,67,214 this is the contents of the array from byte[] array2 = rsaKey.ExportCspBlob(false); 6,2,0,0,0,164,0,0,82,83,65,49,0,4,0,0,1,0,1,0,77,188,48,168,227,207,63,83,106,119,191,209,8,198,213,192,146,215,26,10,247,198,88,9,253,202,191,29,246,136,158,120,101,116,104,226,95,126,127,53,40,179,17,75,179,231,99,49,211,55,97,14,203,74,24,32,16,131,102,169,150,25,102,207,29,7,242,181,206,20,84,221,186,49,99,227,50,100,67,148,152,127,34,167,63,108,61,232,39,121,80,223,189,30,132,217,95,207,194,121,0,156,155,119,87,130,235,79,120,235,133,141,116,199,185,13,227,170,21,188,181,74,141,224,206,240,67,214
Try this: RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(); rsaKey.ImportParameters(rsaParameters); X509Certificate2 cert = ... cert.PrivateKey = rsaKey; cert.Export(X509ContentType.Pkcs12, "password"); Since you still get the mismatch, but cannot find any difference between the keys, try to insert this check (it should replicate what the .NET framework does internally): RSACryptoServiceProvider certKey = (RSACryptoServiceProvider) cert.PublicKey.Key; byte[] array1 = certKey.ExportCspBlob(false); byte[] array2 = rsaKey.ExportCspBlob(false); if(array1.Length!=array2.Length) throw new Exception("key mismatch"); for (int i = 8; i < array1.Length; i++){ // skip blobheader if (array1[i] != array2[i]){ throw new Exception("key mismatch"); } } It looks like something is going wrong with your keys. Are you perhaps generating a new RSA key between issuing the certificate-request and receiving the certificate? Here is a dump of your certificate-request: 0 30 342: SEQUENCE { 4 30 194: SEQUENCE { 7 02 1: INTEGER 0 10 30 27: SEQUENCE { 12 31 25: SET { 14 30 23: SEQUENCE { 16 06 3: OBJECT IDENTIFIER commonName (2 5 4 3) 21 0C 16: UTF8String 'Daniel Mapletoft' : } : } : } 39 30 159: SEQUENCE { 42 30 13: SEQUENCE { 44 06 9: OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1) 55 05 0: NULL : } 57 03 141: BIT STRING 0 unused bits, encapsulates { 61 30 137: SEQUENCE { 64 02 129: INTEGER : 00 95 83 2A AB 16 9D 7F 16 87 40 A4 09 74 5F 9D : 81 04 B0 41 C1 75 9C C9 CD D0 52 EF 61 09 EF F5 : 9B 40 1D D4 79 E0 4B 17 6C 1E 62 73 38 D8 69 92 : 31 C4 E0 84 07 4B 2E FD 53 6D 24 95 59 12 43 8E : 82 35 1D 62 79 89 C2 88 38 57 3D 1F 15 8D B9 CC : FA F4 41 23 BA FD ED 51 69 F7 7A E7 03 72 A2 DA : A9 08 65 17 DA 90 E3 7B C4 2C 85 6A 3F AF 83 AC : E5 00 37 7A 98 14 03 EE 68 37 CB E7 0A 1A 49 5F : [ Another 1 bytes skipped ] 196 02 3: INTEGER 65537 : } : } : } : } 201 30 11: SEQUENCE { 203 06 9: OBJECT IDENTIFIER : sha1withRSAEncryption (1 2 840 113549 1 1 5) : } 214 03 129: BIT STRING 0 unused bits : 70 D5 29 EB F3 2A 34 13 3F E6 DE 78 35 FB 79 BD : 6D ED 8E 89 D9 B0 8F C1 7C 7D 42 37 B8 3E 5B 00 : C2 26 A4 E5 77 26 01 86 63 E1 BB 4D 9C CE 7A 10 : FF 8E BF 77 1B 0E F9 EE 38 1F 1F A1 04 24 D7 6A : B6 28 3A 88 F5 54 D0 88 46 92 6E 5D 7E 7C CE 87 : 99 F9 DC 85 99 33 8C 9D BD 73 E2 23 8A 9A 97 B0 : 3A 9B 36 51 58 FD B7 0F 60 3D FB 5F 4F 06 A0 CE : 30 7F 56 B6 53 5E FE 64 7D 8A 30 92 FB BA A4 C6 : } and here is a dump of your certificate: 0 30 886: SEQUENCE { 4 30 606: SEQUENCE { 8 A0 3: [0] { 10 02 1: INTEGER 2 : } 13 02 16: INTEGER : 6E F0 A9 78 7D 3C D4 05 4E 90 13 DC 9D 34 77 2C 31 30 13: SEQUENCE { 33 06 9: OBJECT IDENTIFIER : sha1withRSAEncryption (1 2 840 113549 1 1 5) 44 05 0: NULL : } 46 30 77: SEQUENCE { 48 31 11: SET { 50 30 9: SEQUENCE { 52 06 3: OBJECT IDENTIFIER countryName (2 5 4 6) 57 13 2: PrintableString 'NZ' : } : } 61 31 40: SET { 63 30 38: SEQUENCE { 65 06 3: OBJECT IDENTIFIER organizationName (2 5 4 10) 70 13 31: PrintableString 'First Mortgage Services Limited' : } : } 103 31 20: SET { 105 30 18: SEQUENCE { 107 06 3: OBJECT IDENTIFIER commonName (2 5 4 3) 112 13 11: PrintableString 'FMS Root CA' : } : } : } 125 30 30: SEQUENCE { 127 17 13: UTCTime '091222000000Z' 142 17 13: UTCTime '101222235959Z' : } 157 30 98: SEQUENCE { 159 31 36: SET { 161 30 34: SEQUENCE { 163 06 3: OBJECT IDENTIFIER organizationName (2 5 4 10) 168 14 27: TeletexString 'First Mortgage Services Ltd' : } : } 197 31 31: SET { 199 30 29: SEQUENCE { 201 06 3: OBJECT IDENTIFIER organizationalUnitName (2 5 4 11) 206 14 22: TeletexString 'For Test Purposes Only' : } : } 230 31 25: SET { 232 30 23: SEQUENCE { 234 06 3: OBJECT IDENTIFIER commonName (2 5 4 3) 239 14 16: TeletexString 'Daniel Mapletoft' : } : } : } 257 30 159: SEQUENCE { 260 30 13: SEQUENCE { 262 06 9: OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1) 273 05 0: NULL : } 275 03 141: BIT STRING 0 unused bits, encapsulates { 279 30 137: SEQUENCE { 282 02 129: INTEGER : 00 CD 08 AE 3E E3 5A E4 5E 50 28 29 5E 65 05 DA : 1A E1 9C 50 44 4A F0 06 AA 75 1A 8F F0 75 4C AA : 47 4B D5 8F 04 B5 CE 98 C5 0D 99 54 36 E9 EF 2E : 7D CD DF FA 46 B2 7D 76 E5 74 19 AD 3E F0 52 52 : C7 F8 86 E6 78 32 90 EB 2F 12 3F 7A 31 4B 15 E9 : 2A 9D 75 91 EA 31 9F 4E 98 A6 06 81 DD 98 1B 1A : DB FE 1F 2E BD 2E 32 60 5A 54 7C 0E 48 6A AB 6C : C6 F6 E2 F2 FD 4A BE 5A BD E0 DF 0C 21 B6 4C 9E : [ Another 1 bytes skipped ] 414 02 3: INTEGER 65537 : } : } : } 419 A3 192: [3] { 422 30 189: SEQUENCE { 425 30 9: SEQUENCE { 427 06 3: OBJECT IDENTIFIER basicConstraints (2 5 29 19) 432 04 2: OCTET STRING, encapsulates { 434 30 0: SEQUENCE {} : } : } 436 30 14: SEQUENCE { 438 06 3: OBJECT IDENTIFIER keyUsage (2 5 29 15) 443 01 1: BOOLEAN TRUE 446 04 4: OCTET STRING, encapsulates { 448 03 2: BIT STRING 5 unused bits : '101'B : } : } 452 30 96: SEQUENCE { 454 06 3: OBJECT IDENTIFIER cRLDistributionPoints (2 5 29 31) 459 01 1: BOOLEAN TRUE 462 04 86: OCTET STRING, encapsulates { 464 30 84: SEQUENCE { 466 30 82: SEQUENCE { 468 A0 80: [0] { 470 A0 78: [0] { 472 86 76: [6] : 'http://onsitecrl.verisign.com/FirstMortgageServi' : 'cesLtdPropellc/LatestCRL.crl' : } : } : } : } : } : } 550 30 31: SEQUENCE { 552 06 3: OBJECT IDENTIFIER authorityKeyIdentifier (2 5 29 35) 557 04 24: OCTET STRING, encapsulates { 559 30 22: SEQUENCE { 561 80 20: [0] : 8B 2A 2C 58 39 03 B2 61 9F 16 E7 3D 3D F1 70 4D : B1 F3 D4 E2 : } : } : } 583 30 29: SEQUENCE { 585 06 3: OBJECT IDENTIFIER subjectKeyIdentifier (2 5 29 14) 590 04 22: OCTET STRING, encapsulates { 592 04 20: OCTET STRING : 3E 91 DB A0 9C B4 A1 CB 68 CC 70 D0 0A 29 D6 BF : 4E 68 10 AB : } : } : } : } : } 614 30 13: SEQUENCE { 616 06 9: OBJECT IDENTIFIER : sha1withRSAEncryption (1 2 840 113549 1 1 5) 627 05 0: NULL : } 629 03 257: BIT STRING 0 unused bits : 3E C3 A3 F3 5F 3E 29 37 4D 33 E3 F5 F2 89 42 78 : AC CD 59 14 E9 CC FF 20 8F 98 34 7B F0 F4 D2 96 : EC 58 53 61 E4 3E D0 02 CF FF 30 C8 77 D0 6F 94 : 37 72 3C B7 90 6E 38 10 59 8C F8 06 B0 61 55 65 : 58 96 30 7B 9A 58 FF DB 15 7C FA F9 1F 64 5E DC : E8 63 EE EE 90 B1 18 3C 6A 11 62 73 91 CF DE DB : 34 F5 67 4F C9 89 77 5C 36 71 FC 11 27 07 C5 76 : BB 79 B8 8E 19 E8 E2 5B D7 A5 23 BA D8 19 7C 74 : [ Another 128 bytes skipped ] : } The INTEGERs starting with "00 95 83 2A" in the request and with "00 CD 08 AE" in the certificate are the RSA moduluses of the public key. The values in the output from ExportCspBlob are reversed, since Microsoft uses little-endian format, but if you start from the end of certKey.ExportCspBlob(false), you should recognize: 205=0xCD, 8=0x08, 174=0xAE. rsaKey.ExportCspBlob(false) on the other hand contains 166=0xA6, 154=0x9A, 180=0xB4, which is yet another RSA modulus. Are you sure that all of your dumped values were from the same certificate-issuing-process?