Hey there,
- Contributors:
- Xearox
in some of my plugins I use an encryption/decryption methods.
I found on http://blog.axxg.de/ a German library for it.
I translated the library and compiled it. Included with JavaDoc and sources.
Feel free to use it.
The author of this library allow the free using of it.
---
There are some limitation with using AES and RSA.
The Java security policies don't allow to create AES Keys bigger than 128bit.
On StackOverflow you can find this solution. I have not tested it.
Also you can't encode really big text files with RSA. There are limitation too. If you want encode big text files you should encode it with AES first and then encode the AES key.
This solution you can find here.
If you have any question, how to use this it, you can take a look on following pages(It is German, sorry):
http://blog.axxg.de/java-aes-verschluesselung-mit-beispiel/
http://blog.axxg.de/java-verschluesselung-beispiel-quickstart/
http://blog.axxg.de/java-aes-rsa-key-file/
I add some examples. The decoding part is not ready at this moment.
If you like this package, please rate it =) Thank you!
---
>>> Examples <<<
How do I create a pair of RSA Keys and save it to disk?Code (Java):import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class CreateRSAKeys {
public static void main(String[] args) {
try {
// TODO Auto-generated method stub
//File
File dir = new File("content/keys");
//Create directory
dir.mkdirs();
//Create random keys
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
keygen.initialize(2048);
KeyPair keyPair = keygen.genKeyPair();
//Read key
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
//Save public key
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
FileOutputStream fos = new FileOutputStream(dir.getAbsoluteFile() + "/public.key");
fos.write(x509EncodedKeySpec.getEncoded());
fos.close();
//Save private key
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
fos = new FileOutputStream(dir.getAbsoluteFile() + "/private.key");
fos.write(pkcs8EncodedKeySpec.getEncoded());
fos.close();
System.out.println("Keys created");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}How do I create an AES Key and save it to disk?
Code (Java):import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
public class CreateAESKey {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//Create file from parameter
File dir = new File("content/keys");
//Create random AES Key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
Key key = keygen.generateKey();
//Write key to file
byte[] bytes = key.getEncoded();
FileOutputStream keyfos = new FileOutputStream(file);
keyfos.write(bytes);
keyfos.close();
System.out.println("Key created");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
How do I load the Keys which I saved to a file?
Code (Java):import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class ReadKeys {
/**
*
* @param path Path to the private key
* @return Returns the Private RSA Key
*/
public PrivateKey getPrivateKey(String path){
try {
//Load file
File file = new File(path);
//Read private key
FileInputStream fis = new FileInputStream(file);
byte[] encodedPrivateKey = new byte[(int) file.length()];
fis.read(encodedPrivateKey);
fis.close();
//Generate key
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
//return private key
return privateKey;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
*
* @param path Path to the public key
* @return Returns the Public RSA Key
*/
public PublicKey getPublicKey(String path){
try {
//Load file
File file = new File(path);
//Read public key
FileInputStream fis = new FileInputStream(file);
byte[] encodedPublicKey = new byte[(int) file.length()];
fis.read(encodedPublicKey);
fis.close();
//Generate key
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
//Return public key
return publicKey;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
*
* @param path Path to the AES key
* @return Returns the AES Key
*/
public SecretKey getAESKey (String path){
try {
//Create file
File file = new File(path);
//Read key
FileInputStream fis = new FileInputStream(file);
byte[] encodedKey = new byte[(int) file.length()];
fis.read(encodedKey);
fis.close();
//Generate key
SecretKey key = new SecretKeySpec(encodedKey, "AES");
//Returns the AES Key
return key;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}How do I encode a file?
This code read a file, create a StringList and use a StringBuilder to a the lines to a string. I should work but the decoding is not tested yet.Code (Java):import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.List;
import javax.crypto.SecretKey;
import de.xearox.easycrypt.EasyCrypt;
public class EncodeFile {
ReadKeys readKeys = new ReadKeys();
/**
*
* @param file File to encode
* @param path Path to the Private Key
* @return Encoded string
*/
public String getEncodedFileRSA(File file, String path){
try {
//Get private key
EasyCrypt ecPrivate = new EasyCrypt(readKeys.getPrivateKey(path), "RSA");
//Set charset
Charset charset = Charset.forName("ISO-8859-1");
//Create list from file
List<String> lines = Files.readAllLines(file.toPath(), charset);
//Add lines to string
StringBuilder sb = new StringBuilder();
for(int i = 0; i<lines.size();i++){
sb.append(lines.get(i));
}
//Set text from StringBuilder
String text = sb.toString();
//Return encoded text as String
return ecPrivate.encrypt(text);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String getEncodedFileAES(File file, String path){
try {
//Get AES Key
SecretKey aesKey = readKeys.getAESKey(path);
//Create EasyCrypt Class
EasyCrypt ec = new EasyCrypt(aesKey, "AES");
//Set charset
Charset charset = Charset.forName("ISO-8859-1");
//Create list from file
List<String> lines = Files.readAllLines(file.toPath(), charset);
//Add lines to string
StringBuilder sb = new StringBuilder();
for(int i = 0; i<lines.size();i++){
sb.append(lines.get(i));
}
//Set text from StringBuilder
String text = sb.toString();
String secretText = ec.encrypt(text);
//Return encoded text as String
return secretText;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
The decoding part I will add soon.
---
Library compiled with Java 7
---
Have fun with it and feel free to send me a message if you have problem with it ;-)
Best greetings,
Xearox

EasyCrypt - Encoding and Decoding with RSA,AES and Base64 1.0
Library for Encoding/Decoding with RSA,AES and Base64