The code in Listing 2-2 implements AES 128-bit encryption. Test the encryption and decryption code in Eclipse. The Activator class, created by Eclipse, must exist in the plug-in.
private final byte[] key = {-0x6A, 0x6D, 0x49, -0x05, 0x79, 0x38, 0x48, -0x0C, 0x6A, 0x19, 0x46, 0x1E, -0x09, -0x5E, -0x2F, 0x17};
private Cipher getCipher(int mode) {
SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(mode, keyspec);
} catch (Exception ex) {}
public String decrypt(String encryptedString) {
String decryptedString = null;
if (encryptedString == null) return null;
if ("".equals(encryptedString)) return "";
byte[] encryptedBytes = new BASE64Decoder().decodeBuffer(encryptedString);
Cipher cipher = getCipher(Cipher.DECRYPT_MODE);
byte[] raw = cipher.doFinal(encryptedBytes);
decryptedString = new String(raw);
} catch (Exception ex) {}
public String encrypt(String plainText) {
String encryptedString = null;
if (plainText == null) return null;
if ("".equals(plainText)) return "";
Cipher cipher = getCipher(Cipher.ENCRYPT_MODE);
byte[] raw = cipher.doFinal(plainText.getBytes());
encryptedString = new BASE64Encoder().encode(raw);
} catch (Exception ex) {}