나는 블로우 피시 알고리즘으로 파일을 암호화하고 있지만, 그것에 대해 뭔가를 모르는 것 같습니다. Encipher()
을 시도 할 때마다 '잘못된 길이'라는 예외가 throw됩니다. 8로 mod를 얻었을 때 길이가 0이어야한다는 것을 알았고 encipher를 시작하기 위해 8 x 8 블록의 스트림이 있어야 함을 의미한다고 생각합니다. 어떻게해야합니까? 복어의블로우 피쉬 길이가 잘못됨
, 인사이 방법 :
public void Encipher(byte[] data, int length)
{
uint xl, xr;
if ((length % 8) != 0) <-- Exception Line
throw new Exception("Invalid Length");
for (int i = 0; i < length; i += 8)
{
// Encode the data in 8 byte blocks.
xl = (uint)((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]);
xr = (uint)((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]);
Encipher(ref xl, ref xr);
// Now Replace the data.
data[i] = (byte)(xl >> 24);
data[i + 1] = (byte)(xl >> 16);
data[i + 2] = (byte)(xl >> 8);
data[i + 3] = (byte)(xl);
data[i + 4] = (byte)(xr >> 24);
data[i + 5] = (byte)(xr >> 16);
data[i + 6] = (byte)(xr >> 8);
data[i + 7] = (byte)(xr);
}
}
내 암호화 방법 :
private void EncryptFile(string szFilePath, string szInfoFile, string szKey, string szEncryptedFile = "")
{
// Blowfish
Blowfish alg = new Blowfish(Encoding.Unicode.GetBytes(szKey));
// Open file
System.IO.FileStream originalStream = System.IO.File.OpenRead(szFilePath);
// Store original file length
long originalLength = originalStream.Length;
System.IO.File.WriteAllText(szInfoFile, originalLength.ToString());
Byte[] buffer = new byte[originalStream.Length + (originalStream.Length % 8)];
originalStream.Read(buffer, 0, buffer.Length);
originalStream.Close();
// Encrypt
alg.Encipher(buffer, buffer.Length);
string szEncFile;
if (szEncryptedFile != string.Empty) szEncFile = szEncryptedFile; else szEncFile = szFilePath;
System.IO.FileStream stream = new System.IO.FileStream(szEncFile, System.IO.FileMode.Create);
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
감사합니다. 무엇을 당신이하려는 것은 8로 나누어 다음 값으로 최대 값 라운드 인 경우
문제의 적절한 진단을 원한다면이 시나리오의 원인이되는 코드를 보여주십시오. –
자신의 Encipher 메소드를 구현하는 대신 http://www.bouncycastle.org/csharp/를 사용하는 것이 좋습니다. –
나는 Blowfish와 함께 돌아 다닙니다. 제안에 대해 감사드립니다. – MahanGM