Advertisement
TheRio

Video Encryption Pseudocode

Feb 20th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.91 KB | None | 0 0
  1. //-----------------------------------------SWAPMAP GENERATING--------------------------------------------------------//
  2.  
  3. int[] MakeSwapMap(int BitmapWidth, int BitmapHeight, Random random)
  4. {
  5.     int Count = ((BitmapWidth * BitmapHeight) / 8) / 8;
  6.     int[] SwapMap = new int[Count];
  7.  
  8.     for (int i = 0; i < Count; i++)
  9.     {
  10.         SwapMap[i] = random.NextInt(i, Count);
  11.     }
  12.  
  13.     return SwapMap;
  14. }
  15.  
  16. int[] MakeSwapMap(int BitmapWidth, int BitmapHeight, string StringSeed)
  17. {
  18.     Xorgens4096 random = new Xorgens4096(StringSeed);
  19.     int Count = ((BitmapWidth * BitmapHeight) / 8) / 8;
  20.     int[] SwapMap = new int[Count];
  21.    
  22.     for (int i = 0; i < Count; i++)
  23.     {
  24.         SwapMap[i] = random.NextInt(i, Count);
  25.     }
  26.  
  27.     return SwapMap;
  28. }
  29.  
  30.  
  31.  
  32. //-----------------------------------------BITMAP SHUFFLING----------------------------------------------------------//
  33.  
  34.  
  35. Bitmap ShuffleBitmap(Bitmap bitmap, int[] SwapMap)
  36. {
  37.     int Width = bitmap.Width;    //Ширина битмапа
  38.     int Height = bitmap.Height;  //Высота битмапа
  39.    
  40.     //Проверка на правильный размер картинки
  41.     if ((Width % 8 != 0) && (Height % 8 != 0))
  42.         throw new ArgumentOutOfRangeException("Размер изображения должен быть кратным 8");
  43.  
  44.     //Проверяем что карта подмен соответсвует для этого битмапа
  45.     if (SwapMap.Length != (((Width * Height) / 8) / 8))
  46.         throw new ArgumentOutOfRangeException("Неверный размер карты подмен");
  47.  
  48.    
  49.     //Свапаем по карте подмен через цикл
  50.     for (int i = 0; i < SwapMap.Length; i++)
  51.     {
  52.         int swapIndex = SwapMap[i];
  53.         int firstX = ((i * 8) % Width);
  54.         int firstY = ((i * 8) / Width) * 8;
  55.         int secondX = ((swapIndex * 8) % Width);
  56.         int secondY = ((swapIndex * 8) / Width) * 8;
  57.  
  58.         for (int y = 0; y < 8; y++)
  59.         {
  60.             for (int x = 0; x < 8; x++)
  61.             {
  62.                 //Попиксельно обмениваем пиксели местами
  63.                 Color firstPx = bitmap.GetPixel(firstX + x, firstY + y);
  64.                 Color secondPx = bitmap.GetPixel(secondX + x, secondY + y);
  65.  
  66.                 bitmap.SetPixel(firstX + x, firstY + y, secondPx);
  67.                 bitmap.SetPixel(secondX + x, secondY + y, firstPx);
  68.             }
  69.         }
  70.     }
  71.  
  72.     return bitmap;
  73. }
  74.  
  75. Bitmap UnShuffleBitmap(Bitmap bitmap, int[] SwapMap)
  76. {
  77.     int Width = bitmap.Width;    //Ширина битмапа
  78.     int Height = bitmap.Height;  //Высота битмапа
  79.    
  80.     //Проверка на правильный размер картинки
  81.     if ((Width % 8 != 0) && (Height % 8 != 0))
  82.         throw new ArgumentOutOfRangeException("Размер изображения должен быть кратным 8");
  83.  
  84.     //Проверяем что карта подмен соответсвует для этого битмапа
  85.     if (SwapMap.Length != (((Width * Height) / 8) / 8))
  86.         throw new ArgumentOutOfRangeException("Неверный размер карты подмен");
  87.  
  88.  
  89.     //Для дешифрации просто читаем карту подмен задом-наперед
  90.     for (int i = SwapMap.Length - 1; i >= 0; i--)
  91.     {
  92.         int swapIndex = SwapMap[i];
  93.         int firstX = ((i * 8) % Width);
  94.         int firstY = ((i * 8) / Width) * 8;
  95.         int secondX = ((swapIndex * 8) % Width);
  96.         int secondY = ((swapIndex * 8) / Width) * 8;
  97.  
  98.         for (int y = 0; y < 8; y++)
  99.         {
  100.             for (int x = 0; x < 8; x++)
  101.             {
  102.                 //Попиксельно обмениваем пиксели местами
  103.                 Color firstPx = bitmap.GetPixel(firstX + x, firstY + y);
  104.                 Color secondPx = bitmap.GetPixel(secondX + x, secondY + y);
  105.  
  106.                 bitmap.SetPixel(firstX + x, firstY + y, secondPx);
  107.                 bitmap.SetPixel(secondX + x, secondY + y, firstPx);
  108.             }
  109.         }
  110.     }
  111.  
  112.     return bitmap;
  113. }
  114.  
  115.  
  116.  
  117. //------------------------------------------VIDEO SHUFFLING----------------------------------------------------------//
  118.  
  119.  
  120. void ShuffleVideo(VideoFileReader reader, VideoFileWriter writer, string StringSeed)
  121. {
  122.     CurrentFrame = 0; //Счетчик текущего кадра, чтобы менять карту подмен каждые 1/4 секунды (250мс)
  123.    
  124.     //Генерируем начальную карту подмен
  125.     uint[] SwapMap = MakeSwapMap(writer.Width, writer.Height, StringSeed);
  126.    
  127.     while (true)
  128.     {
  129.         //Читаем кадр
  130.         using (Bitmap readedFrame = reader.ReadVideoFrame())
  131.         {
  132.             if (readedFrame == null) { break; } //Если прочитаный кадр равен null, то кадры закончились
  133.  
  134.             CurrentFrame++; //Кадр прочитан успешно
  135.            
  136.            
  137.             //Обновляем карту подмен каждые 1/4 секунды (250мс)
  138.             if (CurrentFrame % (reader.FrameRate / 4) == 0)
  139.                 SwapMap = MakeSwapMap(writer.Width, writer.Height, StringSeed);
  140.  
  141.             //Перемешиваем и записываем кадр
  142.             writer.WriteVideoFrame(ShuffleBitmap(readedFrame, SwapMap));
  143.         }
  144.     }
  145. }
  146.  
  147. void UnShuffleVideo(VideoFileReader reader, VideoFileWriter writer, string StringSeed)
  148. {
  149.     CurrentFrame = 0; //Счетчик текущего кадра, чтобы менять карту подмен каждые 1/4 секунды (250мс)
  150.    
  151.     //Генерируем начальную карту подмен
  152.     uint[] SwapMap = MakeSwapMap(writer.Width, writer.Height, StringSeed);
  153.    
  154.     while (true)
  155.     {
  156.         //Читаем кадр
  157.         using (Bitmap readedFrame = reader.ReadVideoFrame())
  158.         {
  159.             if (readedFrame == null) { break; } //Если прочитаный кадр равен null, то кадры закончились
  160.  
  161.             CurrentFrame++; //Кадр прочитан успешно
  162.            
  163.            
  164.             //Обновляем карту подмен каждые 1/4 секунды (250мс)
  165.             if (CurrentFrame % (reader.FrameRate / 4) == 0)
  166.                 SwapMap = MakeSwapMap(writer.Width, writer.Height, StringSeed);
  167.  
  168.             //Перемешиваем и записываем кадр
  169.             writer.WriteVideoFrame(UnShuffleBitmap(readedFrame, SwapMap));
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement