Created
October 6, 2011 01:10
-
-
Save RichardHJensen/1266219 to your computer and use it in GitHub Desktop.
Replace the Sun proprietary classes with Apache Commons stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/main/java/com/rhjensen/encryption/AESEncryptor.java b/src/main/java/com/rhjensen/encryption/AESEncryptor.java | |
index 66313f9..6857481 100644 | |
--- a/src/main/java/com/rhjensen/encryption/AESEncryptor.java | |
+++ b/src/main/java/com/rhjensen/encryption/AESEncryptor.java | |
@@ -1,7 +1,6 @@ | |
package com.rhjensen.encryption; | |
-import sun.misc.BASE64Decoder; | |
-import sun.misc.BASE64Encoder; | |
+import org.apache.commons.codec.binary.Base64; | |
import javax.crypto.*; | |
import javax.crypto.spec.IvParameterSpec; | |
@@ -15,13 +14,15 @@ import java.security.NoSuchAlgorithmException; | |
public class AESEncryptor { | |
private Cipher encryptor; | |
private Cipher decryptor; | |
+ private Base64 base64codec; | |
public AESEncryptor(String sessionKey, String iv) { | |
byte[] keyBytes; | |
byte[] vectorBytes; | |
try { | |
- keyBytes = new BASE64Decoder().decodeBuffer(sessionKey); | |
- vectorBytes = new BASE64Decoder().decodeBuffer(iv); | |
+ base64codec = new Base64(); | |
+ keyBytes = base64codec.decode(sessionKey); | |
+ vectorBytes = base64codec.decode(iv); | |
encryptor = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
encryptor.init(Cipher.ENCRYPT_MODE, | |
new SecretKeySpec(keyBytes, "AES"), | |
@@ -38,8 +39,6 @@ public class AESEncryptor { | |
e.printStackTrace(); | |
} catch (InvalidAlgorithmParameterException e) { | |
e.printStackTrace(); | |
- } catch (IOException e) { | |
- e.printStackTrace(); | |
} | |
} | |
@@ -48,12 +47,12 @@ public class AESEncryptor { | |
byte[] utf8bytes = plainText.getBytes("utf-8"); | |
byte[] ciphertext = encryptor.doFinal(utf8bytes); | |
- return new BASE64Encoder().encode(ciphertext); | |
+ return base64codec.encodeToString(ciphertext); | |
} | |
public String decrypt(String cipherText) throws IOException, IllegalBlockSizeException, BadPaddingException { | |
// decode, decrypt, use bytes to create string | |
- byte[] encryptedBytes = new BASE64Decoder().decodeBuffer(cipherText); | |
+ byte[] encryptedBytes = base64codec.decode(cipherText); | |
byte[] plaintext = decryptor.doFinal(encryptedBytes); | |
return new String(plaintext); | |
} | |
diff --git a/src/test/java/com/rhjensen/encryption/AESEncryptorTest.java b/src/test/java/com/rhjensen/encryption/AESEncryptorTest.java | |
index 0799346..429ace8 100644 | |
--- a/src/test/java/com/rhjensen/encryption/AESEncryptorTest.java | |
+++ b/src/test/java/com/rhjensen/encryption/AESEncryptorTest.java | |
@@ -1,9 +1,9 @@ | |
package com.rhjensen.encryption; | |
+import org.apache.commons.codec.binary.Base64; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
-import sun.misc.BASE64Encoder; | |
import javax.crypto.KeyGenerator; | |
@@ -29,8 +29,8 @@ public class AESEncryptorTest { | |
byte[] keyBytes = KEY_GENERATOR.generateKey().getEncoded(); | |
byte[] vectorBytes = new byte[]{0x7F, 0x6E, 0x5D, 0x4C, 0x3B, 0x2A, 0x19, 0x08, | |
0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00}; | |
- SESSION_KEY = new BASE64Encoder().encode(keyBytes); | |
- VECTOR = new BASE64Encoder().encode(vectorBytes); | |
+ SESSION_KEY = new Base64().encodeToString(keyBytes); | |
+ VECTOR = new Base64().encodeToString(vectorBytes); | |
} | |
@Before |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment