2017-01-19 10 views
0

私は現在PCLCryptoで作業しており、System.Security.Cryptography.RSACryptoServiceProvider SignData関数を模倣する方法を探しています。現在、私が作成するには、このコードを使用しています:Xamarin PCLCrypto RSACryptoServiceProvider SignData相当の

var mac = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaSignPkcs1Sha512);    
var cryptoKey = mac.CreateKeyPair(2048); 
var hash = WinRTCrypto.CryptographicEngine.Sign(cryptoKey, input); 
return WinRTCrypto.CryptographicBuffer.EncodeToBase64String(hash); 

これは私が署名を検証するために使用していたコードです:

var mac = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaSignPkcs1Sha512); 
var keyMaterial = WinRTCrypto.CryptographicBuffer.ConvertStringToBinary(key, Encoding.UTF8); 
var cryptoKey = mac.ImportPublicKey(keyMaterial, CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo); 
return WinRTCrypto.CryptographicEngine.VerifySignature(cryptoKey, data, signature); 

明確にするために、私は私が正しいを使用していますことを100%確信していますキーペア。私のキーは、次の形式になります。

<RSAKeyValue><Modulus>nMhF8TRjT5O7tTtqr1//9ahokRuNGRxdGwc7fwk+i21Zscr/7L0PlfiE/sTQC/VQrj/BHhkX8CXVMTw1ukSN7zZDD7UCbdvhmV7jhPs/TDJP70Y4pgcG624WnQXjWDgR5f7Mbfg18zsevidtGukK+U5huaBfxhxg2Za3X3JzUYc=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; 

誰かがRSACryptoServiceProviderと同じ出力を生成した例に私を指すもらえますか?

答えて

1

私は実際に自分で考え出しました。明らかに、xml形式で指定されたキーをインポートする際に問題がありました。現在の希望の結果を生むソリューション:

public static class CryptographyExtensions 
{ 
    private static readonly String ModulusStartString = "<Modulus>"; 
    private static readonly String ModulusEndString = "</Modulus>"; 
    private static readonly String ExponentStartString = "<Exponent>"; 
    private static readonly String ExponentEndString = "</Exponent>"; 
    private static readonly String PStartString = "<P>"; 
    private static readonly String PEndString = "</P>"; 
    private static readonly String QStartString = "<Q>"; 
    private static readonly String QEndString = "</Q>"; 
    private static readonly String DPStartString = "<DP>"; 
    private static readonly String DPEndString = "</DP>"; 
    private static readonly String DQStartString = "<DQ>"; 
    private static readonly String DQEndString = "</DQ>"; 
    private static readonly String DStartString = "<D>"; 
    private static readonly String DEndString = "</D>"; 
    private static readonly String InverseQStartString = "<InverseQ>"; 
    private static readonly String InverseQEndString = "</InverseQ>"; 


    public static RSAParameters ConvertPrivateKeyFromString(String privateKey) 
    { 
     var rsaparams = new RSAParameters() 
     { 
      Modulus = Convert.FromBase64String(privateKey.Substring(ModulusStartString, ModulusEndString)), 
      Exponent = Convert.FromBase64String(privateKey.Substring(ExponentStartString, ExponentEndString)), 
      P = Convert.FromBase64String(privateKey.Substring(PStartString, PEndString)), 
      Q = Convert.FromBase64String(privateKey.Substring(QStartString, QEndString)), 
      DP = Convert.FromBase64String(privateKey.Substring(DPStartString, DPEndString)), 
      DQ = Convert.FromBase64String(privateKey.Substring(DQStartString, DQEndString)), 
      D = Convert.FromBase64String(privateKey.Substring(DStartString, DEndString)), 
      InverseQ = Convert.FromBase64String(privateKey.Substring(InverseQStartString, InverseQEndString)) 
     }; 
     return rsaparams; 
    } 
    public static RSAParameters ConvertPublicKeyFromString(String publicKey) 
    { 
     var rsaparams = new RSAParameters() 
     { 
      Modulus = Convert.FromBase64String(publicKey.Substring(ModulusStartString, ModulusEndString)), 
      Exponent = Convert.FromBase64String(publicKey.Substring(ExponentStartString, ExponentEndString)) 
     }; 
     return rsaparams; 
    } 

public static class RsaEncryption 
{ 
    public static string HashAndSign(byte[] input, string key) 
    { 
     var mac = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaSignPkcs1Sha512); 
     var cryptoKey = mac.ImportParameters(CryptographyExtensions.ConvertPrivateKeyFromString(key)); 
     var hash = WinRTCrypto.CryptographicEngine.Sign(cryptoKey, input); 
     return WinRTCrypto.CryptographicBuffer.EncodeToBase64String(hash); 
    } 

    public static bool VerifySignature(byte[] data, byte[] signature, string key) 
    { 
     var mac = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaSignPkcs1Sha512); 
     var cryptoKey = mac.ImportParameters(CryptographyExtensions.ConvertPublicKeyFromString(key)); 
     return WinRTCrypto.CryptographicEngine.VerifySignature(cryptoKey, data, signature); 
    } 
} 
public static class StringExtensions 
{ 
    public static string Substring(this string @this, string from = null, string until = null, StringComparison comparison = StringComparison.OrdinalIgnoreCase) 
    { 
     var fromLength = (from ?? string.Empty).Length; 
     var startIndex = !string.IsNullOrEmpty(from) 
      ? @this.IndexOf(from, comparison) + fromLength 
      : 0; 

     if (startIndex < fromLength) { throw new ArgumentException("from: Failed to find an instance of the first anchor"); } 

     var endIndex = !string.IsNullOrEmpty(until) 
     ? @this.IndexOf(until, startIndex, comparison) 
     : @this.Length; 

     if (endIndex < 0) { throw new ArgumentException("until: Failed to find an instance of the last anchor"); } 

     var subString = @this.Substring(startIndex, endIndex - startIndex); 
     return subString; 
    } 
} 
関連する問題