EncryptDecryptTool helps to encrypt and decrypt your important text like password's.
package com.util.encrypt;
import java.util.Scanner;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import beyondwork.secure.BlowfishECB;
public class EncryptionUtils {
public static final String EN_TYPE_PWD = "PASS";
private static BlowfishECB psdECB = null;
private static final String BW_KEY_PWD = "and-fhtiwe-ddfff";
private static final String BW_KEY_NORMAL = "asdjrt4sd";
private static BlowfishECB normalECB = null;
static {
psdECB = new BlowfishECB(BW_KEY_PWD.getBytes());
normalECB = new BlowfishECB(BW_KEY_NORMAL.getBytes());
}
public String decrypt(String strContent) {
return decrypt(strContent, EN_TYPE_PWD);
}
public String encrypt(String strContent) {
return encrypt(strContent, EN_TYPE_PWD);
}
public String encrypt(String strContent, String strType) {
BlowfishECB ecb = null;
if (EN_TYPE_PWD.equalsIgnoreCase(strType)) {
ecb = psdECB;
} else {
ecb = normalECB;
}
return encrypt(strContent, ecb);
}
//encryption method
private String encrypt(String strContent, BlowfishECB ecb) {
byte[] buf = strContent.getBytes();
int iLength = buf.length;
byte[] theBuf = null;
if ((iLength & 7) == 0) {
theBuf = new byte[iLength];
} else {
theBuf = new byte[(iLength & (~7)) + 8];
}
System.arraycopy(buf, 0, theBuf, 0, buf.length);
ecb.encrypt(theBuf);
String strResult = new String(theBuf, 0, theBuf.length);
strResult = base64Encode(theBuf);
return strResult;
}
public static String base64Encode(byte[] bt) {
BASE64Encoder en = new BASE64Encoder();
return en.encode(bt);
}
public static String decrypt(String strContent, String strType) {
BlowfishECB ecb = null;
if (EN_TYPE_PWD.equalsIgnoreCase(strType)) {
ecb = psdECB;
} else {
ecb = normalECB;
}
return decrypt(strContent, ecb);
}
//decryption method
private static String decrypt(String strContent, BlowfishECB ecb) {
byte[] buf = base64Decode(strContent);
int iLength = buf.length;
byte[] theBuf = new byte[iLength];
if (iLength % 8 != 0) {
return "";
}
System.arraycopy(buf, 0, theBuf, 0, buf.length);
for (int i = 0; i < theBuf.length; i++) {
theBuf[i] = 0x20;
}
ecb.decrypt(buf, theBuf);
iLength = 0;
for (int i = theBuf.length - 1; i >= 0; i--) {
if ((int) theBuf[i] != 0) {
iLength = i + 1;
break;
}
}
String strResult = new String(theBuf, 0, iLength);
return strResult;
}
public static byte[] base64Decode(String arg) {
BASE64Decoder bd = new BASE64Decoder();
byte[] result = null;
try {
result = bd.decodeBuffer(arg);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String args[]) {
EncryptionUtils encyptUtils = new EncryptionUtils();
Scanner scanstr = null;
while (true) {
scanstr = new Scanner(System.in);
System.out
.println("Please enter E to encrypt and D to decrypt text(type exit to close the util)");
String flag = scanstr.nextLine();
if (flag.equals("E")) {
System.out.println("Please enter text to encrypt");
String toEncrypt = scanstr.nextLine();
System.out.println("encrypted text*********>"
+ encyptUtils.encrypt(toEncrypt));
} else if (flag.equals("D")) {
System.out.println("Please enter text to decrypt");
String toDecrypt = scanstr.nextLine();
System.out.println("deencrpted text*********>"
+ encyptUtils.decrypt(toDecrypt));
}
else if (flag.equals("exit")) {
break;
} else {
System.out.println("Invalid input");
System.out
.println("Please enter E to encrypt and D to decrypt text");
}
}
}
}