Just make sure you follow any points which are labelled as Security Essential.
java.security.CipherOutputStream
, as follows:
You can now simply callimport java.io.*; import java.security.*; import cryptix.provider.key.RawSecretKey; ... String yourfilename = "test.idea"; String passphrase = "Test passphrase"; // First, create a FileOutputStream to the file you want to write to FileOutputStream outputStream = new FileOutputStream(yourfilename); // Now, initialise a cipher which will do the actual encryption // In this example we will use IDEA, the cipher used by PGP 2.x Cipher cipher = Cipher.getInstance("IDEA/CBC/PKCS#5", "Cryptix"); // (is getBytes() right? Is it portable? --IB) RawSecretKey aKey = new RawSecretKey("IDEA", passphrase.getBytes()); cipher.initEncrypt(aKey); // Finally, create a secure output stream with these two parameters CipherOutputStream out = new CipherOutputStream(outputStream, cipher)
out.write(byte[])
with your data, which will be encrypted and written to the file opened in outputStream
.
Initialising the cipher is the critical line in the above code sample. IDEA
is a cipher object which implements the International Data Encryption Algorithm designed by Ascom-Tech. You can, however, use any class which extends java.security.cipher
. Cryptix provides several such ciphers, including SPEED
and DES
.
Security Essential See the Cryptix FAQ for information on these different ciphers and their relative strengths.
Cipher objects need to be initialised with a piece of secret information (a key). Only someone who knows this information will be able to decrypt the data. The key needed depends on the cipher you use. IDEA requires a 16-byte array. See the documentation of the other ciphers for their requirements.
Security Essential Remember to advise your application's users that passphrases must be reasonably complex to provide proper protection. Single words, names of themselves or friends/relatives, or well-known phrases from songs, poems, etc. can be easily guessed by automatic passphrase-guessing programs (many of which exist!). Refer them to http://skuz.wanweb.net/passfaq.html for more advice.
To read the secured data back in, you need an inputstream connected to the file it is stored in, and a Cipher object as above.
FileInputStream inputStream = new FileInputStream(encryptedfile); // Initialise a cipher to do the decryption Cipher cipher = Cipher.getInstance("IDEA/CBC/PKCS#5", "Cryptix"); RawSecretKey aKey = new RawSecretKey("IDEA", passphrase.getBytes()); cipher.initDecrypt(aKey); CipherInputStream in = new CipherInputStream(inputStream, cipher)
CipherOutputStream
as you did before. The user at the other end does the same with a CipherInputStream
.
Obviously, the user you are communicating with needs to know the passphrase as well. This is OK if the two communicating parties can swap this passphrase by other means - such as a personal meeting - but if they cannot you need to use public-key cryptography, which is more difficult. When you're ready to progress to that, good luck!Socket serverSocket = new Socket(serverName, serverPort); DataOutStream outputStream = serverSocket.getOutputStream(); CipherOutputStream secureOut = new CipherOutputStream(outputStream, cipher);