I am trying to send protected text over the network encryption it with AES algorithm and Base64. For several text that I tried it works fine, but there are a few strings that are not properly decoded. I found a pattern on the string that could not be decoded. They have what it seems to be a breakline on them. Example: Encryption Steps:
1) PlainText
2) AES Encrypted Text:
Íe l1 Ÿ?‰|6ÎÔ.jcæ÷Ï_ðNSO˜A’q@Ó[
3) BASE64 AES Text:
zWUNbDEgnz+JfDbO1C5qY+b3z1/wTlNPmAdBknFA01s=
Decryption Steps:
1) BASE64 AES Text:
zWUNbDEgnz+JfDbO1C5qY+b3z1/wTlNPmAdBknFA01s=
2) AES Encrypted Text:
Íe l1 Ÿ?‰|6ÎÔ.jcæ÷Ï_ðNSO˜A’q@Ó[
3) Plain Text:
í¿Ð“tº„£¹ÍG‰\îœ
Any other text that I encrypt that does not contains breaklines on it encrypted version is properly decrypted.
Ex:
Encryption Steps:
1) Plain Text: P@$$w0rd
2) AES Encrypted Text:
÷÷Odã29ôÐÑÌe£™ø
3) Base64 AES Text:
9xH3T2TjMjn00NHMZaOZ+A==
Decryption Steps:
3) Base64 AES Text:
9xH3T2TjMjn00NHMZaOZ+A==
2) AES Encrypted Text:
÷÷Odã29ôÐÑÌe£™ø
3) Plain Text:
P@$$w0rd
Below are the objects that I created for this purpose.
Base64Codec Object:
package security;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
public class Base64Codec
{
private static Base64Codec objInstance;
private Encoder objEncoder;
private Decoder objDecoder;
private Base64Codec()
{
this.setEncoder();
this.setDecoder();
}
public static Base64Codec getInstance()
{
if (objInstance == null)
{
objInstance = new Base64Codec();
}
return objInstance;
}
private void setEncoder()
{
this.objEncoder = Base64.getEncoder();
}
private Encoder getEncoder()
{
return this.objEncoder;
}
private void setDecoder()
{
this.objDecoder = Base64.getDecoder();
}
private Decoder getDecoder()
{
return this.objDecoder;
}
protected String encode(String strValue)
{
return this.getEncoder().encodeToString(strValue.getBytes());
}
protected String decode(String strValue)
{
return new String(this.getDecoder().decode(strValue));
}
}
AdvancedEncryptionStandard Object
package security;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class AdvancedEncryptionStandard
{
private static final String strKey = "02E68E02BE7400FE11E8A45B60017F98";
private static final byte[] bytKey = strKey.getBytes();
private static final String strAlgorithm = "AES";
private static AdvancedEncryptionStandard objInstance;
private Cipher objCipher;
private SecretKeySpec objSecretKey;
private AdvancedEncryptionStandard() throws NoSuchAlgorithmException, NoSuchPaddingException
{
this.setSecretKey();
this.setCipher();
}
public static AdvancedEncryptionStandard getInstance() throws NoSuchAlgorithmException, NoSuchPaddingException
{
if (objInstance == null)
{
objInstance = new AdvancedEncryptionStandard();
}
return objInstance;
}
private byte[] getKey()
{
return bytKey;
}
private String getAlgorithm()
{
return strAlgorithm;
}
private void setSecretKey()
{
this.objSecretKey = new SecretKeySpec(this.getKey(), this.getAlgorithm());
}
private SecretKeySpec getSecretKey()
{
return this.objSecretKey;
}
private void setCipher() throws NoSuchAlgorithmException, NoSuchPaddingException
{
this.objCipher = Cipher.getInstance(this.getAlgorithm());
}
private Cipher getCipher()
{
return this.objCipher;
}
protected String encrypt(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
this.getCipher().init(Cipher.ENCRYPT_MODE, this.getSecretKey());
return new String(this.getCipher().doFinal(strValue.getBytes()));
}
protected String decrypt(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
this.getCipher().init(Cipher.DECRYPT_MODE, this.getSecretKey());
return new String(this.getCipher().doFinal(strValue.getBytes()));
}
}
SecureText Object
package security;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class SecureText
{
private static SecureText objInstance;
private SecureText()
{
}
public static SecureText getInstance()
{
if (objInstance == null)
{
objInstance = new SecureText();
}
return objInstance;
}
public String encode(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
{
return Base64Codec.getInstance().encode(AdvancedEncryptionStandard.getInstance().encrypt(strValue));
}
public String decode(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
{
return AdvancedEncryptionStandard.getInstance().decrypt(Base64Codec.getInstance().decode(strValue));
}
}
Test:
public static void main(String[] args)
{
//Test SecureText Object. AES and Base64
String strSecureText = <plain text>
System.out.println(strSecureText);
System.out.println(SecureText.getInstance().decode(strSecureText));
//Test AES and Base64 separated
String strCoded;
String strEncrypted;
String strDecrypted;
String strDecoded;
strEncrypted = AdvancedEncryptionStandard.getInstance().encrypt(<plain text>);
System.out.println(strEncrypted);
strCoded = Base64Codec.getInstance().encode(strEncrypted);
System.out.println(strCoded);
strDecoded = Base64Codec.getInstance().decode(strCoded);
System.out.println(strDecoded);
strDecrypted = AdvancedEncryptionStandard.getInstance().decrypt(strDecoded);
System.out.println(strDecrypted);
String strSecureText = SecureText.getInstance().encode(<plain text>);
}