1

Windows Metro 응용 프로그램에서 파일의 내용을 암호화하려고합니다. 문제의 파일은 로컬 (LocalState 폴더)에 장치에 저장되며 사용자가 쉽게 수정할 수 없도록하려는 긴 문자열을 포함합니다. 응용 프로그램은 대부분 대칭 키를 사용하여 파일을 암호화하고 암호 해독합니다.Windows 저장소 응용 프로그램에서 암호화되고 인증 된 데이터를 Windows 저장소 응용 프로그램의 파일에서 읽고 쓰십시오.

응용 프로그램이 키를 얻기 위해 금이 갈 수 있으므로이 보호 기능은 토론에 개방되어 있습니다. 그럼에도 불구하고, 사용자가 파일을 직접 수정/위조 할 수없는 한 그것은 저에게 허용됩니다. 인증 된 암호화가이를 수행하는 방법이라고 생각하지만이 주제에 대한 지식은 정확하지 않습니다.

나는 SymmetricKeyAlgorithmProviderEncryptedAndAuthenticatedData 클래스를 사용하여 윈도우 메트로 API를 사용하여 문자열을 암호화하기 위해 노력하고 오랜 시간을 보냈습니다. 그럼에도 불구하고 사용 사례 (Microsoft 또는 인터넷)는 거의 보이지 않고 거의 항상 데이터를 저장하지 않고 간단한 암호화 (비인증) 또는 인증을 수행합니다. 예를 들어, here 예는 데이터를 연속적으로 만 암호화하고 해독합니다. 사실, 몇몇 예제는 매번 무작위 키를 생성하는데, 나는 그것을 할 수 없다고 생각합니다. 당신이 볼 수 있듯이 심지어 물론 좋지 않은 상수 비표를 사용하고,

private EncryptedAndAuthenticatedData authenticatedEncryption(string strMsg, string strKey) 
{ 
    SymmetricKeyAlgorithmProvider objAlgProv = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesGcm); 
    IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8); 
    IBuffer buffKey = CryptographicBuffer.ConvertStringToBinary(strKey, BinaryStringEncoding.Utf8); 
    IBuffer buffNonce = CryptographicBuffer.CreateFromByteArray(new byte[]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); 
    CryptographicKey key = objAlgProv.CreateSymmetricKey(buffKey); 
    EncryptedAndAuthenticatedData objEncrypted = CryptographicEngine.EncryptAndAuthenticate(key, buffMsg, buffNonce, null); 
    return objEncrypted; 
} 

, 그러나 나는 다른 방법을 찾을 수 없습니다 :

내가 좋아하는 뭔가가있다. 내가 알지 못하는이 방법에는 다른 문제가있을 수 있습니다. 손이 암호화 방법으로

, 나는 다음 성공없이 (그 클래스의 객체가 직렬화 될 수 없음)으로, DataContractSerializer으로 EncryptedAndAuthenticatedData 객체를 직렬화하려고 노력하고, 나는 EncryptedAndAuthenticatedData 객체를 구축 할 수있는 방법을 찾을 수 없습니다 AuthenticationTagEncryptedData 속성 (파일에 기록 할 수 있다고 가정).

이 모든 것은 문자열을 올바르게 암호화하고 인증하는 방법을 찾지 못했음을 의미합니다. 나중에 파일을 읽고 해독 할 수있는 결과를 저장하는 것이 훨씬 적습니다 (나는 인증 된 암호 해독을위한 또 다른 방법이 있습니다. 같은 방식으로 키와 넌스를 사용합니다).

Windows Metro가 제공하는 클래스로이 작업을 수행 할 수 있는지, 어떻게 알 수 있습니까? 더 좋은 방법이 있습니까?

답변

2

그래서 내가 사용하기 쉬운 방법이 있다고 생각합니다 : DataProtectionProvider.

DataProtectionProvider는 컴퓨터 ID, 사용자 ID 및 패키지 ID의 조합에서 파생 된 키를 사용하여 주어진 byte array 또는 Stream을 대칭 적으로 암호화하는 클래스입니다. 그것은 사용하기 쉽고 아주 좋은 보호를 아주 쉽게 제공해야합니다.

샘플 문서

간단한 예제 제공 :이 경우

public async Task<IBuffer> SampleProtectAsync(
    String strMsg, 
    String strDescriptor, 
    BinaryStringEncoding encoding) 
{ 
    // Create a DataProtectionProvider object for the specified descriptor. 
    DataProtectionProvider Provider = new DataProtectionProvider(strDescriptor); 

    // Encode the plaintext input message to a buffer. 
    encoding = BinaryStringEncoding.Utf8; 
    IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding); 

    // Encrypt the message. 
    IBuffer buffProtected = await Provider.ProtectAsync(buffMsg); 

    // Execution of the SampleProtectAsync function resumes here 
    // after the awaited task (Provider.ProtectAsync) completes. 
    return buffProtected; 
} 

public async Task<String> SampleUnprotectData(
    IBuffer buffProtected, 
    BinaryStringEncoding encoding) 
{ 
    // Create a DataProtectionProvider object. 
    DataProtectionProvider Provider = new DataProtectionProvider(); 

    // Decrypt the protected message specified on input. 
    IBuffer buffUnprotected = await Provider.UnprotectAsync(buffProtected); 

    // Execution of the SampleUnprotectData method resumes here 
    // after the awaited task (Provider.UnprotectAsync) completes 
    // Convert the unprotected message from an IBuffer object to a string. 
    String strClearText = CryptographicBuffer.ConvertBinaryToString(encoding, buffUnprotected); 

    // Return the plaintext string. 
    return strClearText; 
} 

strDescriptor는 암호화 된 내용에 액세스 할 수 있도록하려는 사람에 대해 설명합니다. 기기에있는 사람이 있다면 값은 "LOCAL=machine"입니다. 지정된 사용자의 값은 "LOCAL=user"입니다.

MVC 또는 MVVM을 사용하는 경우 LocalStorageController과 같은 항목에 쉽게 추가 할 수 있으므로 모든 로컬 저장소가 앱을 떠나기 전에 자동으로 암호화/해독됩니다.

희망이 있다면 도움이되고 행복하게 코딩 할 수 있습니다.

+0

고마워요! 지금 제 코드가 작동합니다. 나는 이것으로 너무 많은 시간을 보냈고 결국 간단한 접근법이 생겼다. EncryptedAndAuthenticatedData가 파일 암호화에 적합하지 않다고 (또는 심지어 사용 가능하다고) 말하겠습니까? – CanisLupus

+0

또한 SampleProtectAsync 메서드가 "encoding = BinaryStringEncoding.Utf8"줄 때문에 인코딩으로 지정된 항목을 무시하므로 MSDN의 샘플에 작은 오류가있는 것으로 보입니다. 또한 하나의 메서드 이름은 "Async"로 끝나고 "Data"(다른 하나는 "DataAsync"로 끝나야합니다)에서 끝납니다. – CanisLupus

+0

예제 메서드 호출, 다른 사람들이 주석을 읽는 중 : 'IBuffer protectedData = SampleProtectAsync ("암호화 할 문자열", "LOCAL = machine", BinaryStringEncoding.Utf8), ' 및 '문자열 text = SampleUnprotectData (protectedData, BinaryStringEncoding.Utf8); 기다리고 있습니다. 파일에 IBuffer 데이터를 쓰거나 읽으려면 FileIO.WriteBufferAsync 및 FileIO.ReadBufferAsync를 사용할 수 있습니다. – CanisLupus

관련 문제