Skip to content

Instantly share code, notes, and snippets.

@nori3tsu
Created September 27, 2013 10:08
Show Gist options
  • Save nori3tsu/6726470 to your computer and use it in GitHub Desktop.
Save nori3tsu/6726470 to your computer and use it in GitHub Desktop.
AES/ECB/PKCS5Padding
import java.security.GeneralSecurityException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AESCipher {
private SecretKeySpec key;
public AESCipher(String key) {
this.key = new SecretKeySpec(key.getBytes(), "AES");
}
public String encrypt(String str) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, this.key);
return Base64.encodeBase64String(cipher.doFinal(str.getBytes()));
}
public String decrypt(String str) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, this.key);
return new String(cipher.doFinal(Base64.decodeBase64(str)));
}
public static void main(String[] args) throws GeneralSecurityException {
String key = "1234567890123456";
String text = "nori3tsu";
AESCipher cipher = new AESCipher(key);
String enctext = cipher.encrypt(text);
System.out.println(enctext);
String dectext = cipher.decrypt(enctext);
System.out.println(dectext);
}
}
@mustafaozcaninfo
Copy link

Mustafa Özcan

Kişisel Blog makalelerimi SEO konusundaki deneyimleri paylaştığım blogum.Fırsatları ayağınıza getiren mustafa ozcan blog kişisel temalı yazılar ile gündemde yer alan güncel yazıları sizlere sunma fırsatı ile.

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