Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android如何在apk源代码中包含加密公钥,因为设备无法从文件加载它_Android_Encryption - Fatal编程技术网

android如何在apk源代码中包含加密公钥,因为设备无法从文件加载它

android如何在apk源代码中包含加密公钥,因为设备无法从文件加载它,android,encryption,Android,Encryption,在下面的代码中,我正在创建public 以及用于加密的私钥 我真的很想在Android java源代码中嵌入公钥 由于它们是对象的,我不明白如何在代码中包含公钥。 我试图创建一些最终字符串,但没有成功 任何帮助都将不胜感激。 也许这是一个糟糕的ide,因为我没有看到太多关于它的讨论 public void GenerateKeyPair() { try{ KeyPairGenerator kpg = KeyPairGenerator.getInstance(

在下面的代码中,我正在创建public
以及用于加密的私钥

我真的很想在Android java源代码中嵌入公钥

由于它们是对象的,我不明白如何在代码中包含公钥。
我试图创建一些最终字符串,但没有成功

任何帮助都将不胜感激。
也许这是一个糟糕的ide,因为我没有看到太多关于它的讨论

public void GenerateKeyPair()
{       
    try{
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.genKeyPair();

        KeyFactory fact = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pub = fact.getKeySpec(
                kp.getPublic(),RSAPublicKeySpec.class);
        RSAPrivateKeySpec priv = fact.getKeySpec
        (kp.getPrivate(),RSAPrivateKeySpec.class);

        saveToFile("public.key", pub.getModulus(),pub.getPublicExponent());
    saveToFile("private.key", priv.getModulus(),priv.getPrivateExponent());
    }catch(Exception e){
        System.out.println(e.getMessage());
    }
}

public void saveToFile(String fileName,BigInteger mod, 
        BigInteger exp) throws Exception {

    ObjectOutputStream oout = new ObjectOutputStream
    (new BufferedOutputStream(new FileOutputStream(fileName)));
    try {
        oout.writeObject(mod);
        oout.writeObject(exp);
    } catch (Exception e) {
        throw new Exception("error", e);
    } finally {
        oout.close();
    }
}

回答我自己的问题

// create the string for client to use
byte[] b = Base64.encodeBase64(keyPair.getPublic().getEncoded());
String encodedString = new String(b);
System.out.println(encodedString);