Cybersecurity
Cybersecurity involves protecting computer systems and networks from theft, damage, and unauthorized access. It typically involves using cybersecurity tools and techniques such as firewalls, encryption, and penetration testing to secure software applications.
Example of using encryption to secure sensitive data in Java:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Encryption {
public static byte[] encrypt(String text) throws Exception {
// Generate a secret key
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(
SecretKey secretKey = keyGen.generateKey();
// Create a cipher object and initialize it with the secret key
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// Encrypt the text
byte[] encryptedText = cipher.doFinal(text.getBytes());
return encryptedText;
}
public static String decrypt(byte[] encryptedText) throws Exception {
// Generate a secret key
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
// Create a cipher object and initialize it with the secret key
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
// Decrypt the text
byte[] decryptedText = cipher.doFinal(encryptedText);
return new String(decryptedText);
}
public static void main(String[] args) throws Exception {
String text = "secret message";
// Encrypt the text
byte[] encryptedText = encrypt(text);
System.out.println("Encrypted text: " + new String(encryptedText));
// Decrypt the text
String decryptedText = decrypt(encryptedText);
System.out.println("Decrypted text: " + decryptedText);
}
}
No comments:
Post a Comment