[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bouncycastle-crypto-dev
Subject:    [dev-crypto] J2ME reading private key - private key created with
From:       wayneforrest <wayneforrest () gmail ! com>
Date:       2008-10-23 20:20:37
Message-ID: 20126975.post () talk ! nabble ! com
[Download RAW message or body]


Hello, need some here please,

When I run my code trying to read the private key, I get this error:
java.io.IOException: DER length more than 4 bytes

I create the public and private keys using OPENSSL and use the KEYS on J2ME
My encryption seems to work: not sure yet since I have not managed to
decrypt yet.
When I try to read the private key, it fails.

Please have a look at my decrypt(String encypted)  function and help me
correct it.




PHP CODE

openssl_x509_export($sscert, $publickey);
$publickey = $this->pem2der($publickey);

openssl_pkey_export($privkey, $privatekey, $this->passphrase);
$privatekey = $this->pem2der($privatekey);
$this->toDisk($privatekey,$publickey);


function toDisk($priv,$pub) {
  file_put_contents("$dir/gen_privkey.der",$priv);
  file_put_contents("$dir/gen_cert.der",$pub);
}

function pem2der($pem_data) {
		$begin = "CERTIFICATE-----";
		$end   = "-----END";
		$pem_data = substr($pem_data, strpos($pem_data, $begin)+strlen($begin));
		$pem_data = substr($pem_data, 0, strpos($pem_data, $end));
		$der = base64_decode($pem_data);
		return $der;
}


Here is my J2ME CODE

// Encrypt with public KEY
public String encypt(String token) {
        String encypted = "";
        try {

            ASN1InputStream aIn = new
ASN1InputStream(getClass().getResourceAsStream("gen_cert.pem"));
            ASN1Sequence encodedSeq = (ASN1Sequence) aIn.readObject();
            
            //System.out.println("bb:"+encodedSeq); 
            X509CertificateStructure x509 =
                    X509CertificateStructure.getInstance(encodedSeq);
            SubjectPublicKeyInfo pkInfo = x509.getSubjectPublicKeyInfo();
            RSAPublicKeyStructure pk =
                   
RSAPublicKeyStructure.getInstance(pkInfo.getPublicKey());
            RSAKeyParameters pubParameters = new RSAKeyParameters(false,
                    pk.getModulus(), pk.getPublicExponent());
            AsymmetricBlockCipher rsaEngine = new RSAEngine();
            rsaEngine.init(true, pubParameters); // if true = encryption

            byte[] toEncrypt = token.getBytes();
            byte[] bcrsacipher = rsaEngine.processBlock(toEncrypt, 0,
toEncrypt.length);
            encypted = new String(bcrsacipher);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return encypted;
}

// Decrypt with Private Key <--------
    private String decrypt(String encypted) {
        String decrypted = "";
        try {
            ASN1InputStream aIn = new
ASN1InputStream(getClass().getResourceAsStream("gen_privkey.pem"));
            ASN1Sequence encodedSeq = (ASN1Sequence) aIn.readObject();
<---FAILS HERE
            
            System.out.println("dec : "+encodedSeq); 
            
            PrivateKeyInfo prikInfo = new PrivateKeyInfo(encodedSeq);
            //DERObject derobj= prikInfo.getPrivateKey();
            System.out.println(prikInfo);
            RSAPrivateKeyStructure privStruct = new
RSAPrivateKeyStructure((DERSequence) prikInfo.getPrivateKey());
            
            // I extract the relevant parameters about the private key
            BigInteger coeficiente = privStruct.getCoefficient();
            BigInteger modulo = privStruct.getModulus();
            BigInteger exponente1 = privStruct.getExponent1();
            BigInteger exponente2 = privStruct.getExponent2();
            BigInteger primo1 = privStruct.getPrime1();
            BigInteger primo2 = privStruct.getPrime2();
            BigInteger privateExponent = privStruct.getPrivateExponent();
            BigInteger publicExponent = privStruct.getPublicExponent();

            RSAPrivateCrtKeyParameters keySpec = new
RSAPrivateCrtKeyParameters(
                    modulo, publicExponent, privateExponent, primo1, primo2,
                    exponente1, exponente2, coeficiente);

            AsymmetricBlockCipher rsaEngine = new RSAEngine();
            rsaEngine.init(false, keySpec); // if true = encryption

            byte[] toDEcrypt = encypted.getBytes();
            byte[] bcrsacipher = rsaEngine.processBlock(toDEcrypt, 0,
toDEcrypt.length);
            decrypted = new String(bcrsacipher);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return decrypted;
    }
-- 
View this message in context: \
http://www.nabble.com/J2ME-reading-private-key---private-key-created-with-openssl-tp20126975p20126975.html
 Sent from the Bouncy Castle - Dev mailing list archive at Nabble.com.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic