Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Cryptography Fundamentals: Encryption, Signatures, and Verification

Tech May 19 2

Core Cryptographic Concepts

Modern systems rely on cryptographic techniques like symmetric encryption, asymmetric encryption, and digital signatures. These mechanisms secure data transmission, authenticate identities, and ensure message integrity.

Symmetric Encryption

Symmetric algorithms use identical keys for encryption and decryption. Common implementations include:

DES Implementation

public class SymmetricCipher {
  private static final String ALGORITHM = "DES";

  public static String encode(String input, String secret) throws Exception {
    byte[] processed = processData(input.getBytes(), secret.getBytes(), Cipher.ENCRYPT_MODE);
    return Base64.getEncoder().encodeToString(processed);
  }

  public static String decode(String encoded, String secret) throws Exception {
    byte[] data = Base64.getDecoder().decode(encoded);
    byte[] result = processData(data, secret.getBytes(), Cipher.DECRYPT_MODE);
    return new String(result);
  }

  private static byte[] processData(byte[] data, byte[] secret, int operation) throws Exception {
    SecureRandom secureRandom = new SecureRandom();
    DESKeySpec keySpec = new DESKeySpec(secret);
    SecretKey secretKey = SecretKeyFactory.getInstance(ALGORITHM).generateSecret(keySpec);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(operation, secretKey, secureRandom);
    return cipher.doFinal(data);
  }
}

Note: DES keys truncate to 8 bytes regardless of input length.

AES Implementation

public class AESHandler {
  public static String encrypt(String plaintext, String secretKey) throws Exception {
    if (secretKey.length() != 16) throw new IllegalArgumentException("Key must be 16 bytes");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, createKey(secretKey));
    return Base64.getEncoder().encodeToString(cipher.doFinal(plaintext.getBytes(UTF_8)));
  }

  public static String decrypt(String ciphertext, String secretKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, createKey(secretKey));
    return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), UTF_8);
  }

  private static SecretKeySpec createKey(String key) {
    return new SecretKeySpec(key.getBytes(UTF_8), "AES");
  }
}
Cipher Configuration 16-byte Input Partial Block
AES/CBC/NoPadding 16 bytes Unsupported
AES/ECB/PKCS5Padding 32 bytes 16 bytes
AES/CFB/NoPadding 16 bytes Input length

Asymmetric Encryption

Uses key pairs: public key for encryption/verification, private key for decryption/signing.

RSA Operations

public class AsymmetricCrypto {
  private static final String ALGORITHM = "RSA";

  public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
    KeyPairGenerator generator = KeyPairGenerator.getInstance(ALGORITHM);
    generator.initialize(2048);
    return generator.generateKeyPair();
  }

  public static byte[] encrypt(PublicKey publicKey, String message) throws Exception {
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    return cipher.doFinal(message.getBytes(UTF_8));
  }

  public static String sign(String data, PrivateKey privateKey) throws Exception {
    Signature signer = Signature.getInstance("SHA256WithRSA");
    signer.initSign(privateKey);
    signer.update(data.getBytes(UTF_8));
    return Base64.getEncoder().encodeToString(signer.sign());
  }

  public static boolean verify(String data, PublicKey publicKey, String signature) throws Exception {
    Signature verifier = Signature.getInstance("SHA256WithRSA");
    verifier.initVerify(publicKey);
    verifier.update(data.getBytes(UTF_8));
    return verifier.verify(Base64.getDecoder().decode(signature));
  }
}

Hash Functions

One-way transformations that produce fixed-size digests for data integrity verification.

public static String computeHash(String input) throws Exception {
  MessageDigest md = MessageDigest.getInstance("SHA-256");
  byte[] digest = md.digest(input.getBytes());
  return String.format("%064x", new BigInteger(1, digest));
}

Common Algorithms

  • MD5: 128-bit digest (deprecated for security)
  • SHA-1: 160-bit digest
  • SHA-256: 256-bit digest
  • SHA-512: 512-bit digest

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.