Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sample Code fpr PBE

0 views
Skip to first unread message

Martin Welser

unread,
Jun 25, 2004, 7:29:25 PM6/25/04
to
Hello,
can anybody post a sample code for pbe? I don't know what's wrong with my
code. I get always the message, that I have no trusted CA.
I have written two classes and no of them works. Can anybody help me?
java.lang.ExceptionInInitializerError
at javax.crypto.SecretKeyFactory.getInstance(DashoA6275)
at org.concubinet.core.security.CCNPBCrypto.<init>(CCNPBCrypto.java:41)
at
org.concubinet.core.security.CCNTrustCenter.test(CCNTrustCenter.java:139)
at
org.concubinet.core.security.CCNTrustCenter.main(CCNTrustCenter.java:306)
Caused by: java.lang.SecurityException: Cannot set up certs for trusted CAs
at javax.crypto.SunJCE_b.<clinit>(DashoA6275)
... 4 more
Caused by: java.security.PrivilegedActionException:
java.security.InvalidKeyException: MD5/RSA/PKCS#1: Not an RSA public key
at java.security.AccessController.doPrivileged(Native Method)
... 5 more
Caused by: java.security.InvalidKeyException: MD5/RSA/PKCS#1: Not an RSA
public key
at
cryptix.provider.rsa.Any_RSA_PKCS1Signature.engineInitVerify(Any_RSA_PKCS1Si
gnature.java:254)
at java.security.Signature.initVerify(Signature.java:297)
at sun.security.x509.X509CertImpl.verify(X509CertImpl.java:429)
at sun.security.x509.X509CertImpl.verify(X509CertImpl.java:383)
at javax.crypto.SunJCE_b.c(DashoA6275)
at javax.crypto.SunJCE_b.b(DashoA6275)
at javax.crypto.SunJCE_s.run(DashoA6275)
... 6 more
Exception in thread "main"
Here is one of my classes:

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;
import org.bouncycastle.*;
import org.bouncycastle.jce.provider.*;
import sun.misc.*;
public class PBE
{
public PBE()
{
Security.addProvider(new BouncyCastleProvider());
}
private static int ITERATIONS=1000;
private static final int saltlength=8;
public String getString(byte[] bytearray)
{
return(new String(bytearray));
}
public byte[] getByte(String stringtext)
{
return(stringtext.getBytes());
}


public static byte[] encrypt(char[]password,byte[] a) throws Exception
{
Security.addProvider(new BouncyCastleProvider());
String plaintext= new String(a);
//Beginbycreatingarandomsaltof64bits(8bytes)
byte[]salt=new byte[8];
Random random=new Random();
random.nextBytes(salt);
//CreatethePBEKeySpecwiththegivenpassword
PBEKeySpec keySpec=new PBEKeySpec(password);
//GetaSecretKeyFactoryforPBEWithMD5AndDES
SecretKeyFactory
keyFactory=SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC",new
BouncyCastleProvider());
//Createourkey
SecretKey key=keyFactory.generateSecret(keySpec);
//Nowcreateaparameterspecforoursaltanditerations
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,ITERATIONS);
//Createacipherandinitializeitforencrypting
System.out.println("test");
Cipher cipher=Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher.init(Cipher.ENCRYPT_MODE,key,paramSpec);
byte[] ciphertext=cipher.doFinal(plaintext.getBytes());
BASE64Encoder encoder=new BASE64Encoder();
String saltString=encoder.encode(salt);
String ciphertextString=encoder.encode(ciphertext);
String temp=saltString+ciphertextString;
return(temp.getBytes());
}
public static byte[] decrypt(char[]password,byte[] a)throws Exception
{ String text= new String(a);
Security.addProvider(new BouncyCastleProvider());
//BeginbysplittingthetextintosaltandtextStrings
//saltisfirst12chars,BASE64encodedfrom8bytes.
String salt=text.substring(0,12);
String ciphertext=text.substring(12,text.length());
//BASE64Decodethebytesforthesaltandtheciphertext
BASE64Decoder decoder=new BASE64Decoder();
byte[]saltArray=decoder.decodeBuffer(salt);
byte[]ciphertextArray=decoder.decodeBuffer(ciphertext);
//CreatethePBEKeySpecwiththegivenpassword
PBEKeySpec keySpec=new PBEKeySpec(password);
//GetaSecretKeyFactoryforPBEWithMD5AndDES
SecretKeyFactory
keyFactory=SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
//Createourkey
SecretKey key=keyFactory.generateSecret(keySpec);
//Nowcreateaparameterspecforoursaltanditerations
PBEParameterSpec paramSpec=new PBEParameterSpec(saltArray,ITERATIONS);
//Createacipherandinitializeitforencrypting
Cipher cipher=Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher.init(Cipher.DECRYPT_MODE,key,paramSpec);
//Performtheactualdecryption
byte[]plaintextArray=cipher.doFinal(ciphertextArray);
return plaintextArray;
}


}


0 new messages