Skip to content

Instantly share code, notes, and snippets.

@marlonlom
Last active October 2, 2017 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marlonlom/8f02c647fdd6089ec6f9fc1dc6674aa9 to your computer and use it in GitHub Desktop.
Save marlonlom/8f02c647fdd6089ec6f9fc1dc6674aa9 to your computer and use it in GitHub Desktop.
AES 128bit Cross Platform (Java) Encryption Compatibility
/*
* Copyright (c) 2017, marlonlom
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
* Utility class for text encryption, using Advanced Encryption Standard (AES).
*
* @author marlonlom
*/
public final class Aes128EncryptionCompat {
/** The Constant LOGGER. */
private static final Logger LOGGER = Logger
.getLogger(Aes128EncryptionCompat.class.getSimpleName());
/** The Constant characterEncoding. */
private final static String characterEncoding = "UTF-8";
/** The Constant cipherTransformation. */
private final static String cipherTransformation = "AES/CBC/PKCS5Padding";
/** The Constant aesEncryptionAlgorithm. */
private final static String aesEncryptionAlgorithm = "AES";
/**
* Decrypt.
*
* @param cipherText
* the cipher text
* @param key
* the key
* @param initialVector
* the initial vector
* @return the byte[]
* @throws GeneralSecurityException
* the general security exception
*/
public static byte[] decrypt(byte[] cipherText, byte[] key,
byte[] initialVector) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance(cipherTransformation);
SecretKeySpec secretKeySpecy = new SecretKeySpec(key,
aesEncryptionAlgorithm);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
cipherText = cipher.doFinal(cipherText);
return cipherText;
}
/**
* Decrypts a base64 encoded string using the given key (AES 128bit key and
* a Chain Block Cipher).
*
* @param encryptedText
* base64 encoded text
* @param key
* secret key
* @return Decrypted text
*/
public static String decrypt(String encryptedText, String key) {
try {
byte[] cipheredBytes = Base64.decodeBase64(encryptedText);
byte[] keyBytes = getKeyBytes(key);
return new String(decrypt(cipheredBytes, keyBytes, keyBytes),
characterEncoding);
} catch (IOException e) {
LOGGER.severe(e.getMessage());
} catch (GeneralSecurityException e) {
LOGGER.severe(e.getMessage());
}
return Utilidades.STRING_EMPTY;
}
/**
* Encrypt.
*
* @param plainText
* the plain text
* @param key
* the key
* @param initialVector
* the initial vector
* @return the byte[]
* @throws GeneralSecurityException
* the general security exception
*/
public static byte[] encrypt(byte[] plainText, byte[] key,
byte[] initialVector) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance(cipherTransformation);
SecretKeySpec secretKeySpec = new SecretKeySpec(key,
aesEncryptionAlgorithm);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
plainText = cipher.doFinal(plainText);
return plainText;
}
/**
* Encrypts plain text using AES 128bit key and a Chain Block Cipher and
* returns a base64 encoded string.
*
* @param plainText
* plain text to encrypt
* @param key
* secret key
* @return Base64 encoded string
*/
public static String encrypt(String plainText, String key) {
try {
byte[] plainTextbytes = plainText.getBytes(characterEncoding);
byte[] keyBytes = getKeyBytes(key);
return Base64.encodeBase64String(encrypt(plainTextbytes, keyBytes,
keyBytes));
} catch (IOException e) {
LOGGER.severe(e.getMessage());
} catch (GeneralSecurityException e) {
LOGGER.severe(e.getMessage());
}
return Utilidades.STRING_EMPTY;
}
/**
* Gets the key bytes.
*
* @param key
* the key
* @return the key bytes
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private static byte[] getKeyBytes(String key) throws IOException {
byte[] keyBytes = new byte[16];
byte[] parameterKeyBytes = key.getBytes(characterEncoding);
System.arraycopy(parameterKeyBytes, 0, keyBytes, 0,
Math.min(parameterKeyBytes.length, keyBytes.length));
return keyBytes;
}
}
/*
* Copyright (c) 2017, marlonlom
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
public class Demo {
public static void main(String args[]) {
String secret="SI##F.WjRAflw$hFOLRj>3.G{1cF2<v#T[&q0*@Uv?o\"<Jrz]GmRLm*-q0+D<|A";
String username="cisa";
String password="CiSa2017";
final String encUsername=Aes128EncryptionCompat.encrypt(username,secret);
final String encPassword=Aes128EncryptionCompat.encrypt(password,secret);
System.out.println("encrypt(username) = " + encUsername+ "\nencrypt(password) = " + encPassword);
final String decUsername=Aes128EncryptionCompat.decrypt(encUsername,secret);
final String decPassword=Aes128EncryptionCompat.decrypt(encPassword,secret);
System.out.println("decrypt(username) = " + decUsername+ "\ndecrypt(password) = " + decPassword);
}
}
@marlonlom
Copy link
Author

AES 128bit Cross Platform (Java) Encryption Compatibility

From related website at the bottom, it performs encryption between two programming languages.

For Java, the above code was adjusted by reducing the exceptions related to security, using the GeneralSecurityException class.

External dependency

It uses Base64 class from apache commons codec library

Related website

https://zenu.wordpress.com/2011/09/21/aes-128bit-cross-platform-java-and-c-encryption-compatibility/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment