Common

UUID

HTTP Request

Cryptography

Common

Reflection

IO

RsaCrypto Tutorial

This tutorial provides an overview of the RsaCrypto class, which is part of the lightweight-component/aj-util library. RsaCrypto offers a collection of utility methods for asymmetric encryption, decryption, and digital signatures using the RSA algorithm.

Introduction

The RsaCrypto class provides methods for generating RSA key pairs, encrypting and decrypting data using public and private keys, and creating and verifying digital signatures. RSA is an asymmetric cryptographic algorithm that uses a pair of keys: a public key for encryption and a private key for decryption.

Methods

1. init()

Initializes and returns an RSA key pair with a key size of 1024 bits.

Example:

Map<String, byte[]> keyPair = RsaCrypto.init();

2. getPublicKey(Map<String, byte[]> map) and getPrivateKey(Map<String, byte[]> map)

Retrieves the public or private key from a key pair map as a Base64 encoded string.

Example:

Map<String, byte[]> keyPair = RsaCrypto.init();
String publicKey = RsaCrypto.getPublicKey(keyPair);
String privateKey = RsaCrypto.getPrivateKey(keyPair);

3. sign(String privateKey, byte[] data)

Creates a digital signature for the provided data using the private key.

Example:

Map<String, byte[]> keyPair = RsaCrypto.init();
String privateKey = RsaCrypto.getPrivateKey(keyPair);
String signature = RsaCrypto.sign(privateKey, "Hello, World!".getBytes());

4. verify(byte[] data, String publicKey, String sign)

Verifies a digital signature using the public key.

Example:

Map<String, byte[]> keyPair = RsaCrypto.init();
String publicKey = RsaCrypto.getPublicKey(keyPair);
String privateKey = RsaCrypto.getPrivateKey(keyPair);

byte[] data = "Hello, World!".getBytes();
String signature = RsaCrypto.sign(privateKey, data);

boolean isValid = RsaCrypto.verify(data, publicKey, signature);
// isValid should be true

5. Public Key Operations: encryptByPublicKey(byte[] data, String key) and decryptByPublicKey(byte[] data, String key)

Encrypts or decrypts data using the public key.

Example:

Map<String, byte[]> keyPair = RsaCrypto.init();
String publicKey = RsaCrypto.getPublicKey(keyPair);

byte[] data = "Hello, World!".getBytes();
byte[] encrypted = RsaCrypto.encryptByPublicKey(data, publicKey);

6. Private Key Operations: encryptByPrivateKey(byte[] data, String key) and decryptByPrivateKey(byte[] data, String key)

Encrypts or decrypts data using the private key.

Example:

Map<String, byte[]> keyPair = RsaCrypto.init();
String privateKey = RsaCrypto.getPrivateKey(keyPair);
String publicKey = RsaCrypto.getPublicKey(keyPair);

// Encrypt with public key
byte[] data = "Hello, World!".getBytes();
byte[] encrypted = RsaCrypto.encryptByPublicKey(data, publicKey);

// Decrypt with private key
byte[] decrypted = RsaCrypto.decryptByPrivateKey(encrypted, privateKey);
// decrypted should be equal to the original data

Conclusion

The RsaCrypto class provides a comprehensive set of tools for RSA asymmetric encryption, decryption, and digital signatures. It offers methods for key pair generation, data encryption/decryption using both public and private keys, and signature creation/verification. Remember to store private keys securely and follow best practices for cryptographic implementations.