简体   繁体   中英

Unable to save a file in android

Good day.

I need to save PKCS10 CSR in the external storage card.

However, the following code shows an error

java.io.FileNotFoundException: /storage/sdcard0pkcs10.req: open failed: EROFS (Read-only file system)

While AndoidManifest.xml correctly includes

I feel that the problem is directory path since it shows 0 rather than /

package exam.blowfishcipher;

import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.security.*;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import java.security.SecureRandom;

import javax.security.auth.x500.X500Principal;

import org.spongycastle.jce.PKCS10CertificationRequest;
import org.spongycastle.openssl.PEMWriter;

import android.os.Environment;
import android.util.*;
import android.widget.*;

public class PKCS10Generater
{
    static {
        Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
    }

    public static PKCS10CertificationRequest generateRequest(
            KeyPair pair)
            throws Exception

            {           
             return new PKCS10CertificationRequest(
                     "SHA256withRSA",
                     new X500Principal("CN=Test CA Certificate"),
                     //new X500Principal("CN=end"),
                     pair.getPublic(),
                     null,
                     pair.getPrivate());
            }

    public static void pemEncodeToFile(String filename, Object obj, char[] password) throws Exception{
        PEMWriter pw = new PEMWriter(new FileWriter(filename));
           Log.e("Position", "PEMWriter");
           if (password != null && password.length > 0) {
               pw.writeObject(obj, "DESEDE", password, new SecureRandom());
           } else {
               pw.writeObject(obj);
           }
           pw.flush();
           pw.close();
    }

    public static void reqGen() throws Exception
    {
        //create the keys
        Log.e("Position", "reqGen");
        KeyPair pair = Utils.generateRSAKeyPair();

        //modified 20130203
        PKCS10req pkcs10req = new PKCS10req();
        PKCS10CertificationRequest request = pkcs10req.generateRequest(pair);

        pemEncodeToFile(Environment.getExternalStorageDirectory()+"pkcs10.req", request, null);
        Log.e("Position", "getExternalStorage");
        PEMWriter pemWrt = new PEMWriter( new OutputStreamWriter(System.out));
        pemWrt.writeObject(request);
        pemWrt.close();

    }
}

Just modify the line

pemEncodeToFile(Environment.getExternalStorageDirectory()+"pkcs10.req", request, null);

to

pemEncodeToFile(Environment.getExternalStorageDirectory()+"/pkcs10.req", request, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM