This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging
preface
Data encryption is a time-honored technology that uses an encryption algorithm and key to convert plaintext into ciphertext, while decryption uses an algorithm and key to restore ciphertext to plaintext. At its core is cryptography. Data encryption is still one of the most reliable ways for computer systems to protect information. It uses cryptographic technology to encrypt information and realize information concealment so as to protect the security of information.
Encryption is required before file transfer using sockets in a project. The realization of the process of stepping on some pits, the next to the realization of the process for a summary.
DES encryption
As the encryption process is used in the DES encryption algorithm, the following post DES encryption code:
// The secret key algorithm
private static final String KEY_ALGORITHM = "DES";
// Encryption algorithm: algorithm/mode/padding Algorithm/working mode/padding mode
private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
/ / the secret key
private static final String KEY = "12345678";// The DES key must contain 8 bits
public static void main(String args[]) {
String data = "Encryption and decryption";
KLog.d("Encrypted data:" + data);
byte[] encryptData = encrypt(data.getBytes());
KLog.d("Encrypted data:" + new String(encryptData));
byte[] decryptData = decrypt(encryptData);
KLog.d("Decrypted data:" + new String(decryptData));
}
public static byte[] encrypt(byte[] data) {
// Initialize the secret key
SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(data);
return Base64.getEncoder().encode(result);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] decrypt(byte[] data) {
byte[] resultBase64 = Base64.getDecoder().decode(data);
SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(resultBase64);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Copy the code
Output:
Encrypted data: encryption and decryption Encrypted data: rt6XE06pElmLZMaVxrbfCQ== Decrypted data: encryption and decryptionCopy the code
Socket client part code:
Socket socket = new Socket(ApiConstants.HOST, ApiConstants.PORT);
OutputStream outStream = socket.getOutputStream();
InputStream inStream = socket.getInputStream();
RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
fileOutStream.seek(0);
byte[] buffer = new byte[1024];
int len = -1;
while(((len = fileOutStream.read(buffer)) ! = -1)) {
outStream.write(buffer, 0, len); // This is where the byte stream is transmitted
}
fileOutStream.close();
outStream.close();
inStream.close();
socket.close();
Copy the code
Socket server part code:
Socket socket = server.accept(); InputStream inStream = socket.getInputStream(); OutputStream outStream = socket.getOutputStream(); outStream.write(response.getBytes("UTF-8")); RandomAccessFile fileOutStream = new RandomAccessFile(file, "rwd"); fileOutStream.seek(0); byte[] buffer = new byte[1024]; int len; while ((len = inStream.read(buffer)) ! = -1) {fileoutStream. write(buffer, 0, len); } fileOutStream.close(); inStream.close(); outStream.close(); socket.close();Copy the code