Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Save File

  • 21-03-2011 8:26pm
    #1
    Registered Users, Registered Users 2 Posts: 1,686 ✭✭✭


    Hi,

    Getting straight to the point. I have a file and I would like to encrypt the contents of the file with a custom crc32 checksum. How would I go about doing this?

    This way the file will be corrupted if anyone tries to hex edit it or mess with it in any way.


Comments

  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    On a Unix system you can do this from a shell script.
    ~/ $ crc32 file
    8hexdigits
    
    Then save the digits corresponding to the file in a database or serialised hash map off site This will allow you do a crc check on the filesystem to ensure your files have not been modified (for limited possibility of results collision value of not)

    With more detail of your issue a more specific answer could be offered.


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    Sorry, re-read your question and I may have a better sense of what you want to do.
    A very simple example would be:
    to encode:
    perl -e ' 
    use strict;
    use warnings;
    chomp(my $salt=`crc32 file`);
    my $enc=$salt; 
    open (my $file, "<" , "file"); 
    while(read $file, my $content, length($salt) ){
       $enc.=$content ^ $salt;
    } 
    print $enc;'
    >encFile
    

    To decode
    perl -e ' use strict; 
    use warnings; 
    my $content="";open (my $encFile, "<" , "encFile"); 
    read($encFile,my $salt,8);
    while(read $encFile, my $encContent, length($salt) ){
       $content.= $encContent ^ $salt;
    } 
    print $content;'
    

    Though a simple repeated XOR of the file is a poor example, as all unedited characters can be retrieved, employing the salt as a password for another encryption library and prepending it to the file as a number rather than a string would give you a much better security.


  • Registered Users, Registered Users 2 Posts: 1,686 ✭✭✭RealistSpy


    Sorry, re-read your question and I may have a better sense of what you want to do.
    A very simple example would be:
    to encode:
    perl -e ' 
    use strict;
    use warnings;
    chomp(my $salt=`crc32 file`);
    my $enc=$salt; 
    open (my $file, "<" , "file"); 
    while(read $file, my $content, length($salt) ){
       $enc.=$content ^ $salt;
    } 
    print $enc;'
    >encFile
    

    To decode
    perl -e ' use strict; 
    use warnings; 
    my $content="";open (my $encFile, "<" , "encFile"); 
    read($encFile,my $salt,8);
    while(read $encFile, my $encContent, length($salt) ){
       $content.= $encContent ^ $salt;
    } 
    print $content;'
    

    Though a simple repeated XOR of the file is a poor example, as all unedited characters can be retrieved, employing the salt as a password for another encryption library and prepending it to the file as a number rather than a string would give you a much better security.

    What are the chances og making this code vb, java or c++? I don't know perl


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    I'll do a Java version this evening if I get the time, Perl is hard-wired at this stage, everything else I actually have to plan ;)


  • Registered Users, Registered Users 2 Posts: 1,686 ✭✭✭RealistSpy


    I'll do a Java version this evening if I get the time, Perl is hard-wired at this stage, everything else I actually have to plan ;)

    We never did perl in college so as far as my head know....Perl is for the old :P. hahahaha

    Thanks for the help.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    Sorry, by the time I'd walked the dogs and made dinner last night i was a wreck.

    The basics in Java are similar.
    You have the class
    java.util.zip.CRC32 which can create a checksum for a byte array which you can create from a file reader.
    Use the resulting long as a key for a javax.crypto.Cipher object ("Blowfish/CBC/NoPadding") and you set it to encrypt or decrypt as apropriate, now add your key to the end of the byte array and write to file.

    Or to decrypt read the file into a byte array, cast the long in the final 8 bytes and use as the decryption key for a Blowfish Cipher on the rest of the byte array.

    Perform a CRC check on the result, and if it matches your key, all is well otherwise throw a DireWarningFileMessedWithException or whatever

    PS: Old! how dare you, you insolent pup, how very dare you. I'm a Unix admin who does some coding, and as you can see, in order to demonstrate the idea in Perl I needed a couple of lines (actually written at the command prompt without an editor), in order to produce a Java solution I have to do actual work, OK a Java XOR solution would be more straightforward than the one described above, but a Perl Blowfish solution would be much easier to produce also.


Advertisement