EncryptDecryptTool helps to encrypt and decrypt your important text like password's.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 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" ); } } } } |