Date: Mon, 8 Jun 1998 09:30:11 -0400
From: omar@yacht.ee.fit.edu (Omar Ba_rukab)
Message-Id: <199806081330.JAA18605@yawl.ee.fit.edu>
Dear Sir;
I am trying to practice doing encryption and decryption using both the DES and
the public and private keys too. I used the DES way according to the the Java
Cryptography Extension 1.2 API Specification & Reference (Last Modified : 12 Feb -1998). I did not find any thing helpful in these notes about asymmetric
cryptography and I wish to have a sample code showing me that because I did typed some code as shown below and I go the message shown below too. Your reply to my question is appreciated and thank you.
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import com.sun.crypto.provider.SunJCE;
public class Sample2 {
public static void main (String args[]) {
try {
// Provider sunJce = new com.sun.crypto.provider.SunJCE();
SunJCE jce = new SunJCE();
Security.addProvider(jce);
// Security.addProvider(sunJce);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
keyGen.initialize(512, new SecureRandom());
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
Cipher cipher1 , cipher2;
cipher1 = Cipher.getInstance ("DSA");
cipher2 = Cipher.getInstance ("DSA");
// Initialize the cipher for encryption
cipher1.init(Cipher.ENCRYPT_MODE, pub);
cipher2.init(Cipher.DECRYPT_MODE, priv);
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("b.txt");
CipherInputStream cis1 = new CipherInputStream(fis, cipher1);
byte[] b = new byte[256];
int i = cis1.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis1.read(b);
}
fis.close();
fos.close();
cis1.close();
FileInputStream fis1 = new FileInputStream("b.txt");
FileOutputStream fos1 = new FileOutputStream("c.txt");
CipherInputStream cis2 = new CipherInputStream(fis1, cipher2);
byte[] b1 = new byte[256];
int j = cis2.read(b1);
while (j != -1) {
fos1.write(b1, 0, j);
j = cis2.read(b1);
}
fis1.close();
fos1.close();
cis2.close();
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
Error: java.security.NoSuchAlgorithmException: algorithm DSA not available.