Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Program:Encryption and Decryption of String/Bytes with RSA Algorithm  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
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 r_muthiahraja@yahoo.co.in
*/
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!