BigInteger->字节[]-> Biginteger.看起来相等,但如果语句失败 [英] BigInteger -> byte[] -> Biginteger. Looks equal but if statement fails

查看:79
本文介绍了BigInteger->字节[]-> Biginteger.看起来相等,但如果语句失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑为自己存储一个公钥的想法.为此,我需要将BigInteger转换为某种变量,然后根据该值重新创建BigInteger.

I am playing around with an idea I have for storing a public key for myself. For this I would need to transform the BigInteger in some sort of a variable and then recreate the BigInteger from that value.

我已经搜索了Stackoverflow,以找到最好的方法是使用byte [].

I have searched through Stackoverflow to find the best way to do this is with byte[].

这是我在Eclipse中的代码

This is my code in Eclipse:

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPublicKey;

public class Vaja2 {
    public static void main(String[] args){
        try {

            // Create RSA Keypair (to obtain a BigInteger)
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
            kpg.initialize(1024);
            KeyPair keypair = kpg.generateKeyPair();

            // Extract the public key
            RSAPublicKey publicKey = (RSAPublicKey) keypair.getPublic();

            // Get the public Exponent from the public key, I have BigInteger now.
            BigInteger pkPublicExBI = publicKey.getPublicExponent();

            //Try this:   BigInteger -> byte-> BigInteger
            byte[] pkModByte = pkPublicExBI.toByteArray();
            BigInteger pkPublicExBIrecreated = new BigInteger(pkModByte);


            // Show Results
            System.out.println("Original BigInteger:    " + pkPublicExBI);
            System.out.println("Recreated BigInteger:   " + pkPublicExBIrecreated);

            if (pkPublicExBI == pkPublicExBIrecreated) {
                System.out.println("\nThey are equal");
            }
            else {
                System.out.println("\nThey are NOT equal");
            }


        } catch (Exception e) {
            // Nothing happens
        }

    }
}

这是Eclipse控制台中显示的结果.

And this is the result in shown in the Eclipse console.

Original BigInteger:    65537
Recreated BigInteger:   65537

They are NOT equal

if语句告诉我,两个BigInteger不相等.但是在控制台中,我看到它们都等于65537.

The if statement tells me, that the two BigIntegers are not equal. Yet in the console, I see them as both being equal to 65537.

我的问题:

  1. "if"语句为什么失败?

  1. Why does the "if" statement fail?

您将如何建议我使代码有所不同.假定该程序要求将公钥存储在notepad.exe或类似的文本编码程序(将使用ASCII)中.

How would you recommend me to make the code different. Assuming the program will demand public key to be stored in notepad.exe or similar text-encoding program (ASCII will be used).

推荐答案

比较对象时,请使用.equals()而不是==. ==将比较对象的引用,而.equals()将检查它们是否具有相同的值.由于两个对象很少具有相同的引用,除了在没有关系的地方比较原始类型(int,char,但是String是不是!)之外,永远不要使用==.

Use .equals() as opposed to == when comparing objects. == will compare the objects' references, whereas .equals() will check to see if they have the same values. Since two objects will very rarely have the same reference you should never use == except for comparing primitive types (int, char, but String is not a primitive type!) where it doesn't matter.

所以你想要

if (pkPublicExBI.equals(pkPublicExBIrecreated)) {

代替

if (pkPublicExBI == pkPublicExBIrecreated) {

这篇关于BigInteger->字节[]-> Biginteger.看起来相等,但如果语句失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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