private static Trim ( byte array ) : byte[] | ||
array | byte | |
Résultat | byte[] |
public static RSA FromCapiPrivateKeyBlob(byte[] blob, int offset) { if (blob == null) { throw new ArgumentNullException(nameof(blob)); } if (offset >= blob.Length) { throw new ArgumentException("blob is too small."); } RSAParameters parameters = new RSAParameters(); try { if (blob[offset] != (byte)7 || blob[offset + 1] != (byte)2 || (blob[offset + 2] != (byte)0 || blob[offset + 3] != (byte)0) || CryptoConvert.ToUInt32LE(blob, offset + 8) != 843141970U) { throw new CryptographicException("Invalid blob header"); } int int32Le = CryptoConvert.ToInt32LE(blob, offset + 12); byte[] array = new byte[4]; Buffer.BlockCopy((Array)blob, offset + 16, (Array)array, 0, 4); Array.Reverse((Array)array); parameters.Exponent = CryptoConvert.Trim(array); int srcOffset1 = offset + 20; int count1 = int32Le >> 3; parameters.Modulus = new byte[count1]; Buffer.BlockCopy((Array)blob, srcOffset1, (Array)parameters.Modulus, 0, count1); Array.Reverse((Array)parameters.Modulus); int srcOffset2 = srcOffset1 + count1; int count2 = count1 >> 1; parameters.P = new byte[count2]; Buffer.BlockCopy((Array)blob, srcOffset2, (Array)parameters.P, 0, count2); Array.Reverse((Array)parameters.P); int srcOffset3 = srcOffset2 + count2; parameters.Q = new byte[count2]; Buffer.BlockCopy((Array)blob, srcOffset3, (Array)parameters.Q, 0, count2); Array.Reverse((Array)parameters.Q); int srcOffset4 = srcOffset3 + count2; parameters.DP = new byte[count2]; Buffer.BlockCopy((Array)blob, srcOffset4, (Array)parameters.DP, 0, count2); Array.Reverse((Array)parameters.DP); int srcOffset5 = srcOffset4 + count2; parameters.DQ = new byte[count2]; Buffer.BlockCopy((Array)blob, srcOffset5, (Array)parameters.DQ, 0, count2); Array.Reverse((Array)parameters.DQ); int srcOffset6 = srcOffset5 + count2; parameters.InverseQ = new byte[count2]; Buffer.BlockCopy((Array)blob, srcOffset6, (Array)parameters.InverseQ, 0, count2); Array.Reverse((Array)parameters.InverseQ); int srcOffset7 = srcOffset6 + count2; parameters.D = new byte[count1]; if (srcOffset7 + count1 + offset <= blob.Length) { Buffer.BlockCopy((Array)blob, srcOffset7, (Array)parameters.D, 0, count1); Array.Reverse((Array)parameters.D); } } catch (Exception ex) { throw new CryptographicException("Invalid blob.", ex); } RSA rsa; try { rsa = RSA.Create(); rsa.ImportParameters(parameters); } catch (CryptographicException ex) { try { rsa = (RSA) new RSACryptoServiceProvider(new CspParameters() { Flags = CspProviderFlags.UseMachineKeyStore }); rsa.ImportParameters(parameters); } catch { throw ex; } } return(rsa); }
public static RSA FromCapiPrivateKeyBlob(byte[] blob, int offset) { if (blob == null) { throw new ArgumentNullException("blob"); } if (offset >= blob.Length) { throw new ArgumentException("blob is too small."); } RSAParameters parameters = default(RSAParameters); try { if (blob[offset] != 7 || blob[offset + 1] != 2 || blob[offset + 2] != 0 || blob[offset + 3] != 0 || CryptoConvert.ToUInt32LE(blob, offset + 8) != 843141970u) { throw new CryptographicException("Invalid blob header"); } int num = CryptoConvert.ToInt32LE(blob, offset + 12); byte[] array = new byte[4]; Buffer.BlockCopy(blob, offset + 16, array, 0, 4); Array.Reverse(array); parameters.Exponent = CryptoConvert.Trim(array); int num2 = offset + 20; int num3 = num >> 3; parameters.Modulus = new byte[num3]; Buffer.BlockCopy(blob, num2, parameters.Modulus, 0, num3); Array.Reverse(parameters.Modulus); num2 += num3; int num4 = num3 >> 1; parameters.P = new byte[num4]; Buffer.BlockCopy(blob, num2, parameters.P, 0, num4); Array.Reverse(parameters.P); num2 += num4; parameters.Q = new byte[num4]; Buffer.BlockCopy(blob, num2, parameters.Q, 0, num4); Array.Reverse(parameters.Q); num2 += num4; parameters.DP = new byte[num4]; Buffer.BlockCopy(blob, num2, parameters.DP, 0, num4); Array.Reverse(parameters.DP); num2 += num4; parameters.DQ = new byte[num4]; Buffer.BlockCopy(blob, num2, parameters.DQ, 0, num4); Array.Reverse(parameters.DQ); num2 += num4; parameters.InverseQ = new byte[num4]; Buffer.BlockCopy(blob, num2, parameters.InverseQ, 0, num4); Array.Reverse(parameters.InverseQ); num2 += num4; parameters.D = new byte[num3]; if (num2 + num3 + offset <= blob.Length) { Buffer.BlockCopy(blob, num2, parameters.D, 0, num3); Array.Reverse(parameters.D); } } catch (Exception inner) { throw new CryptographicException("Invalid blob.", inner); } RSA rsa = null; try { rsa = RSA.Create(); rsa.ImportParameters(parameters); } catch (CryptographicException ex) { try { rsa = new RSACryptoServiceProvider(new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore }); rsa.ImportParameters(parameters); } catch { throw ex; } } return(rsa); }
private static RSA FromCapiPrivateKeyBlob(byte[] blob, int offset) { RSAParameters rSAParameter = new RSAParameters(); try { if (blob[offset] != 7 || blob[offset + 1] != 2 || blob[offset + 2] != 0 || blob[offset + 3] != 0 || CryptoConvert.ToUInt32LE(blob, offset + 8) != 843141970) { throw new CryptographicException("Invalid blob header"); } int int32LE = CryptoConvert.ToInt32LE(blob, offset + 12); byte[] numArray = new byte[4]; Buffer.BlockCopy(blob, offset + 16, numArray, 0, 4); Array.Reverse(numArray); rSAParameter.Exponent = CryptoConvert.Trim(numArray); int num = offset + 20; int num1 = int32LE >> 3; rSAParameter.Modulus = new byte[num1]; Buffer.BlockCopy(blob, num, rSAParameter.Modulus, 0, num1); Array.Reverse(rSAParameter.Modulus); num += num1; int num2 = num1 >> 1; rSAParameter.P = new byte[num2]; Buffer.BlockCopy(blob, num, rSAParameter.P, 0, num2); Array.Reverse(rSAParameter.P); num += num2; rSAParameter.Q = new byte[num2]; Buffer.BlockCopy(blob, num, rSAParameter.Q, 0, num2); Array.Reverse(rSAParameter.Q); num += num2; rSAParameter.DP = new byte[num2]; Buffer.BlockCopy(blob, num, rSAParameter.DP, 0, num2); Array.Reverse(rSAParameter.DP); num += num2; rSAParameter.DQ = new byte[num2]; Buffer.BlockCopy(blob, num, rSAParameter.DQ, 0, num2); Array.Reverse(rSAParameter.DQ); num += num2; rSAParameter.InverseQ = new byte[num2]; Buffer.BlockCopy(blob, num, rSAParameter.InverseQ, 0, num2); Array.Reverse(rSAParameter.InverseQ); num += num2; rSAParameter.D = new byte[num1]; if (num + num1 + offset <= (int)blob.Length) { Buffer.BlockCopy(blob, num, rSAParameter.D, 0, num1); Array.Reverse(rSAParameter.D); } } catch (Exception exception) { throw new CryptographicException("Invalid blob.", exception); } RSA rSACryptoServiceProvider = null; try { rSACryptoServiceProvider = RSA.Create(); rSACryptoServiceProvider.ImportParameters(rSAParameter); } catch (CryptographicException cryptographicException) { bool flag = false; try { rSACryptoServiceProvider = new RSACryptoServiceProvider(new CspParameters() { Flags = CspProviderFlags.UseMachineKeyStore }); rSACryptoServiceProvider.ImportParameters(rSAParameter); } catch { flag = true; } if (flag) { throw; } } return(rSACryptoServiceProvider); }