2012-03-24 151 views
3

我正在尝试为RSA加密生成我的第一个公钥/私钥对。这是我第一次这样做,但通过查看各种教程和网站,我决定使用下面的代码。虽然我的代码不会给我错误,但它强制关闭。一切都张贴包括我的进口,可以sombody请帮我理解为什么我的代码不生成键和给我的错误?是的,我没有宣布它在AndroidManifest.xml文件在生成公钥/私钥之前关闭RSA加密force

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> 
+0

@Obliviator只有我检查你的问题是,你说“是的,我确实在声明中声明”,这意味着你有没有声明这个类在清单中不扩展为Activity然后,它肯定会给你错误你可以在Last.So上发布你的清单文件代码,并且可能是你的Activity类的地方。 – Herry 2012-03-24 06:20:19

+0

我张贴清单 – 2012-03-24 15:34:43

回答

0

@The Obliviator当我看到你的AndroidManifest时,我发现你应该从你的清单中删除下面的代码。

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

因为此类不作为扩展活动,你需要这个类GenerateKeyPair所以没有必要宣布这一类清单file.And什么关于这个类的安装,是这个类也不像以前那样活动再延长删除它也是。然后你会成功运行。