Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, anyone knows how to split an image into pieces, using javascript? I need to encrypt and decrypt that picture into pieces so that hacker will have a problem getting the information
Posted

1 solution

Happy to share VB .NET code to encrypt/decrypt the image. Use some tool to split an image into pieces. Image encryption code:

VB
Public Function EncryptStream(ByVal input As Byte()) As Byte()
    Dim rijn As New RijndaelManaged()
    Dim encrypted As Byte()
    Dim key As Byte() = New Byte() {&H22, &HC0, &H6D, &HCB, &H23, &HA6, _
     &H3, &H1B, &H5A, &H1D, &HD3, &H9F, _
     &H85, &HD, &HC1, &H72, &HED, &HF4, _
     &H54, &HE6, &HBA, &H65, &HC, &H22, _
     &H62, &HBE, &HF3, &HEC, &H14, &H81, _
     &HA8, &HA}
    '32
    Dim IV As Byte() = New Byte() {&H43, &HB1, &H93, &HB, &H1A, &H87, _
     &H52, &H62, &HFB, &H8, &HD, &HC0, _
     &HCA, &H40, &HC2, &HDB}
    '16
    'Get an encryptor.
    Dim encryptor As ICryptoTransform = rijn.CreateEncryptor(key, IV)

    'Encrypt the data.
    Dim msEncrypt As New MemoryStream()
    Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)


    'Write all data to the crypto stream and flush it.
    csEncrypt.Write(input, 0, input.Length)
    csEncrypt.FlushFinalBlock()

    'Get encrypted array of bytes.
    encrypted = msEncrypt.ToArray()

    Return encrypted

End Function


Decryption code:

VB
Public Function DecryptStream(ByVal input As Byte()) As Byte()
    Dim rijn As New RijndaelManaged()
    Dim decrypted As Byte()
    Dim key As Byte() = New Byte() {&H22, &HC0, &H6D, &HCB, &H23, &HA6, _
     &H3, &H1B, &H5A, &H1D, &HD3, &H9F, _
     &H85, &HD, &HC1, &H72, &HED, &HF4, _
     &H54, &HE6, &HBA, &H65, &HC, &H22, _
     &H62, &HBE, &HF3, &HEC, &H14, &H81, _
     &HA8, &HA}
    '32
    Dim IV As Byte() = New Byte() {&H43, &HB1, &H93, &HB, &H1A, &H87, _
     &H52, &H62, &HFB, &H8, &HD, &HC0, _
     &HCA, &H40, &HC2, &HDB}
    '16


    'Get a decryptor that uses the same key and IV as the encryptor.
    Dim decryptor As ICryptoTransform = rijn.CreateDecryptor(key, IV)

    'Now decrypt the previously encrypted message using the decryptor
    ' obtained in the above step.
    Dim msDecrypt As New MemoryStream(input)
    Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

    decrypted = New Byte(input.Length - 1) {}

    'Read the data out of the crypto stream.
    csDecrypt.Read(decrypted, 0, decrypted.Length)

    Return decrypted
End Function
 
Share this answer
 
Comments
Joel Tan Wei Shan 12-Mar-12 22:20pm    
Hi, thanks for the code, but just enquiring, is there any way to split the image by default to uneven shape? like a jig-saw puzzle form, due to security reasons, as I doing a project on that. Example: a random image, split into many parts via code, and the size must be uneven, like bigger square and smaller square, so that it will be harder for hacker to put back the pieces as one
Joel Tan Wei Shan 12-Mar-12 22:27pm    
http://www.google.com.sg/imgres?q=split+image+into+tiles&um=1&hl=en&sa=N&biw=1680&bih=949&tbm=isch&tbnid=mm-8EYBUItGF-M:&imgrefurl=https://wiki.openmrs.org/display/docs/Google%2BMaps%2BImage%2BViewer%2BModule&docid=s7uskvnQ0eQ2HM&imgurl=https://wiki.openmrs.org/download/attachments/3346707/Googlemaps-heterochromiacat_2.jpg%253Fversion%253D1%2526modificationDate%253D1282171110000&w=384&h=288&ei=BbBeT5SXO6utiQeeqY3cBw&zoom=1&iact=rc&dur=238&sig=118284517343590822152&page=2&tbnh=142&tbnw=189&start=40&ndsp=48&ved=1t:429,r:22,s:40&tx=155&ty=94



an example, but the squares are of different size, larger/smaller, (have to do this by coding) do you know to do?

thanks~

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900