2012-12-03 2 views
3

PgpDecrypt라는 클래스를 사용하여 클라이언트가 제공 한이 샘플 파일의 암호를 해독하려고했습니다. 코드가이 선에 올 때 :BouncyCastle PGP를 사용하여 파일의 암호 해독에서 예외가 발생했습니다.

Stream clear = pbe.GetDataStream(privKey); 

는 오류를 반환 : 예외가 해독 된 비밀 키

여기 내 암호 해독 코드입니다 : 내 세 번째로 BouncyCastle을 사용하고

PgpDecrypt test = new PgpDecrypt(string.Concat(pathh, "TestDecryptionFile"), 
              string.Concat(pathh, "mypgpprivatekey.key"), 
              "mypassphrase", 
              @"d:/test/", 
              string.Concat(pathh, "clientpublickey.key")); 

FileStream fs = File.Open(string.Concat(pathh, "TestDecryptionFile"), FileMode.Open); 
test.Decrypt(fs, @"d:\test\"); 

.NET 용 파티 라이브러리

이 문제를 해결할 수있는 아이디어는 큰 도움이 될 것입니다. 미리 감사드립니다! 의 PgpEncryptionKeys 클래스 내부에 여전히

/// <summary> 
/// Return the last key we can use to decrypt. 
/// Note: A file can contain multiple keys (stored in "key rings") 
/// </summary> 
private PgpSecretKey GetLastSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle) 
{ 
    return (from PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings() 
      select kRing.GetSecretKeys().Cast<PgpSecretKey>() 
              .LastOrDefault(k => k.IsSigningKey)) 
              .LastOrDefault(key => key != null); 
} 

ReadSecretKey을 확인하십시오

당신은 BouncyCastle 클래스 PGPEncrypt, PGPDecrypt 및 PGPEncryptionKeys 다음과 같은 경우
+1

'pathh '는 어디에 할당되어 있습니까? –

+0

파일이있는 로컬 경로 ... @ "D : \ Users \ MyUser \ Documents \ Visual Studio 2008 \ Projects \ sFTPwithPGP \ keys \ keys26112012 \" – iceheaven31

+1

경로를 작성할 때 안전을 추가하려면 [Path.Combine] (http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx). –

답변

5

... PGPEncryptionKeys 클래스에서

은이 방법을 추가 방법은 다음과 같습니다

private PgpSecretKey ReadSecretKey(string privateKeyPath, bool toEncrypt) 
{ 
    using (Stream keyIn = File.OpenRead(privateKeyPath)) 
    using (Stream inputStream = PgpUtilities.GetDecoderStream(keyIn)) 
    { 
     PgpSecretKeyRingBundle secretKeyRingBundle = new PgpSecretKeyRingBundle(inputStream); 
     PgpSecretKey foundKey = toEncrypt ? GetFirstSecretKey(secretKeyRingBundle) : GetLastSecretKey(secretKeyRingBundle); 

     if (foundKey != null) 
      return foundKey; 
    } 
    throw new ArgumentException("Can't find signing key in key ring."); 
} 

^_^

+1

완벽하게 일했습니다. 이 문제를 겪는 사람은 다음과 같이 생성자를 수정하는 것을 잊지 마세요. public PgpEncryptionKeys (string publicKeyPath, string privateKeyPath, string passPhrase, bool toEncrypt) ' – Corwin01

+0

감사합니다. 나는 또한 당신을 도왔다 니 기쁘다. :) – iceheaven31

+1

예프. 완벽하게 작동하고 있습니다 =) – Corwin01

관련 문제