2016-06-30 37 views
0

我有一個問題,在RSAParameter中設置模參數。 我轉換我的公鑰字符串在字節數組中,我的問題是長度太長。設置模數RSA參數從字符串公鑰

byte[] lExponent = { 1, 0, 1 }; 

//Create a new instance of the RSACryptoServiceProvider class. 
RSACryptoServiceProvider lRSA = new RSACryptoServiceProvider(); 


//Create a new instance of the RSAParameters structure. 
RSAParameters lRSAKeyInfo = new RSAParameters(); 

//Set RSAKeyInfo to the public key values. 
string KeyString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCV/eUrmhIZul32nN41sF0y/k4detUxPTQngHFQGOoQNCRa84+2mGdCAg3EN9DPsUtCSHjscfp5xC9otgZsj13Rn7atbGZhJn5eZpIzPZV/psfeueL0Idq7b1msyBNG8dqR0WblYvzSY8uWwIIWyOkrQvtUwHJoxrBD4iLO/NEvzQIDAQAB"; 
PublicKey = Convert.FromBase64String(KeyString); 


lRSAKeyInfo.Modulus = PublicKey; 
lRSAKeyInfo.Exponent = lExponent; 

lRSA.ImportParameters(lRSAKeyInfo); 

return Convert.ToBase64String(lRSA.Encrypt(InputStringbytes, false)); 

問題是我的密鑰大小是1296而不是1024.我用XMLParameter字符串進行了測試,但我遇到了同樣的問題。

我需要幫助。感謝每一個前進

回答

0

我不明白,如果你是說有問題的原始公鑰或者你認爲它是正確的,你的代碼不工作。我用下面(BouncyCastle的庫)進行加密:

public string PublicKeyEncrypt(string plaintext, Stream publickey) 
{ 
    try 
    { 
     var rsaKeyParameters = (RsaKeyParameters)PublicKeyFactory.CreateKey(publickey); 
     var rsaParameters = new RSAParameters(); 
     rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(); 
     rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned(); 
     var rsa = new RSACryptoServiceProvider(); 
     rsa.ImportParameters(rsaParameters); 
     return Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes(plaintext), true)); 
    } 
    catch (Exception e) 
    { 
     // Whatever 
    } 
} 

如果你需要用一個字節數組的鍵來調用它:

public string PublicKeyEncrypt(string plaintext, byte[] publickey) 
{ 
    return PublicKeyEncrypt(plaintext, new MemoryStream(publickey, false)); 
} 
+0

通常情況下,公鑰是好的,因爲與庫中的JavaScript公鑰工作(https://github.com/travist/jsencrypt)。我想不要使用他的庫,但在.NET中實現加密。對我來說,這是我的代碼不起作用。 – Christophe

0

你的字符串長度216個字符,這意味着它代表162個字節。 162字節是1296位。

所以程序似乎正在做你剛剛告訴它做的事情(模數的長度(以位爲單位)是RSA密鑰的大小)。

因此,您在那裏的字符串不代表RSA 1024位模數值,您必須複製了錯誤的數據值。

+0

我已經看到了。我認爲鑰匙串有問題。我沒有複製錯誤的數據。我有ujst削減containt的原始字符串----- BEGIN PUBLIC KEY -----和----- END PUBLIC KEY ----- – Christophe

+0

在我的項目中,我需要加密密碼才能使用開放AM的SSO lgin。實際上它已經與這個公鑰一起使用,並且密碼使用JSENCRYPT.JS加密。沒有問題。這是我在c#開發中遇到問題的原因。# – Christophe

+0

對於目前我將嘗試生成另一個公鑰並測試 – Christophe

0

您的KeyString是一個base64編碼的DER編碼的SubjectPublicKeyInfo對象,事實上它包含1024位RSA模量。要親自看看,您可以使用lapo.it base64 ASN.1 decoder。只需複製並粘貼base64字符串並單擊解碼即可。

在Java中,此格式(不帶base64編碼)由PublicKey.getEncoded()方法返回。

在stackoverflow上有各種答案試圖處理這個問題。在this answerbouncycastle C# library被使用,並且下面的C#片段被提供:

byte[] publicKeyBytes = Convert.FromBase64String(publicKeyString); 
AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes); 
RsaKeyParameters rsaKeyParameters = (RsaKeyParameters) asymmetricKeyParameter; 
RSAParameters rsaParameters = new RSAParameters(); 
rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(); 
rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned(); 
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 
rsa.ImportParameters(rsaParameters); 

This answer包含的碼較長的序列,但是使用BouncyCastle的C#庫避免。

我還沒有測試過其中之一。

0

這是ce解決方案在DER編碼的blob公鑰中的模數。

private string Encrypt(string pPublicKey, string pInputString) 
    { 
     //Create a new instance of the RSACryptoServiceProvider class. 
     RSACryptoServiceProvider lRSA = new RSACryptoServiceProvider(); 

     //Import key parameters into RSA. 
     lRSA.ImportParameters(GetRSAParameters(pPublicKey)); 

     return Convert.ToBase64String(lRSA.Encrypt(Encoding.UTF8.GetBytes(pInputString), false)); 
    } 

    private static RSAParameters GetRSAParameters(string pPublicKey) 
    { 
     byte[] lDer; 

     //Set RSAKeyInfo to the public key values. 
     int lBeginStart = "-----BEGIN PUBLIC KEY-----".Length; 
     int lEndLenght = "-----END PUBLIC KEY-----".Length; 
     string KeyString = pPublicKey.Substring(lBeginStart, (pPublicKey.Length - lBeginStart - lEndLenght)); 
     lDer = Convert.FromBase64String(KeyString); 


     //Create a new instance of the RSAParameters structure. 
     RSAParameters lRSAKeyInfo = new RSAParameters(); 

     lRSAKeyInfo.Modulus = GetModulus(lDer); 
     lRSAKeyInfo.Exponent = GetExponent(lDer); 

     return lRSAKeyInfo; 
    } 

    private static byte[] GetModulus(byte[] pDer) 
    { 
     //Size header is 29 bits 
     //The key size modulus is 128 bits, but in hexa string the size is 2 digits => 256 
     string lModulus = BitConverter.ToString(pDer).Replace("-", "").Substring(58, 256); 

     return StringHexToByteArray(lModulus); 
    } 

    private static byte[] GetExponent(byte[] pDer) 
    { 
     int lExponentLenght = pDer[pDer.Length - 3]; 
     string lExponent = BitConverter.ToString(pDer).Replace("-", "").Substring((pDer.Length * 2) - lExponentLenght * 2, lExponentLenght * 2); 

     return StringHexToByteArray(lExponent); 
    }  

    public static byte[] StringHexToByteArray(string hex) 
    { 
     return Enumerable.Range(0, hex.Length) 
         .Where(x => x % 2 == 0) 
         .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) 
         .ToArray(); 
    } 

謝謝您的幫助