/*
Program:Encryption and Decryption of String/Bytes with RSA Algorithm
--------------------------------------------------------------
While running this code following errors occurs
*/
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.PrivateKey;
import javax.crypto.Cipher;
import java.security.Security;
import java.security.Provider;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
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 args[]) throws Exception
{
Provider prov=new org.bouncycastle.jce.provider.BouncyCastleProvider();
Security.addProvider(prov);
String xform = "RSA/NONE/PKCS1PADDING";
System.out.println("First");
// 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();
byte[] dataBytes = "J2EE Security for
Servlets, EJBs and Web Services".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!"));
}
}
/*
Error
-----
AysmmetricChipherTest.java:8: package org.bouncycastle.jce.provider.BouncyCastleProvider does not exsist
AysmmetricChipherTest.java:26: package org.bouncycastle.jce.provider.BouncyCastleProvider does not exsist
Provider prov=new org.bouncycastle.jce.provider.BouncyCastleProvider();
2 errors ^
*/
/*
If any external sofware need to support BouncyCastleProvider,send the particular package with your reply.
I request you to not only rectifying errors and also run this program succesful manner.
I need
java code for running above program using DDDS algorithm.
Kindly help me through
[email protected] */