WikiGalaxy

Personalize

Data Encryption and Secure Data Transmission

Understanding Data Encryption

Data encryption is a critical component in securing data transmission. It involves converting data into a coded format to prevent unauthorized access.

  • Symmetric Encryption
  • Uses the same key for both encryption and decryption. It's fast but requires secure key distribution.

  • Asymmetric Encryption
  • Utilizes a pair of keys - public and private. It is more secure but slower than symmetric encryption.

  • Hash Functions
  • Transforms data into a fixed-size hash value, ensuring data integrity.

Example: Symmetric Encryption with AES


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class SymmetricEncryption {
    public static void main(String[] args) throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        SecretKey secretKey = keyGen.generateKey();
        
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        
        String plainText = "SecureData";
        byte[] encryptedText = cipher.doFinal(plainText.getBytes());
        System.out.println(new String(encryptedText));
    }
}
        

Symmetric Encryption with AES

This example demonstrates how to encrypt data using the AES algorithm, a popular symmetric encryption technique.

Example: Asymmetric Encryption with RSA


import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;

public class AsymmetricEncryption {
    public static void main(String[] args) throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        KeyPair pair = keyGen.generateKeyPair();
        PublicKey publicKey = pair.getPublic();
        PrivateKey privateKey = pair.getPrivate();
        
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        
        String plainText = "SecureData";
        byte[] encryptedText = cipher.doFinal(plainText.getBytes());
        System.out.println(new String(encryptedText));
    }
}
        

Asymmetric Encryption with RSA

RSA is a widely used asymmetric encryption algorithm that uses a pair of keys for secure data transmission.

Example: Hashing with SHA-256


import java.security.MessageDigest;

public class HashingExample {
    public static void main(String[] args) throws Exception {
        String data = "SecureData";
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] hash = md.digest(data.getBytes());
        
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            hexString.append(Integer.toHexString(0xFF & b));
        }
        System.out.println(hexString.toString());
    }
}
        

Hashing with SHA-256

SHA-256 is a cryptographic hash function that ensures data integrity by producing a unique hash value for a given input.

Example: Digital Signatures


import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Signature;

public class DigitalSignatureExample {
    public static void main(String[] args) throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
        KeyPair pair = keyGen.generateKeyPair();
        PrivateKey privateKey = pair.getPrivate();
        
        Signature sign = Signature.getInstance("SHA256withDSA");
        sign.initSign(privateKey);
        
        String data = "SecureData";
        sign.update(data.getBytes());
        
        byte[] digitalSignature = sign.sign();
        System.out.println(new String(digitalSignature));
    }
}
        

Digital Signatures

Digital signatures provide data authenticity and integrity by using cryptographic algorithms to sign data.

Example: Secure Socket Layer (SSL)


// SSL example is theoretical as Java code for SSL setup is complex
// SSL/TLS is used for secure communication over a computer network
        

Secure Socket Layer (SSL)

SSL/TLS protocols are used to establish a secure connection between a client and server, ensuring data is transmitted securely.

Example: Transport Layer Security (TLS)


// TLS example is theoretical as Java code for TLS setup is complex
// TLS is an updated, more secure version of SSL
        

Transport Layer Security (TLS)

TLS is an enhanced version of SSL, providing improved security features for data transmission over networks.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025