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!"));
}
}