How can I convert Matlab Integer into java.math.BigInteger

5 ビュー (過去 30 日間)
kkc
kkc 2014 年 5 月 20 日
コメント済み: Friedrich 2014 年 5 月 21 日
i want to use RSA algorithm to demonstrate the simple key generation in matlab (step by step); and i use GUI to pass the data.
the problem is when i using java.math.BigInteger;
i found that java.math.BigInteger needs char to calculate the modPow, modInvers, etc.
so when my input is an integer, i convert to string use "num2str", it works.
mycode:
a=23423423423454
b=num2str(a)
c=java.math.BigInteger(b)
but when i use larger number, it goes to error.
mycode:
a=999999999999999999
b=num2str(a)
c=java.math.BigInteger(b)
so i have to write manuual
mycode:
a='999999999999999999'
b=java.math.BigInteger(a)
is there a function to do it automatic without errorr??? i am newbie to Matlab
  2 件のコメント
Friedrich
Friedrich 2014 年 5 月 20 日
Why do you want to use BigInteger in the first place? Why don't you do the calculation completly in MATLAB?
kkc
kkc 2014 年 5 月 20 日
rsa function to enrypt is:
(a^b)mod c
when a^b is larger than 2^31 (i think 31, maybe im wrong) then matlab give Inf answer (infinit i think).
So use BigInteger because it can calculate that.
And i execute all the code in matlab, because matlab can run some java code.

サインインしてコメントする。

採用された回答

Friedrich
Friedrich 2014 年 5 月 20 日
When using that big numbers use uint64 instead of doubles. This will also help in calling java.math.BigInteger, e.g.
>>a=uint64(999999999999999999)
>>c=java.math.BigInteger(num2str(a))
When using double values you have loss of precission, e.g.
>> a=999999999999999999;
>> sprintf('%16.0f',a)
ans =
1000000000000000000
>>
  2 件のコメント
kkc
kkc 2014 年 5 月 21 日
thanks,, it can cover my math up to 64 bit,,
any idea if i want to do more than 64 bit???
Friedrich
Friedrich 2014 年 5 月 21 日
If it gets higher I would suggest using Symbolic Math Toolbox:
>> bigint = sym('999999999999999999')^10000;
>> mod(bigint,32)
ans =
1
So you can do you complete calculation using symbolic math.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by