Posts

Java - RSA Encryption and Decryption example - Asymmetric cryptography

Image
The RSA algorithm (Rivest-Shamir-Adleman) is a cryptographic algorithm that is used for specific security services or purposes, which enables public-key encryption and is widely used to secure sensitive data, particularly when it is being sent over an insecure network such as the HTTP. A public key is shared publicly, while a private key is secret and must not be shared with anyone. The following illustration highlights how asymmetric cryptography works: Example 1: The Cipher Type: RSA/ECB/PKCS1Padding import java.io.IOException; import java.security.GeneralSecurityException; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Arrays; import java.util.Base64; import javax.crypto

JAVA - AES Encryption and Decryption example - Symmetric cryptography

Image
Advanced Encryption Standard which is a symmetric encryption algorithm. AES encryption is used by the U.S. to secure sensitive but unclassified material, so we can say it is secure enough. Advanced Encryption Standard(AES) is a symmetric encryption algorithm. AES is the industry standard now as it allows 128-bit, 192-bit and 256-bit encryption. Symmetric encryption is very fast as compared to asymmetric encryption and is used in systems such as database systems. The following illustration highlights how symmetric cryptography works: Example: import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Arrays; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; public class AESDemo { private static SecretKeySpec secretKey; private static byte [] key; public static