2016-10-25 1 views
0

Stegolayer()이라는 LSB 스테 가노 그래피 기능이 있습니다. 여기에 데이터를 숨겨 보내야합니다. 내 원본 데이터는 AES로 암호화되어 output에 저장됩니다. 이와 함께 파일 바이트 해시 코드와 RSA로 암호화 된 세션 키를 첨부합니다.여러 부분을 결합한 후 여러 부분으로 구성된 바이트 스트림을 분할하는 방법

사실 내 내장 된 암호는 암호화 된 데이터 + 해시 코드 & 세션 키를 하나의 단일 바이트 스트림으로 구성합니다. 압축을 풀면 어떻게이 두 부분을 나눌 수 있습니까? 내가 추출 후 분할하려고

EncryptFile(pass_txt.Text, loadedFilePath, output); 
      fileContainer = File.ReadAllBytes(output); 
      hashcode = SHA256.Create().ComputeHash(File.ReadAllBytes(loadedFilePath)); 
      Newpassword = CreateRandomPassword(pass_txt.Text.Length); 
      Newpasswordbytes = Byteconverter.GetBytes(Newpassword); 
      RSAplain= Combine(hashcode,Newpasswordbytes); 
      RSAcipher = RSAencryption(RSAplain, RSA.ExportParameters(false), false); 
      bytestobehidden = Combine(fileContainer, RSAcipher); 
      fileSize = bytestobehidden.Length; 
      if (8 * ((height * (width/3) * 3)/3 - 1) < fileSize + fileNameSize) 
      { 
       MessageBox.Show("File size is too large!\nPlease use a larger image to hide this file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       return; 
      } 

      StegoLayer(); 

private byte[] Combine(byte[] a, byte[] b) 
    { 
     byte[] c = new byte[a.Length + b.Length]; 
     System.Buffer.BlockCopy(a, 0, c, 0, a.Length); 
     System.Buffer.BlockCopy(b, 0, c, a.Length, b.Length); 
     return c; 
    } 

public static void EncryptFile(string password, string in_file, string out_file) 
    { 
     CryptFile(password, in_file, out_file, true); 
    } 

    public static void DecryptFile(string password, string in_file, string out_file) 
    { 
     CryptFile(password, in_file, out_file, false); 
    } 
    public static void CryptFile(string password,string in_file, string out_file, bool encrypt) 
    { 
     // Create input and output file streams. 
     using (FileStream in_stream = 
      new FileStream(in_file, FileMode.Open, FileAccess.Read)) 
     { 
      using (FileStream out_stream = 
       new FileStream(out_file, FileMode.Create, 
        FileAccess.Write)) 
      { 
       // Encrypt/decrypt the input stream into 
       // the output stream. 
       CryptStream(password, in_stream, out_stream, encrypt); 
      } 
     } 
    } 
    // Encrypt the data in the input stream into the output stream. 
    public static void CryptStream(string password, 
     Stream in_stream, Stream out_stream, bool encrypt) 
    { 
     // Make an AES service provider. 
     AesCryptoServiceProvider aes_provider = 
      new AesCryptoServiceProvider(); 

     // Find a valid key size for this provider. 
     int key_size_bits = 0; 
     for (int i = 1024; i > 1; i--) 
     { 
      if (aes_provider.ValidKeySize(i)) 
      { 
       key_size_bits = i; 
       break; 
      } 
     } 
     //Debug.Assert(key_size_bits > 0); 
     // Console.WriteLine("Key size: " + key_size_bits); 

     // Get the block size for this provider. 
     int block_size_bits = aes_provider.BlockSize; 

     // Generate the key and initialization vector. 
     byte[] key = null; 
     byte[] iv = null; 
     byte[] salt = { 0x0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 
    0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 }; 
     MakeKeyAndIV(password, salt, key_size_bits, block_size_bits, 
      out key, out iv); 

     // Make the encryptor or decryptor. 
     ICryptoTransform crypto_transform; 
     if (encrypt) 
     { 
      crypto_transform = aes_provider.CreateEncryptor(key, iv); 
     } 
     else 
     { 
      crypto_transform = aes_provider.CreateDecryptor(key, iv); 
     } 

     // Attach a crypto stream to the output stream. 
     // Closing crypto_stream sometimes throws an 
     // exception if the decryption didn't work 
     // (e.g. if we use the wrong password). 
     try 
     { 
      using (CryptoStream crypto_stream = 
       new CryptoStream(out_stream, crypto_transform, 
        CryptoStreamMode.Write)) 
      { 
       // Encrypt or decrypt the file. 
       const int block_size = 1024; 
       byte[] buffer = new byte[block_size]; 
       int bytes_read; 
       while (true) 
       { 
        // Read some bytes. 
        bytes_read = in_stream.Read(buffer, 0, block_size); 
        if (bytes_read == 0) break; 

        // Write the bytes into the CryptoStream. 
        crypto_stream.Write(buffer, 0, bytes_read); 
       } 
      } // using crypto_stream 
     } 
     catch 
     { 
     } 

     crypto_transform.Dispose(); 
    } 
    // Use the password to generate key bytes. 
    private static void MakeKeyAndIV(string password, byte[] salt, 
     int key_size_bits, int block_size_bits, 
     out byte[] key, out byte[] iv) 
    { 
     Rfc2898DeriveBytes derive_bytes = 
      new Rfc2898DeriveBytes(password, salt, 1000); 

     key = derive_bytes.GetBytes(key_size_bits/8); 
     iv = derive_bytes.GetBytes(block_size_bits/8); 
    } 

을 다음하지만 다시 새 키를 검색 할 수 없습니다 나는 내가 잘못 알고하지 않는

나는 bytestobehidden를 구축합니다.

+0

좋아, 어떻게 추출 'bytestobehidden'추출한 후 –

+0

내 이미지에이 바이트를 첨부하는 방법과 내가 그들을 추출하는 방법을 읽고, 그것을 이해하는 예제를 제공합니다. –

+0

'output'은 암호화 후 암호화 된 파일의 경로입니다. 예, 이것은 제가 사용하는 'combine()'함수입니다. –

답변

0

bytestobehidden의 시작 부분에 스트림을 분할 할 위치를 나타내는 몇 바이트를 추가 할 수 있습니다. 예 :

fileContainer = ... 
RSAcipher = ... 

byte[] header = new byte[3]; 
int fileLength = fileContainer.Length; 
header[0] = (byte) ((fileLength >> 16) & 0xff); 
header[1] = (byte) ((fileLength >> 8) & 0xff); 
header[2] = (byte) (fileLength & 0xff); 

byte[] bytestobehidden = Combine(header, fileContainer, RSAcipher); 

여기서 Combine()은 3 바이트 배열을 허용합니다. 당신의 비밀을 추출 후에는

int fileLength = (int) (bytestobehidden[0] << 16) + 
       (int) (bytestobehidden[1] << 8) + 
       (int) bytestobehidden[2]; 

byte[] fileContainer = new byte[fileLength]; 
byte[] RSACipher = new byte[bytestobehidden.Length-fileLength-3]; 
System.Array.Copy(bytestobehidden, 3, fileContainer, 0, fileLength); 
System.Array.Copy(bytestobehidden, fileLength+3, RSACipher, 0, bytestobehidden.Length-fileLength-3); 

&, >><<bitwise operators을하다 할 수 있습니다. 헤더를 2 바이트 길이로만 수정할 수 있지만 64KB보다 큰 파일 크기는 절대 처리하지 않아야합니다. 반면에 3 바이트는 16 MB 크기까지 인코딩 할 수 있습니다.

+0

감사합니다.이 코드는 me.i에 유용합니다. 내 코드를 편집했는데 하나의 디버그에서 암호화와 암호 해독을 만들 때 잘 실행되지만 암호화를 한 후에 내 애플리케이션을 닫고 암호 해독을 다시 실행하면됩니다 .It 'RSAdecryption()'의 출력이 null이라는 것과 같은 오류를 다시 내게 주어라. –

+0

이것은 프로젝트의 후속 문제 일 수 있지만 사물의 계획에서 추출 된 바이트 스트림을 부분 분할하는 것과 관련하여 여기에서 질문하는 것과는 별도의 문제입니다. 관련 코드를 보여주는 새로운 질문을하는 것이 좋습니다. 상황에 도움이된다고 생각한다면이 질문에 다시 링크하십시오. – Reti43

관련 문제