When we do projects, we usually link the front and back ends through data transmission, so if we encounter sensitive data, encryption is very important. In order to protect the security and confidentiality of the data, we usually use encryption to encrypt the data. A common encryption algorithm is AES encryption. The following will introduce in detail how to use AES encryption at the front end and back end to protect data security.
Introduction to AES encryption
AES (Advanced Encryption Standard) is a symmetric encryption algorithm in which the same key is used to encrypt and decrypt data. In AES encryption, data is divided into blocks and encrypted one by one, each block is encrypted with the same key, and then transmitted to the recipient for decryption. That is to say, encryption and decryption use the same key. The AES encryption algorithm has the characteristics of high efficiency, security, and reliability, and is widely used in the field of data encryption. The AES algorithm has three key lengths: 128 bits, 192 bits, and 256 bits. Among them, 128-bit key length is widely used because it provides sufficient security and has high encryption efficiency.
An example of encrypting data on the Android frontend and encrypting data on the PHP backend:
In Android, you can use the AES encryption algorithm in the javax.crypto library. The following is a sample code snippet to encrypt and send string data to backend server:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;
public class AES {
private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String CHARSET = "UTF-8";
public static String encrypt(String input, String key, String iv) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(CHARSET));
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] encrypted = cipher.doFinal(input.getBytes(CHARSET));
return Base64.encodeToString(encrypted, Base64.DEFAULT);
}
}
function decrypt($input, $key, $iv) {
$data = base64_decode($input);
$decrypted = openssl_decrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
return $decrypted;
}
function encrypt($data, $key, $iv) {
$method = "AES-256-CBC";
$options = OPENSSL_RAW_DATA;
$encrypted = openssl_encrypt($data, $method, $key, $options, $iv);
return base64_encode($encrypted);
}
function generateIV() {
$ivLength = openssl_cipher_iv_length("AES-256-CBC");
return openssl_random_pseudo_bytes($ivLength);
}
$key = "0123456789abcdef0123456789abcdef";
$iv = generateIV();
$data = array(
"name" => "John Doe",
"email" => "johndoe@example.com",
"age" => 30
);
$jsonData = json_encode($data);
$encryptedData = encrypt($jsonData, $key, $iv);
echo "Encrypted JSON data: ".$encryptedData."";
echo "IV: ".base64_encode($iv)."";
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;
import org.json.JSONObject;
public class AES {
public static String decrypt(String encryptedData, String key, String iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
byte[] keyBytes = key.getBytes("UTF-8");
byte[] ivBytes = Base64.decode(iv, Base64.DEFAULT);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decryptedBytes = cipher.doFinal(Base64.decode(encryptedData, Base64.DEFAULT));
return new String(decryptedBytes, "UTF-8");
}
public static void main(String[] args) throws Exception {
String encryptedJSONData = "ENCIPHERED_JSON_DATA";
String iv = "INITIALIZATION_VECTOR";
String key = "0123456789abcdef0123456789abcdef";
// Decrypt JSON data
String decryptedJSONData = decrypt(encryptedJSONData, key, iv);
// Convert to JSON object
JSONObject jsonObject = new JSONObject(decryptedJSONData);
// Output the fields of the JSON object
System.out.println(jsonObject.getString("name"));
System.out.println(jsonObject.getString("email"));
System.out.println(jsonObject.getInt("age"));
}
}
0 Comments