RSA Encryption forceclosing before generating public/private keys

陌路散爱 提交于 2019-12-22 10:59:32

问题


I am trying to generate my first public/private key pair for an RSA encryption. This is my first time doing so but through looking at various tutorials and website i've decided to do so with the following code. Although my code does not give me errors, it force closes. Everything is posted including my imports, can sombody please help me understand why my code is not generating keys and giving me errors? And yes i did declare it in the AndroidManifest.xml file

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

    public class RSA {
        public static void GenerateKeyPair() {
            try {
                KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
                kpg.initialize(4096);
                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 static 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();
            }
        }
    }


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.BLAH"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".UUIDActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Installation"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".RSA"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

回答1:


I don't know what's causing your problem (we'd have to see where you use this class to debug that), but I do have an alternative for you, if you can include a 3rd party library. See JSch, which can generate RSA keypairs (e.g. for use in SSH public-key authentication). Documentation: http://epaul.github.com/jsch-documentation/simple.javadoc/

The method you're looking for is KeyPair.genKeyPair.




回答2:


@The Obliviator When i see you AndroidManifest ,i found that you should have to remove below code from your manifest.

  <activity
        android:name=".RSA"
        android:label="@string/app_name" >
    </activity>

Because this class is not extends As Activity ,you required this class for GenerateKeyPair so no need to declare this class in Manifest file.And what about this class Installation ,is this class is also not extends as Activity then remove it also.After that you will get run successfully.



来源:https://stackoverflow.com/questions/9849207/rsa-encryption-forceclosing-before-generating-public-private-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!