• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Encryption & Decryption Problem with "JDK default" provider

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below code throws the following exception
Exception in thread "main" java.lang.NullPointerException
at EncryptionProblem.crypt(EncryptionProblem.java:85)
at EncryptionProblem.<init>(EncryptionProblem.java:37)
at EncryptionProblem.main(EncryptionProblem.java:16)
Press any key to continue . . .
But runs fine if uncomment the lines 106 & 15,
line 15adds the BouncyCastle provider
line 106KeyFactory retrieves the Algorithm from BouncyCastle provider
Kindly help me executing the same above code without BouncyCastle provider
BELOW IS THE COMPLETE CODE ATTACHED (documented lines are deleted)

import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;

public class EncryptionProblem {
public static final String PUB_FILE = "publicKey.txt";
public static final String PRI_FILE = "privateKey.txt";
public static final int PRIVATE = 0;
public static final int PUBLIC = 1;
public static void main(String[] args) {
// Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
new EncryptionProblem();
}
public EncryptionProblem() {
KeyPairGenerator kpg = initKeys();
PrivateKey priKey = null;
PublicKey pubKey = null;
try {
priKey = (PrivateKey)getKey(PRI_FILE, PRIVATE);
pubKey = (PublicKey)getKey(PUB_FILE, PUBLIC);
} catch (Exception e) { System.err.println("30:" + e); }
Cipher c = null;
try {
c = Cipher.getInstance("RSA");
} catch (Exception e) {}
String message = "Encryption & Decryption of a MESSAGE";
byte[] text = message.getBytes();
text = crypt(c, pubKey, text, Cipher.ENCRYPT_MODE);
System.out.println("Encrypted Text:"+new String(text));
text = crypt(c, priKey, text, Cipher.DECRYPT_MODE);
System.out.println("Decrypting Text:" + new String(text));
}

private KeyPairGenerator initKeys() {
KeyPairGenerator kpg = null;
try {
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024, new SecureRandom());
KeyPair kp = kpg.generateKeyPair();
PrivateKey priKey = kp.getPrivate();
writeKeyToFile(PRI_FILE, priKey.getEncoded());
PublicKey pubKey = kp.getPublic();
writeKeyToFile(PUB_FILE, pubKey.getEncoded());
} catch (Exception e) { }
return kpg;
}
private void writeKeyToFile(String filename, byte[] contents) {
try {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(contents);
fos.flush();
fos.close();
} catch (Exception e) { }
}
private byte[] crypt(Cipher cipher, Key key, byte[] text, int type) {
ByteArrayOutputStream out = null;
try {
cipher.init(type, key, new SecureRandom());
out = new ByteArrayOutputStream();
int s = cipher.getBlockSize();
int r = 0;
for (int t = 0; t < text.length; t += s) {
if (text.length - t <= s) {
r = text.length - t;
} else {
r = s;
}
out.write(cipher.doFinal(text, t, r));
}
out.flush();
out.close();
} catch (Exception e) { }
return out.toByteArray();
}

private Key getKey(String filename, int type) throws Exception {
FileInputStream fis = null;
fis = new FileInputStream(filename);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int b;
try {
while ((b = fis.read()) != -1) {
baos.write(b);
}
} catch (Exception e) {}
byte[] keydata = baos.toByteArray();
Key key = null;
try {
KeyFactory kf = KeyFactory.getInstance("RSA", "SUN");
// KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
switch (type) {
case PRIVATE:
PKCS8EncodedKeySpec encPrivateKey = new PKCS8EncodedKeySpec(keydata);
key = kf.generatePrivate(encPrivateKey);
return key;
case PUBLIC:
X509EncodedKeySpec encPublicKey = new X509EncodedKeySpec(keydata);
key = kf.generatePublic(encPublicKey);
return key;
}
} catch (Exception e) { }
return key;
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic