암호 암호화 루틴을 쓰고 있습니다. 내 문제를 설명하기 위해 아래 응용 프로그램을 작성했습니다. 약 20 %의 시간 동안이 코드는 예상대로 작동합니다. 나머지 시간에는 암호 해독에서 암호 예외가 발생합니다. "데이터가 유효하지 않습니다."ProtectedData.Protect intermittent failure
해독 부분이 매번 동일하게 작동하기 때문에 문제가 암호화 부분에 있다고 생각합니다. 즉, 암호화 루틴이 해독 루틴이 해독 할 수있는 값을 산출하는 경우, 해독 루틴은 해독 루틴을 해독 할 수 있습니다. 그러나 암호화 루틴이 암호 해독 루틴을 중단시키는 값을 생성하는 경우, 항상 칙칙합니다. 따라서 해독 루틴은 일관성이 있습니다. 암호화 루틴은 그렇지 않습니다.
유니 코드 인코딩을 사용하는 것이 잘못된 것 같지만 동일한 결과를 얻은 다른 사람이 시도했습니다.
내가 뭘 잘못하고 있니?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace DataProtectionTest
{
public partial class Form1 : Form
{
private static readonly byte[] entropy = { 1, 2, 3, 4, 1, 2, 3, 4 };
private string password;
public Form1()
{
InitializeComponent();
}
private void btnEncryptIt_Click(object sender, EventArgs e)
{
Byte[] pw = Encoding.Unicode.GetBytes(textBox1.Text);
Byte[] encryptedPw = ProtectedData.Protect(pw, entropy, DataProtectionScope.LocalMachine);
password = Encoding.Unicode.GetString(encryptedPw);
}
private void btnDecryptIt_Click(object sender, EventArgs e)
{
Byte[] pwBytes = Encoding.Unicode.GetBytes(password);
try
{
Byte[] decryptedPw = ProtectedData.Unprotect(pwBytes, entropy, DataProtectionScope.LocalMachine);
string pw = Encoding.Unicode.GetString(decryptedPw);
textBox2.Text = pw;
}
catch (CryptographicException ce)
{
textBox2.Text = ce.Message;
}
}
}
}
이 코드는 여전히 버그가 있습니다 ... 내 대답은 아래 참조하십시오. –
Base64 문자열로 변환하거나 Base64 문자열로 변환하는 동작이 내 문제를 해결했습니다. Previusly 유니 코드 문자열로 변환 중입니다. 정말 유용합니다. 감사 – rideintothesun