2013-03-26 34 views
8

私はOpenSSL形式の公開鍵ファイルと秘密鍵ファイルに正常に書きました。C#とBouncy CastleのRSA PrivateKeyを読んでください

ファイル:

-----BEGIN PUBLIC KEY----- 
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpHCHYgawzNlxVebSKXL7vfc/i 
hP+dQgMxlaPEi7/vpQtV2szHjIP34MnUKelXFuIETJjOgjWAjTTJoj38MQUWc3u7 
SRXaGVggqQEKH+cRi5+UcEObIfpi+cIyAm9MJqKabfJK2e5X/OS7FgAwPjgtDbZO 
ZxamOrWWL8KGB+lH+QIDAQAB 
-----END PUBLIC KEY----- 

-----BEGIN RSA PRIVATE KEY----- 
MIICXAIBAAKBgQCpHCHYgawzNlxVebSKXL7vfc/ihP+dQgMxlaPEi7/vpQtV2szH 
jIP34MnUKelXFuIETJjOgjWAjTTJoj38MQUWc3u7SRXaGVggqQEKH+cRi5+UcEOb 
Ifpi+cIyAm9MJqKabfJK2e5X/OS7FgAwPjgtDbZOZxamOrWWL8KGB+lH+QIDAQAB 
AoGBAIXtL6jFWVjdjlZrIl4JgXUtkDt21PD33IuiVKZNft4NOWLu+wp17/WZYn3S 
C2fbSXfaKZIycKi0K8Ab6zcUo0+QZKMoaG5GivnqqTPVAuZchkuMUSVgjGvKAC/D 
12/b+w+Shs9pvqED1CxfvtePXNwL6ZNuaREFC5hF/YpMVyg5AkEA3BUCZYJ+Ec96 
2cwsdY6HocW8Kn+RIqMjkNtyLA19cQV5mpIP7kAiW6drBDlraVANi+5AgK2zQ+ZT 
hYzs/JfRKwJBAMS1g5/B7XXnfC6VTRs8AMveZudi5wS/aGpaApybsfx1NTLLsm3l 
GmGTkbCr+EPzvJ5zRSIAHAA6N6NdORwzEWsCQHTli+JTD5dyNvScaDkAvbYFi06f 
d32IXYnBpcEUYT65A8BAOMn5ssYwBL23qf/ED431vLkcig1Ut6RGGFKKaQUCQEfa 
UdkSWm39/5N4f/DZyySs+YO90csfK8HlXRzdlnc0TRlf5K5VyHwqDkatmoMfzh9G 
1dLknVXL7jTjQZA2az8CQG0jRSQ599zllylMPPVibW98701Mdhb1u20p1fAOkIrz 
+BNEdOPqPVIyqIP830nnFsJJgTG2eKB59ym+ypffRmA= 
-----END RSA PRIVATE KEY----- 

と公開鍵はもちろんのちょうど公開鍵の部分が含まれています。

私のメッセージを公開鍵で暗号化した後。私は秘密鍵ファイル を読んでそれを解読したいが、それはうまくいかない。非対称キーにオブジェクトをキャストできないというプライベートキーを読み取ろうとして例外が発生します。ここで

は私のコードです:ここでは

public static AsymmetricKeyParameter ReadAsymmetricKeyParameter(string pemFilename) 
{ 
    var fileStream = System.IO.File.OpenText(pemFilename); 
    var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(fileStream); 
    var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject(); 
    return KeyParameter; 
} 

static void Encrypt2(string publicKeyFileName, string inputMessage, string encryptedFileName) 
    { 
     UTF8Encoding utf8enc = new UTF8Encoding(); 
     FileStream encryptedFile = null; 

     try 
     { 
      // Converting the string message to byte array 
      byte[] inputBytes = utf8enc.GetBytes(inputMessage); 

      // RSAKeyPairGenerator generates the RSA Key pair based on the random number and strength of key required 
      /*RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator(); 
      rsaKeyPairGnr.Init(new Org.BouncyCastle.Crypto.KeyGenerationParameters(new SecureRandom(), 512)); 
      Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair(); 
      */ 

      AsymmetricKeyParameter publicKey = ReadAsymmetricKeyParameter(publicKeyFileName); 

      // Creating the RSA algorithm object 
      IAsymmetricBlockCipher cipher = new RsaEngine(); 

      // Initializing the RSA object for Encryption with RSA public key. Remember, for encryption, public key is needed 
      cipher.Init(true, publicKey); 

      //Encrypting the input bytes 
      byte[] cipheredBytes = cipher.ProcessBlock(inputBytes, 0, inputMessage.Length); 

      //Write the encrypted message to file 
      // Write encrypted text to file 
      encryptedFile = File.Create(encryptedFileName); 
      encryptedFile.Write(cipheredBytes, 0, cipheredBytes.Length); 
     } 
     catch (Exception ex) 
     { 
      // Any errors? Show them 
      Console.WriteLine("Exception encrypting file! More info:"); 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 
      // Do some clean up if needed 
      if (encryptedFile != null) 
      { 
       encryptedFile.Close(); 
      } 
     } 

    } 

は、復号化機能です。 2つ目はBouncy Castleを使用しないことですが、Bouncy Castleを使用するのは後でJavaで暗号化と復号化を行うためです。

static void Decrypt2(string privateKeyFileName, string encryptedFileName, string plainTextFileName) 
    { 
     UTF8Encoding utf8enc = new UTF8Encoding(); 
     FileStream encryptedFile = null; 
     StreamWriter plainFile = null; 
     byte[] encryptedBytes = null; 
     string plainText = ""; 

     try 
     { 
      // Converting the string message to byte array 
      //byte[] inputBytes = utf8enc.GetBytes(inputMessage); 

      // RSAKeyPairGenerator generates the RSA Key pair based on the random number and strength of key required 
      /*RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator(); 
      rsaKeyPairGnr.Init(new Org.BouncyCastle.Crypto.KeyGenerationParameters(new SecureRandom(), 512)); 
      Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair(); 
      */ 


      StreamReader sr = File.OpenText(privateKeyFileName); 
      PemReader pr = new PemReader(sr); 

      PemReader pemReader = new PemReader(new StringReader(privateKeyFileName)); 
      AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject(); 
      Console.WriteLine(keyPair.ToString()); 
      AsymmetricKeyParameter privatekey = keyPair.Private; 


      Console.WriteLine(pr.ReadPemObject()); 
      AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject(); 

      AsymmetricKeyParameter privateKey = ReadAsymmetricKeyParameter(privateKeyFileName); 

      // Creating the RSA algorithm object 
      IAsymmetricBlockCipher cipher = new RsaEngine(); 
      Console.WriteLine("privateKey: " + privateKey.ToString()); 

      // Initializing the RSA object for Decryption with RSA private key. Remember, for decryption, private key is needed 
      //cipher.Init(false, KeyPair.Private); 
      //cipher.Init(false, KeyPair.Private); 
      cipher.Init(false, keyPair.Private);  

      // Read encrypted text from file 
      encryptedFile = File.OpenRead(encryptedFileName); 
      encryptedBytes = new byte[encryptedFile.Length]; 
      encryptedFile.Read(encryptedBytes, 0, (int)encryptedFile.Length); 

      //Encrypting the input bytes 
      //byte[] cipheredBytes = cipher.ProcessBlock(inputBytes, 0, inputMessage.Length); 
      byte[] cipheredBytes = cipher.ProcessBlock(encryptedBytes, 0, encryptedBytes.Length); 

      //Write the encrypted message to file 
      // Write encrypted text to file 
      plainFile = File.CreateText(plainTextFileName); 
      plainText = Encoding.Unicode.GetString(cipheredBytes); 
      plainFile.Write(plainText); 
     } 
     catch (Exception ex) 
     { 
      // Any errors? Show them 
      Console.WriteLine("Exception encrypting file! More info:"); 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 
      // Do some clean up if needed 
      if (plainFile != null) 
      { 
       plainFile.Close(); 
      } 
      if (encryptedFile != null) 
      { 
       encryptedFile.Close(); 
      } 
     } 
    } 


    // Decrypt a file 
    static void Decrypt(string privateKeyFileName, string encryptedFileName, string plainFileName) 
    { 
     // Variables 
     CspParameters cspParams = null; 
     RSACryptoServiceProvider rsaProvider = null; 
     StreamReader privateKeyFile = null; 
     FileStream encryptedFile = null; 
     StreamWriter plainFile = null; 
     string privateKeyText = ""; 
     string plainText = ""; 
     byte[] encryptedBytes = null; 
     byte[] plainBytes = null; 

     try 
     { 
      // Select target CSP 
      cspParams = new CspParameters(); 
      cspParams.ProviderType = 1; // PROV_RSA_FULL 
      //cspParams.ProviderName; // CSP name 
      rsaProvider = new RSACryptoServiceProvider(cspParams); 

      // Read private/public key pair from file 
      privateKeyFile = File.OpenText(privateKeyFileName); 
      privateKeyText = privateKeyFile.ReadToEnd(); 

      // Import private/public key pair 
      rsaProvider.FromXmlString(privateKeyText); 

      // Read encrypted text from file 
      encryptedFile = File.OpenRead(encryptedFileName); 
      encryptedBytes = new byte[encryptedFile.Length]; 
      encryptedFile.Read(encryptedBytes, 0, (int)encryptedFile.Length); 

      // Decrypt text 
      plainBytes = rsaProvider.Decrypt(encryptedBytes, false); 

      // Write decrypted text to file 
      plainFile = File.CreateText(plainFileName); 
      plainText = Encoding.Unicode.GetString(plainBytes); 
      plainFile.Write(plainText); 
     } 
     catch (Exception ex) 
     { 
      // Any errors? Show them 
      Console.WriteLine("Exception decrypting file! More info:"); 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 
      // Do some clean up if needed 
      if (privateKeyFile != null) 
      { 
       privateKeyFile.Close(); 
      } 
      if (encryptedFile != null) 
      { 
       encryptedFile.Close(); 
      } 
      if (plainFile != null) 
      { 
       plainFile.Close(); 
      } 
     } 

    } // Decrypt 

ご協力いただければ幸いです!

+2

static AsymmetricKeyParameter readPrivateKey(string privateKeyFileName) { AsymmetricCipherKeyPair keyPair; using (var reader = File.OpenText(privateKeyFileName)) keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject(); return keyPair.Private; } 

次に、このキーは、以下のようにデータを復号化するために使用することができますそれほど言いませんか? –

+0

いいえ、それは申し訳ありません。非対称キーにオブジェクトをキャストできないというプライベートキーを読み取ろうとして例外が発生します。私は、公開鍵ReadAsymmeyricKeyParameterを読み取る関数を持っていますが、何らかの理由で私有鍵のために働いていません。 – c0d3Junk13

+0

解読メソッドでpemReader.ReadObject()から実際に返されるオブジェクトのタイプは何ですか? –

答えて

12

私はこれを理解しました。基本的にはBouncyCastleを使用してプライベートopensslのキーを読み、C#が、このようなものですします実際にはありません「それは働いていない」

AsymmetricKeyParameter key = readPrivateKey(pemFilename);  
RsaEngine e = new RsaEngine();  
e.Init(false, key); 

byte[] decipheredBytes = e.ProcessBlock(cipheredData, 0, cipheredData.Length); 
関連する問題