|
package coder; |
|
|
|
import java.io.DataInputStream; |
|
import java.io.File; |
|
import java.io.FileInputStream; |
|
import java.io.FileNotFoundException; |
|
import java.io.FileOutputStream; |
|
import java.io.IOException; |
|
import java.io.UnsupportedEncodingException; |
|
import java.security.InvalidKeyException; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.security.spec.InvalidKeySpecException; |
|
import java.util.Scanner; |
|
import javax.crypto.Cipher; |
|
import javax.crypto.IllegalBlockSizeException; |
|
import javax.crypto.KeyGenerator; |
|
import javax.crypto.NoSuchPaddingException; |
|
import javax.crypto.SecretKey; |
|
import javax.crypto.SecretKeyFactory; |
|
import javax.crypto.spec.DESKeySpec; |
|
|
|
/** |
|
* |
|
* @author periklis master_ex ntanasis - pntanasis@gmail.com |
|
*/ |
|
public class Coder { |
|
|
|
// @see http://download.oracle.com/javase/1.4.2/docs/guide/security/jce/JCERefGuide.html#PBE |
|
//found here: http://www.exampledepot.com/egs/javax.crypto/DesString.html |
|
public static class DesEncrypter { |
|
|
|
Cipher ecipher; |
|
Cipher dcipher; |
|
|
|
DesEncrypter(SecretKey key) { |
|
try { |
|
ecipher = Cipher.getInstance("DES"); |
|
dcipher = Cipher.getInstance("DES"); |
|
ecipher.init(Cipher.ENCRYPT_MODE, key); |
|
dcipher.init(Cipher.DECRYPT_MODE, key); |
|
|
|
} catch (javax.crypto.NoSuchPaddingException e) { |
|
} catch (java.security.NoSuchAlgorithmException e) { |
|
} catch (java.security.InvalidKeyException e) { |
|
} |
|
} |
|
|
|
public String encrypt(String str) { |
|
try { |
|
// Encode the string into bytes using utf-8 |
|
byte[] utf8 = str.getBytes("UTF8"); |
|
|
|
// Encrypt |
|
byte[] enc = ecipher.doFinal(utf8); |
|
|
|
// Encode bytes to base64 to get a string |
|
return new sun.misc.BASE64Encoder().encode(enc); |
|
} catch (javax.crypto.BadPaddingException e) { |
|
} catch (IllegalBlockSizeException e) { |
|
} catch (UnsupportedEncodingException e) { |
|
} catch (java.io.IOException e) { |
|
} |
|
return null; |
|
} |
|
|
|
public String decrypt(String str) { |
|
try { |
|
// Decode base64 to get bytes |
|
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); |
|
|
|
// Decrypt |
|
byte[] utf8 = dcipher.doFinal(dec); |
|
|
|
// Decode using utf-8 |
|
return new String(utf8, "UTF8"); |
|
} catch (javax.crypto.BadPaddingException e) { |
|
} catch (IllegalBlockSizeException e) { |
|
} catch (UnsupportedEncodingException e) { |
|
} catch (java.io.IOException e) { |
|
} |
|
return null; |
|
} |
|
} |
|
|
|
/** Save the specified TripleDES SecretKey to the specified file */ |
|
public static void writeKey(SecretKey key, File f) throws IOException, |
|
NoSuchAlgorithmException, InvalidKeySpecException { |
|
// Convert the secret key to an array of bytes like this |
|
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DES"); |
|
DESKeySpec keyspec = (DESKeySpec) keyfactory.getKeySpec(key, |
|
DESKeySpec.class); |
|
byte[] rawkey = keyspec.getKey(); |
|
|
|
// Write the raw key to the file |
|
FileOutputStream out = new FileOutputStream(f); |
|
out.write(rawkey); |
|
out.close(); |
|
} |
|
|
|
/** Read a TripleDES secret key from the specified file */ |
|
public static SecretKey readKey(File f) throws IOException, |
|
NoSuchAlgorithmException, InvalidKeyException, |
|
InvalidKeySpecException { |
|
// Read the raw bytes from the keyfile |
|
DataInputStream in = new DataInputStream(new FileInputStream(f)); |
|
byte[] rawkey = new byte[(int) f.length()]; |
|
in.readFully(rawkey); |
|
in.close(); |
|
|
|
// Convert the raw bytes to a secret key like this |
|
DESKeySpec keyspec = new DESKeySpec(rawkey); |
|
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DES"); |
|
SecretKey key = keyfactory.generateSecret(keyspec); |
|
return key; |
|
} |
|
|
|
public static void main(String[] args) throws FileNotFoundException, |
|
NoSuchAlgorithmException, NoSuchPaddingException, IOException, |
|
InvalidKeySpecException, InvalidKeyException { |
|
|
|
if (args[0].equalsIgnoreCase("-en")) { |
|
Scanner in = new Scanner(new File(args[1])); |
|
|
|
String code = ""; |
|
|
|
while (in.hasNext()) { |
|
code += in.nextLine() + "\n"; |
|
} |
|
|
|
code = "?>" + code + "<?php"; |
|
|
|
SecretKey key = KeyGenerator.getInstance("DES").generateKey(); |
|
DesEncrypter encrypter = new DesEncrypter(key); |
|
|
|
writeKey(key, new File("key")); |
|
|
|
String encrypted = encrypter.encrypt(code); |
|
|
|
System.out.println(encrypted); |
|
} else if (args[0].equalsIgnoreCase("-de")) { |
|
Scanner in = new Scanner(new File(args[1])); |
|
|
|
String code = ""; |
|
|
|
while (in.hasNext()) { |
|
code += in.nextLine() + "\n"; |
|
} |
|
|
|
SecretKey key = readKey(new File(args[2])); |
|
DesEncrypter encrypter = new DesEncrypter(key); |
|
|
|
String decrypted = encrypter.decrypt(code); |
|
|
|
System.out.println(decrypted); |
|
} |
|
|
|
} |
|
} |