Whenever i go to decrypt a chunk of data, using RSA, i get the following exception thrown:
java.security.ProviderException: java.security.KeyException: The operation completed successfully.
this is a weird exception that i cannot find any information on, it happens at this line:
byte[] clearData = decipher.doFinal(chunk.getBytes());
here is the entire method:
public void signFile(File file, String infile)
{
try
{
Provider a[] = Security.getProviders();
boolean hasCAPI = false;
updateText("INSTALLED PROVIDERS:");
for(int i = 0; i < a.length; i++) //display installed providers
{
System.out.println(a.toString());
updateText(a[i].toString());
if(a[i].contains("SunMSCAPI"))
hasCAPI = true;
}
if(!hasCAPI)
updateText("MSCAPI provider not found, breaking execution.");
else
{
updateText("MSCAPI provider found");
//String alias = "Peter Barkley's Encryption Certificate"; //"Peter Barkley's EFS Certificate";
updateText("Trying to open Windows-MY keystore using SunMSCAPI provider...");
KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
ks.load(null, null); //File, Char[] password
String alias = "";
Enumeration aliases = ks.aliases();
updateText("CERTS FOUND:");
while(aliases.hasMoreElements())
{
String aliasKey = (String)aliases.nextElement();
updateText(aliasKey);
if(aliasKey.contains("Encryption Certificate"))
{
updateText("Using Certificate: " +aliasKey);
alias = aliasKey;
}
}
Certificate c = ks.getCertificate(alias);
System.out.println("Alias: " +ks.getCertificateAlias(c));
System.out.println("Cert Type: " +c.getType());
updateText("Alias: " +ks.getCertificateAlias(c));
updateText("Cert Type: " +c.getType());
//System.out.println(ks.size()); //Number of certs in key store
PublicKey pubKey = (PublicKey) c.getPublicKey();
PrivateKey privKey = (PrivateKey)ks.getKey(alias, null);
KeyPair kp = new KeyPair(pubKey, privKey);
/*try{
c.verify(pubKey);
}
catch(Exception e){
updateText("Verify Public Key Error: " +e.getMessage());
e.printStackTrace();
}*/
if(privKey == null) //user did not sign into Entrust
{
updateText("Private key could not be gathered, probably due to bad entrust sign in");
System.out.println("Private key could not be gathered");
}
System.out.println("Private Key: " privKey "\n\nPublic Key: " +pubKey);
updateText("Private Key: " privKey "\n\nPublic Key: " +pubKey);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
updateText("Encrypting: " +infile);
int len = infile.length();
int chunks = len/100;
int leftover = len - (chunks*100);
String leftoverString = infile.substring(len - leftover, len);
String finalCipherText = "";
//break into 100byte chunks and encrypt
for(int i = 0; i < chunks; i++)
{
String chunk = infile.substring(i*100, (i+1)*100);
byte[] cipherData = cipher.doFinal(chunk.getBytes());
int cipherlength = cipherData.length;
String ciphertext = new String(cipherData, 0, cipherlength);
//updateText("Chunk: " chunk " ciphertext: " +ciphertext);
finalCipherText += ciphertext;
}
byte[] cipherData = cipher.doFinal(leftoverString.getBytes());
int cipherlength = cipherData.length;
String ciphertext = new String(cipherData, 0, cipherlength);
finalCipherText += ciphertext;
updateText("Encrypted text: " +finalCipherText);
//decrypt
Cipher decipher = Cipher.getInstance("RSA");
decipher.init(Cipher.DECRYPT_MODE, privKey);
len = finalCipherText.length();
chunks = len/100;
leftover = len - (chunks*100);
leftoverString = finalCipherText.substring(len - leftover, len);
String finalText = "";
for(int i = 0; i < chunks; i++)
{
String chunk = finalCipherText.substring(i*100, (i+1)*100);
byte[] clearData = decipher.doFinal(chunk.getBytes());
int clearlength = clearData.length;
String cleartext = new String(clearData, 0, clearlength);
finalText += cleartext;
}
byte[] clearData = decipher.doFinal(leftoverString.getBytes());
int clearlength = clearData.length;
String cleartext = new String(clearData, 0, clearlength);
finalText += cleartext;
updateText("Decrypted text: " +finalText);
}
}catch(Exception e){
updateText(""+e);
System.out.println(e.getMessage());
e.printStackTrace();
}
}Any ideas on the decryption error?