Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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
*/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!