生成公钥/私钥之前RSA加密forceclosing [英] RSA Encryption forceclosing before generating public/private keys

查看:175
本文介绍了生成公钥/私钥之前RSA加密forceclosing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个RSA加密我第一个公开/私有密钥对。这是我第一次这样做,而是通过寻找不同的教程和网站,我已经决定用下面的code这样做。虽然我的code不给我的错误,它强制关闭。一切都张贴包括我的进口,可以sombody请帮​​助我理解了为什么我的code未生成密钥,并给我的错误?是的,我并声明它在AndroidManifest.xml文件

 进口java.io.BufferedOutputStream;
进口java.io.FileOutputStream中;
进口java.io.ObjectOutputStream中;
进口java.math.BigInteger的;
进口java.security.KeyFactory;
进口java.security.KeyPair中;
进口java.security.KeyPairGenerator;
进口java.security.spec.RSAPrivateKeySpec;
进口java.security.spec.RSAPublicKeySpec;    公共类RSA {
        公共静态无效GenerateKeyPair(){
            尝试{
                KPG的KeyPairGenerator = KeyPairGenerator.getInstance(RSA);
                kpg.initialize(4096);
                密钥对KP = kpg.genKeyPair();                事实上的KeyFactory = KeyFactory.getInstance(RSA);
                RSAPublicKeySpec酒馆= fact.getKeySpec(kp.getPublic(),
                        RSAPublicKeySpec.class);
                RSAPrivateKeySpec私法= fact.getKeySpec(kp.getPrivate()
                        RSAPrivateKeySpec.class);                的SaveToFile(public.key,pub.getModulus(),pub.getPublicExponent());
                的SaveToFile(private.key,priv.getModulus(),
                        priv.getPrivateExponent());
            }赶上(例外五){
                的System.out.println(e.getMessage());
            }
        }        公共静态无效的SaveToFile(字符串文件名,BigInteger的MOD,
                BigInteger的EXP)抛出异常{
            ObjectOutputStream的oout =新的ObjectOutputStream(
                    新的BufferedOutputStream(新的FileOutputStream(文件名)));
            尝试{
                oout.writeObject(MOD);
                oout.writeObject(EXP);
            }赶上(例外五){
                抛出新的异常(错误,E);
            } {最后
                oout.close();
            }
        }
    }
< XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.BLAH
    安卓版code =1
    机器人:=的versionName1.0>    <采用-SDK安卓的minSdkVersion =7/>    <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:名字=。UUIDActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:安装NAME =
            机器人:标签=@字符串/ APP_NAME>
        < /活性GT;
        <活动
            机器人:RSANAME =
            机器人:标签=@字符串/ APP_NAME>
        < /活性GT;
    < /用途>< /清单>


解决方案

我不知道是什么导致你的问题(我们不得不看到你使用这个类来调试),但我有一个替代你,如果你可以包括第三方库。请参见 JSch ,它可以生成密钥对RSA(例如在SSH公钥认证使用)。文档: http://epaul.github.com/jsch-documentation/simple.javadoc/

您正在寻找的方法是密钥对。 genKeyPair <​​/A>。

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>

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.

这篇关于生成公钥/私钥之前RSA加密forceclosing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆