Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Urgent: rsa encryption/decryption

843810Apr 20 2004 — edited Apr 21 2004
Can someone help me? I don't know what is wrong with my program. and I am working with jdk 1.4.2

Every time when I drive this program, it comes "no such algorithm". It seems that when I am initialize cipher, and the rsa algorithm is not known.

import java.security.spec.*;
import java.security.interfaces.*;
import java.io.*;
import java.text.*;
import java.lang.*;
import javax.crypto.*;
//import java.security.AlgorithmParameters;
import java.security.*;


public class Crypto1 {

public static void main(String[] args) throws IOException {

if ( (args.length < 1 || args.length > 4) || (args[0].equals("-h")) ) {
System.out.println("syntax: java Crypto1 inputFile encryptedFile decryptedFile");
System.out.println();
return;
}


String inputFile = args[0];
String encryptedFile = args[1]; // inneh?ller krypterad text
String decryptedFile = args[2]; // inneh?ller dekrypterad text



runRSA( inputFile, encryptedFile, decryptedFile);

} // end Main

private static void runRSA( String inputFile, String encryptedFile, String decryptedFile) throws IOException {
PublicKey rsapub;
PrivateKey rsapriv;
Cipher cipher=null;
KeyPairGenerator keypairGen;
KeyPair keypair;

BufferedInputStream inFile = new BufferedInputStream(new FileInputStream(inputFile));

// finn reda p?l&#37592;gden av filen
// l&#37597; sedan in den
int size = 0;
int numberOfReadChars = 0;
while ((numberOfReadChars = inFile.read()) != -1) {
size++;
}
//inFile.close();

//inFile = new BufferedInputStream(new FileInputStream(inputFile));
//numberOfReadChars = 0;
byte[] inData = new byte[size];
numberOfReadChars = inFile.read(inData, 0, size);
inFile.close();
System.out.println("SYM ############################################");

//generate keypair
try {
keypairGen = KeyPairGenerator.getInstance("rsa");}catch(NoSuchAlgorithmException ex) {
System.out.println("RSA KeyPair Generator not found.");
return;
}

keypairGen.initialize(1024);//use modulus length=1024
keypair= keypairGen.generateKeyPair();
if (keypair == null) {
System.out.println("Key generation failure. KeyPair was not generated.");
return;
}

//get RSA public /private key
rsapub=keypair.getPublic();
rsapriv=keypair.getPrivate();
System.out.println(rsapub);
System.out.println(rsapriv);
System.out.println("RSA key done ###################################");

//generate RSA Cipher
try{
cipher= Cipher.getInstance("RSA");
}catch (javax.crypto.NoSuchPaddingException e) {
//should not happen
System.out.println("Invalid padding");
} catch (java.security.NoSuchAlgorithmException e) {
//should not happen
System.out.println("no such algorithm");
}
// System.out.print("hello");
byte[] inText = {' '};
byte[] outText = {' '};

inText = inData;

try{
cipher.init(Cipher.ENCRYPT_MODE, rsapub);
outText = cipher.doFinal(inText); // krypterad data
}catch(Exception ex){
System.out.println("error encrypting");
return;
}
byte[] outData = outText;
int outDataSize = outData.length;
BufferedOutputStream outFile = new BufferedOutputStream(new FileOutputStream(encryptedFile));
outFile.write(outData, 0, outDataSize);
outFile.flush();
outFile.close();



// l?s in den krypterade filen och dekryptera
inFile = new BufferedInputStream(new FileInputStream(encryptedFile));

// finn reda p? l?ngden av filen
// l?s sedan in den
size = 0;
numberOfReadChars = 0;
while ((numberOfReadChars = inFile.read()) != -1) {
size++;
}
inFile.close();

inFile = new BufferedInputStream(new FileInputStream(encryptedFile));
numberOfReadChars = 0;
inData = new byte[size];
numberOfReadChars = inFile.read(inData, 0, size);
inFile.close();
inText = inData;

try{
cipher.init(Cipher.DECRYPT_MODE, rsapriv);
outText =cipher.doFinal(inText); // dekrypterad data
}catch(Exception ex){
System.out.println("error dncrypting");
return;
}

outData = outText;
outDataSize = outData.length;
outFile = new BufferedOutputStream(new FileOutputStream(decryptedFile));
outFile.write(outData, 0, outDataSize);
outFile.flush();
outFile.close();


// }catch(Exception e) {
//System.out.println(e);
//System.exit(0);
//}

}
}





Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 19 2004
Added on Apr 20 2004
2 comments
213 views