Created
November 30, 2017 07:53
-
-
Save minhphong306/957c4e8f4f1205fbe924fe64950a1b40 to your computer and use it in GitHub Desktop.
AES key generator (from string or none), encrypt and decrypt in java
This file contains hidden or 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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package network_security; | |
import java.io.UnsupportedEncodingException; | |
import java.security.InvalidKeyException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.security.spec.KeySpec; | |
import java.util.Arrays; | |
import java.util.Base64; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.SecretKey; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.PBEKeySpec; | |
import javax.crypto.spec.SecretKeySpec; | |
/** | |
* | |
* @author Admin | |
*/ | |
public class SymmetricCryptor { | |
//hằng xác định thuật toán mã hóa | |
private final String Algorithm = "AES"; | |
//khóa | |
private SecretKey secretKey; | |
//bộ sinh khóa theo thuật toán đã chọn | |
private KeyGenerator keyGen; | |
//bộ mã | |
private Cipher cipher; | |
public SymmetricCryptor() { | |
} | |
// Tạo khóa không sử dụng chuỗi | |
public void createSymetricKey() throws Exception { | |
//khởi tạo bộ sinh khóa | |
keyGen = KeyGenerator.getInstance(Algorithm); | |
//sinh khóa | |
secretKey = keyGen.generateKey(); | |
} | |
// Tạo khóa sử dụng chuỗi | |
public void createSymetricKeyFromPassword(String keystring) throws Exception { | |
byte[] key = new byte[16]; | |
key = keystring.getBytes("UTF-8"); | |
MessageDigest sha = MessageDigest.getInstance("SHA-1"); | |
key = sha.digest(key); | |
key = Arrays.copyOf(key, 16); | |
secretKey = new SecretKeySpec(key, "AES"); | |
} | |
public SecretKey getSecretKey() { | |
return secretKey; | |
} | |
public String encryptText(String msg, SecretKey key) | |
throws Exception { | |
//tạo bộ mã | |
cipher = Cipher.getInstance(Algorithm); | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
return Base64.getEncoder().encodeToString( | |
cipher.doFinal(msg.getBytes("UTF-8"))); | |
} | |
public String decryptText(String msg, SecretKey key) | |
throws Exception { | |
cipher = Cipher.getInstance(Algorithm); | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
return new String(cipher.doFinal( | |
Base64.getDecoder().decode(msg)), "UTF-8"); | |
} | |
public static void main(String[] args) throws Exception { | |
SymmetricCryptor SC = new SymmetricCryptor(); | |
SC.createSymetricKeyFromPassword("daicaphong"); | |
String msg = "Nguyễn Văn An"; | |
String encrypted_msg = | |
SC.encryptText(msg, SC.getSecretKey()); | |
System.out.println("Plain text: " + msg); | |
System.out.println("Encrypted text: " + encrypted_msg); | |
String decrypted_msg = | |
SC.decryptText(encrypted_msg, SC.getSecretKey()); | |
System.out.println("Decrypted text: " + decrypted_msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment