Securing RSA encryption before generating public / private keys

I am trying to create my first public / private key pair for RSA encryption. This is my first time, but after looking at various tutorials and websites, I decided to do it with the following code. Although my code does not give me errors, it makes me close. Everything is published, including my import, can someone please help me understand why my code does not generate keys and does not give me errors? And yes, I declared 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> 
+3
source share
2 answers

I don't know what causes your problem (we would have to see where you use this class to debug it), but I have an alternative for you if you can include a third-party library. See JSch , which can generate RSA key pairs (for example, for use in SSH public key authentication). Documentation: http://epaul.github.com/jsch-documentation/simple.javadoc/

The method you are looking for is KeyPair.genKeyPair .

0
source

@Obliviator When I see you AndroidManifest, I find that you will need to remove the code below from your manifest.

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

Since this class does not extend As Activity, you need this class for GenerateKeyPair, so there is no need to declare this class in the manifest file. As for this setting of the class, this class also does not propagate as an Activity, and then deletes it. After that, you will get a successful execution.

0
source

Source: https://habr.com/ru/post/1403383/


All Articles