制作code从java程序可运行

制作code从java程序可运行

本文介绍了RSA密钥对使用充气城堡生成。制作code从java程序可运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java code,我发现,生成一个公共和通过充气城堡库中的私钥。我的问题是实现它为code。通过我的Andr​​oid设备运行的。像我是编程,并通过我的大部分故障排除我的code不显示RSA密钥我仍然无法让我的code照我问,虽然我没有得到任何错误。我怀疑是我把我所有的code进入方式尝试 / 块,但我不能真的确定。
编辑:在低code

这是生成的RSA公钥和私钥的Java类。 (它的工作原理)

 公共类ClassMain {
    公共静态无效的主要(字串[] args)抛出异常{
        串ST =Ebenezersawesome;
        字节[] =明文ST.getBytes(UTF8);
        //生成RSA密钥
        的System.out.println(\\ n开始生成RSA密钥)​​;
        KeyPairGenerator的注册机= KeyPairGenerator.getInstance(RSA);
        keyGen.initialize(4096);
        密钥对键= keyGen.generateKeyPair();
        的System.out.println(说完产生RSA密钥)​​;
        //
        //创建一个RSA Cipher对象(指定算法,模式,
        //填充)。
        密码加密= Cipher.getInstance(RSA / ECB / PKCS1Padding);
        //
        //打印提供商信息
        的System.out.println(\\ n+ cipher.getProvider()的getInfo());
        的System.out.println(\\ n开始加密);
        //
        //初始化Cipher对象。
        cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());
        //
        //使用公共密钥加密明文
        字节[] =密文cipher.doFinal(明文);
        的System.out.println(完成加密:);
        的System.out.println(新的String(密文UTF8));
        的System.out.println(\\ n开始解密);
        //
        //初始化Cipher对象。
        cipher.init(Cipher.DECRYPT_MODE,key.getPrivate());
        //
        //使用私钥解密密文
        字节[] = newPlainText cipher.doFinal(密文);
        的System.out.println(完成解密:);
        的System.out.println(新的String(newPlainTextUTF8));
    }
}

这是我尝试在Android应用程序,以显示code尝试。
编辑:在code ++工程,但由于某种原因,我的try / catch停止,并且不产生TV3 code

  TextView的TV1;
    TextView中TV2;
    TextView的TV3;
    按钮转换;
    字符串publicKeyFilename = NULL;
    字符串privateKeyFilename = NULL;
    串ST =Ebenezersawesome;    @覆盖公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        Log.d(EBZ,制造它的onCreate);
        TV1 =(的TextView)findViewById(R.id.tv1);
        TV2 =(的TextView)findViewById(R.id.tv2);
        TV3 =(的TextView)findViewById(R.id.tv3);
        转换=(按钮)findViewById(R.id.button1);        // tv2.setText(ST);
        convert.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                Log.d(EBZ,制造它的onCreate);
                尝试{
                    字节[] =明文ST.getBytes(UTF8);
                    Log.d(EBZ,去到try块);
                    KeyPairGenerator的注册机=的KeyPairGenerator
                            .getInstance(RSA);
                    keyGen.initialize(2048);
                    密钥对键= keyGen.generateKeyPair();
                    密码加密= Cipher.getInstance(RSA / ECB / PKCS1Padding);
                    tv1.setText(。cipher.getProvider()的getInfo()的toString());
                    Log.d(EBZ,制造它通过TV1);
                    //tv1.setText(ST);
                    cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());
                    字节[] =密文cipher.doFinal(明文);
                    tv2.setText(。新的String(密文UTF8)的toString());
                    Log.d(EBZ,制造它通过TV2);
                    // tv2.setText(ST);
                    字节[] = newPlainText cipher.doFinal(密文);
                     //tv3.setText(new字符串(newPlainTextUTF8)的toString());
                    // tv3.setText(ST);
                    Log.d(EBZ,制造它通过TV3);
                }赶上(例外五){
                    的System.out.println(错误);
                }
            }
        });
    }


解决方案

好吧尝试以下code,而不是你,看到你的的try / catch 块里面发生了什么。

  TextView的TV1;
TextView中TV2;
TextView的TV3;
按钮转换;
字符串publicKeyFilename = NULL;
字符串privateKeyFilename = NULL;
串ST =Ebenezersawesome;@覆盖公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    Log.d(EBZ,制造它的onCreate);
    TV1 =(的TextView)findViewById(R.id.tv1);
    TV2 =(的TextView)findViewById(R.id.tv2);
    TV3 =(的TextView)findViewById(R.id.tv3);
    转换=(按钮)findViewById(R.id.button1);    尝试{
                字节[] =明文ST.getBytes(UTF8);
                Log.d(EBZ,去到try块);
                注册机的KeyPairGenerator =
                    KeyPairGenerator.getInstance(RSA);
                keyGen.initialize(4096);
                密钥对键= keyGen.generateKeyPair();
                密码加密=
                    Cipher.getInstance(RSA / ECB / PKCS1Padding);
                tv3.setText(。cipher.getProvider()的getInfo()的toString());
                //tv3.setText(ST);
                cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());
                字节[] =密文cipher.doFinal(明文);
                tv1.setText(。新的String(密文UTF8)的toString());
                //tv1.setText(ST);
                字节[] = newPlainText cipher.doFinal(密文);
                tv2.setText(。新的String(newPlainTextUTF8)的toString());
                //tv2.setText(ST);
            }赶上(例外五){
                的System.out.println(错误);
            }
    // tv2.setText(ST);
    convert.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            Log.d(EBZ,制造它的onCreate);
        }
    });
}

I am using a Java code that I found that generates a public and a private key via the bouncy castle library. My problem is implementing it into code runnable by my android device. My code does not display the RSA keys like I programmed it to and through most of my troubleshooting I am still unable to make my code do as I ask, although I get no errors. My suspicion is the way I put all of my code into a try/catch block but I am not really sure.Edit: Lower in code

This is the Java class that generates the RSA public and private key. (It works)

public class ClassMain {
    public static void main(String[]args) throws Exception {
        String ST = "Ebenezersawesome";
        byte[] plainText = "ST".getBytes("UTF8");
        // Generating RSA Key
        System.out.println("\nStart generating RSA key");
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(4096);
        KeyPair key = keyGen.generateKeyPair();
        System.out.println("Finish generating RSA key");
        //
        // Creates an RSA Cipher object (specifying the algorithm, mode, and
        // padding).
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        //
        // Print the provider information
        System.out.println("\n" + cipher.getProvider().getInfo());
        System.out.println("\nStart encryption");
        //
        // Initializes the Cipher object.
        cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
        //
        // Encrypt the plaintext using the public key
        byte[] cipherText = cipher.doFinal(plainText);
        System.out.println("Finish encryption: ");
        System.out.println(new String(cipherText, "UTF8"));
        System.out.println("\nStart decryption");
        //
        // Initializes the Cipher object.
        cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
        //
        // Decrypt the ciphertext using the private key
        byte[] newPlainText = cipher.doFinal(cipherText);
        System.out.println("Finish decryption: ");
        System.out.println(new String(newPlainText, "UTF8"));
    }
}

This is my attempt at trying to display the code in an android application.Edit: The code works but for some reason my try/catch stops and does not generate code in tv3.

  TextView tv1;
    TextView tv2;
    TextView tv3;
    Button convert;
    String publicKeyFilename = null;
    String privateKeyFilename = null;
    String ST = "Ebenezersawesome";

    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("Ebz", "Made it to onCreate");
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        convert = (Button) findViewById(R.id.button1);

        // tv2.setText(ST);
        convert.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.d("Ebz", "Made it to onCreate");
                try {
                    byte[] plainText = "ST".getBytes("UTF8");
                    Log.d("Ebz", "made it to Try Block");
                    KeyPairGenerator keyGen = KeyPairGenerator
                            .getInstance("RSA");
                    keyGen.initialize(2048);
                    KeyPair key = keyGen.generateKeyPair();
                    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
                    tv1.setText(cipher.getProvider().getInfo().toString());
                    Log.d("Ebz", "Made it passed tv1");
                    //tv1.setText(ST);
                    cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
                    byte[] cipherText = cipher.doFinal(plainText);
                    tv2.setText(new String(cipherText, "UTF8").toString());
                    Log.d("Ebz", "Made it passed tv2");
                    // tv2.setText(ST);
                    byte[] newPlainText = cipher.doFinal(cipherText);
                     //tv3.setText(new String(newPlainText, "UTF8").toString());
                    // tv3.setText(ST);
                    Log.d("Ebz", "Made it passed tv3");
                } catch (Exception e) {
                    System.out.println("error");
                }
            }
        });
    }
解决方案

Ok try following code instead yours and see what happens inside your try/catch block.

TextView tv1;
TextView tv2;
TextView tv3;
Button convert;
String publicKeyFilename = null;
String privateKeyFilename = null;
String ST = "Ebenezersawesome";

@Override public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d("Ebz", "Made it to onCreate");
    tv1 = (TextView) findViewById(R.id.tv1);
    tv2 = (TextView) findViewById(R.id.tv2);
    tv3 = (TextView) findViewById(R.id.tv3);
    convert = (Button) findViewById(R.id.button1);

    try {
                byte[]plainText = "ST".getBytes("UTF8");
                Log.d("Ebz", "made it to Try Block");
                KeyPairGenerator keyGen =
                    KeyPairGenerator.getInstance("RSA");
                keyGen.initialize(4096);
                KeyPair key = keyGen.generateKeyPair();
                Cipher cipher =
                    Cipher.getInstance("RSA/ECB/PKCS1Padding");
                tv3.setText(cipher.getProvider().getInfo().toString());
                //tv3.setText(ST);
                cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
                byte[]cipherText = cipher.doFinal(plainText);
                tv1.setText(new String(cipherText, "UTF8").toString());
                //tv1.setText(ST);
                byte[]newPlainText = cipher.doFinal(cipherText);
                tv2.setText(new String(newPlainText, "UTF8").toString());
                //tv2.setText(ST);
            } catch(Exception e) {
                System.out.println("error");
            }
    // tv2.setText(ST);
    convert.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Log.d("Ebz", "Made it to onCreate");
        }
    });
}

这篇关于RSA密钥对使用充气城堡生成。制作code从java程序可运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 21:22