Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What should i use? RSA/NONE/PKCS1PADDING not working jre1.5

843811Dec 17 2005 — edited Dec 18 2005
hi im new to encryption and im using an example off the net, problem is it's not working throwing NoSuchAlgorithmException:
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.*;
import javax.crypto.Cipher;

public class AsymmetricCipherTest {
  private static byte[] encrypt(byte[] inpBytes , PublicKey key,
      String xform) throws Exception {
    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(inpBytes);
  }
  private static byte[] decrypt(byte[] inpBytes, PrivateKey key,
      String xform) throws Exception{
    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.DECRYPT_MODE, key);
    return cipher.doFinal(inpBytes);
  }

  public static void main(String[] unused) throws Exception {
Provider p = new com.sun.crypto.provider.SunJCE();
Security.addProvider(p);

    String xform = "RSA/NONE/PKCS1PADDING";
    // Generate a key-pair
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(512); // 512 is the keysize.
    KeyPair kp = kpg.generateKeyPair();
    PublicKey pubk = kp.getPublic();
    PrivateKey prvk = kp.getPrivate();
    System.out.println("Public key - " + pubk);
    System.out.println("Private key - " + prvk);
    byte[] dataBytes =
        "i am a secret key abcdefg".getBytes();

    byte[] encBytes = encrypt(dataBytes, pubk, xform);
    byte[] decBytes = decrypt(encBytes, prvk, xform);

    boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
    System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));
  }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 15 2006
Added on Dec 17 2005
1 comment
1,083 views