Created
September 27, 2013 10:08
-
-
Save nori3tsu/6726470 to your computer and use it in GitHub Desktop.
AES/ECB/PKCS5Padding
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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.