java.security.interfaces


1. Verifying a Digital Signature

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;

public class VerifySignature {

    public static boolean verify(byte[] signatureBytes, byte[] message, PublicKey publicKey) throws Exception {
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initVerify(publicKey);
        signature.update(message);
        return signature.verify(signatureBytes);
    }
}

2. Encrypting a Secret Key Using a Public Key

import java.security.Cipher;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;

public class EncryptSecretKey {

    public static byte[] encrypt(byte[] secretKey, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(secretKey);
    }
}

3. Decrypting a Secret Key Using a Private Key

4. Generating a Diffie-Hellman Key Pair

5. Calculating the Diffie-Hellman Secret

6. Generating an RSA Key Pair

7. Generating an AES Key Pair

8. Encrypting Data with an AES Key

9. Decrypting Data with an AES Key

10. Hashing Data with SHA-256

11. Signing Data with an RSA Private Key

12. Verifying a Signature with an RSA Public Key

13. Generating an ECDSA Key Pair

14. Signing Data with an ECDSA Private Key

15. Verifying a Signature with an ECDSA Public Key

16. Generating a DSA Key Pair

17. Signing Data with a DSA Private Key

18. Verifying a Signature with a DSA Public Key

19. Generating a DH Key Pair

20. Agreeing on a Secret Key with DH

21. Generating an ElGamal Key Pair

22. Encrypting Data with an ElGamal Public Key

23. Decrypting Data with an ElGamal Private Key

24. Generating a DES Key Pair

25. Encrypting Data with a DES Key

26. Decrypting Data with a DES Key

27. Generating a TripleDES Key Pair

28. Encrypting Data with a TripleDES Key

29. Decrypting Data with a TripleDES Key