RSA Signature Generation : RSA algorithm « Security « Java Tutorial






import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.Signature;

public class MainClass {
  public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");

    keyGen.initialize(512, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("SHA1withRSA", "BC");

    signature.initSign(keyPair.getPrivate(), new SecureRandom());

    byte[] message = "abc".getBytes();
    signature.update(message);

    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);
    System.out.println(signature.verify(sigBytes));
  }
}








36.38.RSA algorithm
36.38.1.Basic RSA example.
36.38.2.RSA example with random key generation.
36.38.3.RSA example with PKCS #1 Padding.
36.38.4.RSA example with OAEP Padding and random key generation.
36.38.5.An example of using RSA to encrypt a single asymmetric key.
36.38.6.Simple Digital Signature Example
36.38.7.Creates a 1024 bit RSA key pair and stores it to the filesystem as two files
36.38.8.RSA Signature Generation
36.38.9.An RSA sample application