How to encrypt/decrypt a file in Java?

前端 未结 6 1497
滥情空心
滥情空心 2021-01-16 09:07

I am writing a Java application which can \"encrypt\" and consequently \"decrypt\" whatever binary file.

I am just a beginner in the \"cryptography\" area so I would

相关标签:
6条回答
  • 2021-01-16 09:27

    Maybe this open source library can help you:

    http://www.jasypt.org/

    0 讨论(0)
  • 2021-01-16 09:38

    try the sample given below. u could convert the bytes to string and then encrypt and then write it to file. reverse it while decrypting.

    http://www.exampledepot.com/egs/javax.crypto/desstring.html

    below u can find a sample DES enc&dec for files..

    http://www.exampledepot.com/egs/javax.crypto/DesFile.html

    0 讨论(0)
  • 2021-01-16 09:43

    Don't store it there! Any good encryption is based on mathematical algorithms (like AES). You may want to have a look at BouncyCastle http://www.bouncycastle.org/ - but encryption is not a simple topic, so you should get a good book to learn about its basics first!

    0 讨论(0)
  • 2021-01-16 09:48

    It would probably be easier not to check the password give by the user against a global password, rather ensure that only that one password (known by the user) decrypts the ciphertext into the correct plaintext, any other password would return gibberish. This is usually how cryptography works and means you don't have to store a centralised password anywhere.

    0 讨论(0)
  • 2021-01-16 09:49

    A really simple way to use a password to encrypt is to use XOR, here is some pseudo code

    for(byte in file)
    {
        Byte newByte = byte ^ (byte) password[i];
        outputFile.write(newByte);
        i = (i + 1) password.length();
    }
    

    This is based on the identity that (x XOR y) XOR y = x, all you need to do is encrypt/decrypt with the same password.

    0 讨论(0)
  • 2021-01-16 09:53

    Use the password to encrypt your data. You could for example repeat the password so that it matches the byte array's length and then do something like

    data[i] = data[i] >> password[i];
    

    Edit: if you wanted to store the password, you would have to encrypt it. Which - at least when using symmetrical cryptosystems - will be inherently insecure.

    0 讨论(0)
提交回复
热议问题