2014-01-25 307 views
3

我將一些Java代碼移植到C#,但它看起來好像Java和C#之間的公鑰填充不一致。X509證書公鑰加密

這裏是我的Java代碼:

package Encryption; 
import java.security.cert.X509Certificate; 
import java.security.interfaces.RSAPublicKey; 
import java.io.ByteArrayInputStream; 
import java.security.cert.CertificateException; 
import java.security.cert.CertificateFactory; 
import javax.xml.bind.DatatypeConverter; 
/** 
* 
* @author Cameron 
*/ 
public class Encryptiontest { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 

     String publicKey = "MIIGdzCCBV+gAwIBAgIKEpMtNAAIAAKN5TANBgkqhkiG9w0BAQUFADCBizETMBEGCgmSJomT8ixkARkWA2NvbTEZMBcGCgmSJomT8ixkARkWCW1pY3Jvc29mdDEUMBIGCgmSJomT8ixkARkWBGNvcnAxFzAVBgoJkiaJk/IsZAEZFgdyZWRtb25kMSowKAYDVQQDEyFNaWNyb3NvZnQgU2VjdXJlIFNlcnZlciBBdXRob3JpdHkwHhcNMTIwNjA1MTY1OTMzWhcNMTQwNTE5MjIyMzMwWjB1MQswCQYDVQQGEwJVUzELMAkGA1UECBMCV0ExEDAOBgNVBAcTB1JlZG1vbmQxEjAQBgNVBAoTCU1pY3Jvc29mdDESMBAGA1UECxMJWGJveCBMaXZlMR8wHQYDVQQDExZ4cGFzc3BvcnQueGJveGxpdmUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvUihVNnWYpu3uJmcLy+PBecKu4ziVD7OIeZ/V+tJkXbc5+6OW8G+QDtJKuJkkuxGNLBNmLHbCyXsJ/US3kKkU7/7yK7jfWRNdqAKJdDTVxsWnxlo+/28ScGrAV6wK2bbK8GQBpsYRn1HKGCGceWIBCSqUfI7rwgwDnvqcW5PeivORd4+or5DdhgUMwiV5Vr2fvdcAiQR1CKgMphxO4+OmZ4khpB/HT/xS4FscvfFsSBLM37jBMrnhY5yNKPeHZB2eYvehnnw22NFHJNksa+vVFXL9aJc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GAQUFBwMBMA0GCSqGSIb3DQEBBQUAA4IBAQBhJKcKn7h3/pZK2A5wk6lonJMyra4u3bFLzfvg0HWWiGx+vWCbBA2EJoar8V1yCMKezAQOcJWDKzO5HzY9m+PGm7FZtoYZWmCaYt2+Hlt/X7py3Nhgey/xYBTXb1rnu9jhk84+lG4dSAyyTYrsCgrCG5vsqGM4ZZz7FGOhUWQ4QAZ36vgSoLpOy6/6xpaWor4ritklCmHYYGpyuUuZLBt/Tu44ng2rh98hyxMNgaBfZ1cpqnMyYatWIgPPg8DEuNF4iGaujIFQfrU2VuyiUvFWLA9H5vfVLh5CBYR7qqRda0g4p22FDxdfmLu4Q2iZg+rbGxu9+g/9AdAXvhMEjvqC"; 
     ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(publicKey)); 
     X509Certificate x509certificate; 
     try 
     { 
      x509certificate = (X509Certificate)CertificateFactory.getInstance("X.509").generateCertificate(bytearrayinputstream); 
      RSAPublicKey key = (RSAPublicKey)x509certificate.getPublicKey(); 
      System.out.println(DatatypeConverter.printBase64Binary(key.getEncoded())); 
     } 
     catch (CertificateException certificateexception) 
     { 
     } 
    } 

} 

當我打印RSAPublicKey的Base64表示,這就是我得到:

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvUihVNnWYpu3uJmcLy + PBecKu4ziVD7OIeZ/V + tJkXbc5 + 6OW8G + QDtJKuJkkuxGNLBNmLHbCyXsJ/US3kKkU7/7yK7jfWRNdqAKJdDTVxsWnxlo +/+ 28ScGrAV6wK2bbK8GQBpsYRn1HKGCGceWIBCSqUfI7rwgwDnvqcW5PeivORd4 + or5DdhgUMwiV5Vr2fvdcAiQR1CKgMphxO4 OmZ4khpB/HT/xS4FscvfFsSBLM37jBMrnhY5yNKPeHZB2eYvehnnw22NFHJNksa + vVFXL9aJcZWJc/bqqlhlhL8eLdYSR/KA006PSInW8yWtd4IFVKJ1Moa41gCUZL8 1voQIDAQAB

但是,結果與具有相同base64公鑰的C#不同。這是我的C#代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Security.Cryptography.X509Certificates; 
using System.Text; 
using System.Threading.Tasks; 

namespace EncryptionTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      String publicKey = "MIIGdzCCBV+gAwIBAgIKEpMtNAAIAAKN5TANBgkqhkiG9w0BAQUFADCBizETMBEGCgmSJomT8ixkARkWA2NvbTEZMBcGCgmSJomT8ixkARkWCW1pY3Jvc29mdDEUMBIGCgmSJomT8ixkARkWBGNvcnAxFzAVBgoJkiaJk/IsZAEZFgdyZWRtb25kMSowKAYDVQQDEyFNaWNyb3NvZnQgU2VjdXJlIFNlcnZlciBBdXRob3JpdHkwHhcNMTIwNjA1MTY1OTMzWhcNMTQwNTE5MjIyMzMwWjB1MQswCQYDVQQGEwJVUzELMAkGA1UECBMCV0ExEDAOBgNVBAcTB1JlZG1vbmQxEjAQBgNVBAoTCU1pY3Jvc29mdDESMBAGA1UECxMJWGJveCBMaXZlMR8wHQYDVQQDExZ4cGFzc3BvcnQueGJveGxpdmUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvUihVNnWYpu3uJmcLy+PBecKu4ziVD7OIeZ/V+tJkXbc5+6OW8G+QDtJKuJkkuxGNLBNmLHbCyXsJ/US3kKkU7/7yK7jfWRNdqAKJdDTVxsWnxlo+/28ScGrAV6wK2bbK8GQBpsYRn1HKGCGceWIBCSqUfI7rwgwDnvqcW5PeivORd4+or5DdhgUMwiV5Vr2fvdcAiQR1CKgMphxO4+OmZ4khpB/HT/xS4FscvfFsSBLM37jBMrnhY5yNKPeHZB2eYvehnnw22NFHJNksa+vVFXL9aJcZWJc/bqqlhlhL8eLdYSR/KA006PSInW8yWtd4IFVKJ1Moa41gCUZL81voQIDAQABo4IC8DCCAuwwRAYJKoZIhvcNAQkPBDcwNTAOBggqhkiG9w0DAgICAIAwDgYIKoZIhvcNAwQCAgC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2+Hlt/X7py3Nhgey/xYBTXb1rnu9jhk84+lG4dSAyyTYrsCgrCG5vsqGM4ZZz7FGOhUWQ4QAZ36vgSoLpOy6/6xpaWor4ritklCmHYYGpyuUuZLBt/Tu44ng2rh98hyxMNgaBfZ1cpqnMyYatWIgPPg8DEuNF4iGaujIFQfrU2VuyiUvFWLA9H5vfVLh5CBYR7qqRda0g4p22FDxdfmLu4Q2iZg+rbGxu9+g/9AdAXvhMEjvqC"; 
      byte[] data = Convert.FromBase64String(publicKey); 

      X509Certificate x509certificate = new X509Certificate(data); 
      byte[] rsaPublicKey = x509certificate.GetPublicKey(); 

      string base64 = Convert.ToBase64String(rsaPublicKey); 

      Console.WriteLine(base64); 
      Console.ReadKey(); 
     } 
    } 
} 

C#輸出:

MIIBCgKCAQEAvUihVNnWYpu3uJmcLy + PBecKu4ziVD7OIeZ/V + tJkXbc5 + 6OW8G + QDtJKuJkkuxGNLBNmLHbCyXsJ/US3kKkU7/7yK7jfWRNdqAKJdDTVxsWnxlo +/28ScGrAV6wK2bbK8GQBpsYRn1HKGCGceWIBCSqUfI7rwgwDnvqcW5PeivORd4 + or5DdhgUMwiV5Vr2fvdcAiQR1CKgMphxO4 + OmZ4khpB/HT/xS4FscvfFsSBLM37jBMrnhY5yNKPeHZB2eYvehnnw22NFHJNksa + vVFXL9aJcZWJc/bqqlhlhL8eLdYSR/KA006PSInW8yWtd4IFVKJ1Moa41gCUZL81voQIDAQAB

爲什麼是兩個相似的,但墊戴德不同?我如何使C#輸出看起來像Java輸出?

回答

3

Java輸出是ASN.1 SubjectPublicKeyInfo的DER編碼,如RFC 5280, section 4.1中所述。 C#輸出是ASN.1 RSAPublicKey的DER編碼,因爲許多地方都包含RFC 2313, section 7.

Here是一個易於使用的在線ASN.1解碼器,我覺得很方便。您可以將base64直接粘貼到表單中並將其解碼。

我不是一個真正的.NET專家,但打獵周圍我想你可能得到的東西更接近,如果不相同,通過作爲替代使用X509Certificate2類。你可以construct an X509Certificate2 object from an X509Certificate object。 X509Certificate2類包含您可以訪問的PublicKey屬性。該對象似乎有一個EncodedKeyValue,並且在這個看似無限的鏈條之後可以獲得原始數據。換句話說,就像:

X509Certificate2 x509certificate2 = new X509Certificate2(data); 
byte[] rsaPublicKey = x509certificate2.PublicKey.EncodedKeyValue.RawData; 

應該工作。但我沒有測試過它。

+0

好像'PublicKey.EncodedKeyValue.RawData'是相同的字節數組作爲取之於'返回的getpublickey()'。任何其他想法? –

+0

@CameronTinker:我想知道爲什麼我認爲它會有所不同?我會嘗試一些實驗。 –

+0

沒問題@GregS。我實際上能夠成功移植我的Java代碼。上面的代碼只是一個片段。我正在使用'X509Certificate'生成一個RSA公鑰,用於加密一些數據。我使用'x509certificate.PublicKey.Key'作爲輸入參數傳遞給'RSAOAEPKeyExchangeFormatter'來做密鑰交換。我更改爲使用X509Certificate2,因爲PublicKey僅在該類上可用。感謝您讓我走上正軌! –