import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/*...*/
public static String encrypt(String key, String clearText) throws Exception{
Cipher c = Cipher.getInstance(RC4_NAME);
Key mainKey = new SecretKeySpec(key.getBytes(), "RC4");
c.init(Cipher.ENCRYPT_MODE, mainKey);
byte[] encVal = c.doFinal(clearText.getBytes(UNICODE));
String encryptedValue = Base64.encodeToString(encVal, false);
return encryptedValue;
}
public static String decrypt(String key, String encryptedData) throws Exception {
Cipher c = Cipher.getInstance(RC4_NAME);
Key mainKey = new SecretKeySpec(key.getBytes(), "RC4");
c.init(Cipher.DECRYPT_MODE, mainKey);
byte[] decordedValue = Base64.decode(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue, UNICODE);
return decryptedValue;
}
Comments