Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 10.15 KB | None | 0 0
  1. import java.*
  2. import java.lang.*
  3. import java.awt.*
  4. import java.awt.image.*
  5. import java.io.*
  6. import javax.imageio.*
  7. import java.nio.ByteBuffer
  8. import java.nio.file.*
  9. import kotlin.experimental.and
  10. import kotlin.experimental.or
  11.  
  12. object Lab2 {
  13.     @JvmStatic
  14.     fun main(args: Array<String>) {
  15.  
  16.         if (args.size < 3 || args.size > 4) {
  17.             System.exit(1)
  18.         }
  19.  
  20.         if (args[0] == "encrypt" && args.size == 4) {
  21.  
  22.             val inputFile = args[1]
  23.             val outputFile = args[2]
  24.  
  25.             var src: BufferedImage? = null
  26.             try {
  27.                 src = ImageIO.read(File(inputFile))
  28.             } catch (e: IOException) {
  29.                 println("Lab2: could not open file $inputFile")
  30.                 System.exit(1)
  31.             }
  32.  
  33.             val img = BufferedImage(src!!.width, src.height, BufferedImage.TYPE_4BYTE_ABGR)
  34.             img.graphics.drawImage(src, 0, 0, null)
  35.  
  36.             val width = img.width
  37.             val height = img.height
  38.             val max_size = width * height - 4
  39.  
  40.             println("Image width: $width")
  41.             println("Image height: $height")
  42.             println("Max encryption length: $max_size")
  43.  
  44.             var to_encrypt_bytes: ByteArray? = null
  45.             try {
  46.                 val fileToEncrypt = Paths.get(args[3])
  47.                 to_encrypt_bytes = Files.readAllBytes(fileToEncrypt)
  48.             } catch (e: IOException) {
  49.                 println("Could not open file " + args[3])
  50.                 System.exit(1)
  51.             }
  52.  
  53.             val encrypt_length = to_encrypt_bytes!!.size
  54.             println("Encryption length: $encrypt_length")
  55.             if (encrypt_length >= 2147483647) {
  56.                 println("Encryption data is too large")
  57.                 System.exit(1)
  58.             }
  59.             if (encrypt_length >= max_size) {
  60.                 println("Encryption data is too large for selected image")
  61.                 System.exit(1)
  62.             }
  63.  
  64.             val encrypt_length_bytes = ByteBuffer.allocate(4).putInt(encrypt_length).array()
  65.             var cur_byte = 0
  66.             // Первые 4 пикселя содержат длинну сообщения
  67.             for (y in 0 until height) {
  68.                 for (x in 0 until width) {
  69.                     if (cur_byte < 4) {
  70.                         val rgb_int = img.getRGB(x, y)
  71.                         val color = Color(rgb_int, true)
  72.                         val cur_encrypt_byte = encrypt_length_bytes[cur_byte]
  73.                         // Первые 2 бита в красном
  74.                         var red = color.red
  75.                         val new_red = cur_encrypt_byte.toInt().ushr(6) and 0x03 or 0xfc
  76.                         red = red or 0x03 and new_red
  77.                         // Вторые 2 бита в зеленом
  78.                         var green = color.green
  79.                         val new_green = cur_encrypt_byte.toInt().ushr(4) and 0x03 or 0xfc
  80.                         green = green or 0x03 and new_green
  81.                         // Третьи два бита в синем
  82.                         var blue = color.blue
  83.                         val new_blue = cur_encrypt_byte.toInt().ushr(2) and 0x03 or 0xfc
  84.                         blue = blue or 0x03 and new_blue
  85.                         // Последние 2 бита в альфа
  86.                         var alpha = color.alpha
  87.                         val new_alpha = cur_encrypt_byte and 0x03 or 0xfc.toByte()
  88.                         alpha = alpha or 0x03 and new_alpha.toInt()
  89.                         // Новый цвет
  90.                         val new_col = Color(red, green, blue, alpha)
  91.                         img.setRGB(x, y, new_col.rgb)
  92.                         cur_byte++
  93.                     } else {
  94.                         break
  95.                     }
  96.                 }
  97.                 if (cur_byte >= 4) {
  98.                     break
  99.                 }
  100.             }
  101.  
  102.             cur_byte = 0
  103.             for (y in 4 until height) {
  104.                 for (x in 4 until width) {
  105.                     if (cur_byte < encrypt_length) {
  106.                         val rgb_int = img.getRGB(x, y)
  107.                         val color = Color(rgb_int, true)
  108.                         val cur_encrypt_byte = to_encrypt_bytes[cur_byte]
  109.                         // Первые 2 бита в красном
  110.                         var red = color.red
  111.                         val new_red = cur_encrypt_byte.toInt().ushr(6) and 0x03 or 0xfc
  112.                         red = red or 0x03 and new_red
  113.                         // Вторые два бита в зеленом
  114.                         var green = color.green
  115.                         val new_green = cur_encrypt_byte.toInt().ushr(4) and 0x03 or 0xfc
  116.                         green = green or 0x03 and new_green
  117.                         // Третьи 2 бита в синем
  118.                         var blue = color.blue
  119.                         val new_blue = cur_encrypt_byte.toInt().ushr(2) and 0x03 or 0xfc
  120.                         blue = blue or 0x03 and new_blue
  121.                         // Последние 2 бита в альфа
  122.                         var alpha = color.alpha
  123.                         val new_alpha = cur_encrypt_byte and 0x03 or 0xfc.toByte()
  124.                         alpha = alpha or 0x03 and new_alpha.toInt()
  125.                         // make new color
  126.                         val new_col = Color(red, green, blue, alpha)
  127.                         img.setRGB(x, y, new_col.rgb)
  128.                         cur_byte++
  129.                     } else {
  130.                         break
  131.                     }
  132.                 }
  133.                 if (cur_byte >= encrypt_length) {
  134.                     break
  135.                 }
  136.             }
  137.  
  138.             try {
  139.                 val output = File(outputFile)
  140.                 ImageIO.write(img, "png", output)
  141.             } catch (e: IOException) {
  142.                 println("Could not save file $outputFile")
  143.             }
  144.  
  145.             println("Success")
  146.  
  147.         } else if (args[0] == "decrypt" && args.size == 3) {
  148.  
  149.             var img: BufferedImage? = null
  150.  
  151.             try {
  152.                 img = ImageIO.read(File(args[1]))
  153.             } catch (e: IOException) {
  154.                 println("Could not open file")
  155.                 System.exit(1)
  156.             }
  157.  
  158.             val width = img!!.width
  159.             val height = img.height
  160.  
  161.             val length_bytes = ByteArray(4)
  162.             var cur_byte = 0
  163.             // First 4 pixels contain length of message
  164.             for (y in 0 until height) {
  165.                 for (x in 0 until width) {
  166.                     if (cur_byte < 4) {
  167.                         val rgb_int = img.getRGB(x, y)
  168.                         val color = Color(rgb_int, true)
  169.                         var encrypted_info: Byte = 0x0
  170.                         // first two bits in RED
  171.                         val red = color.red
  172.                         encrypted_info = (encrypted_info or ((red and 0x03 shl 6).toByte()))
  173.                         // second two bits in GREEN
  174.                         val green = color.green
  175.                         encrypted_info = (encrypted_info or ((green and 0x03 shl 4).toByte()))
  176.                         // third two bits in BLUE
  177.                         val blue = color.blue
  178.                         encrypted_info = (encrypted_info or ((blue and 0x03 shl 2).toByte()))
  179.                         // last two bits in ALPHA
  180.                         val alpha = color.alpha
  181.                         encrypted_info = (encrypted_info or ((alpha and 0x03).toByte()))
  182.                         // add byte
  183.                         length_bytes[cur_byte] = encrypted_info
  184.                         cur_byte++
  185.                     } else {
  186.                         break
  187.                     }
  188.                 }
  189.                 if (cur_byte >= 4) {
  190.                     break
  191.                 }
  192.             }
  193.  
  194.             val encrypt_length = ByteBuffer.wrap(length_bytes).int
  195.  
  196.             val decrypted_bytes = ByteArray(encrypt_length)
  197.             cur_byte = 0
  198.             for (y in 4 until height) {
  199.                 for (x in 4 until width) {
  200.                     if (cur_byte < encrypt_length) {
  201.                         val rgb_int = img.getRGB(x, y)
  202.                         val color = Color(rgb_int, true)
  203.                         var encrypted_info: Byte = 0x0
  204.                         // Первые 2 бита в красном
  205.                         val red = color.red
  206.                         encrypted_info = (encrypted_info or ((red and 0x03 shl 6).toByte()))
  207.                         // Вторые 2 бита в зеленом
  208.                         val green = color.green
  209.                         encrypted_info = (encrypted_info or ((green and 0x03 shl 4).toByte()))
  210.                         // Третьи 2 бита в синем
  211.                         val blue = color.blue
  212.                         encrypted_info = (encrypted_info or ((blue and 0x03 shl 2).toByte()))
  213.                         // Последние 2 бита в альфа
  214.                         val alpha = color.alpha
  215.                         encrypted_info = (encrypted_info or ((alpha and 0x03).toByte()))
  216.                         // Добавляем байт
  217.                         decrypted_bytes[cur_byte] = encrypted_info
  218.                         cur_byte++
  219.                     } else {
  220.                         break
  221.                     }
  222.                 }
  223.                 if (cur_byte >= encrypt_length) {
  224.                     break
  225.                 }
  226.             }
  227.  
  228.             try {
  229.                 val decoded = String(decrypted_bytes, "UTF-8")
  230.                 println(decoded)
  231.                 val decrypted_output = FileOutputStream(args[2])
  232.                 decrypted_output.write(decrypted_bytes)
  233.                 decrypted_output.close()
  234.             } catch (e: UnsupportedEncodingException) {
  235.                 println("Сould not decoded bytes")
  236.                 System.exit(1)
  237.             } catch (e: IOException) {
  238.                 println("Сould not save file " + args[2])
  239.                 System.exit(1)
  240.             }
  241.  
  242.         } else {
  243.             System.exit(1)
  244.         }
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement