モジュラスが q,p 素数からランダムに生成され、それぞれ 128 バイト - 1024 ビットであるとき、大きな整数を使用して Java で RSA 暗号化を実装しようとしています。
私の問題は、モジュラスが 257 バイトで、最初のバイトが 0 で、2 番目のバイトが常に 1 (1* *) ( star = 何でも 0\1。) それ以外の場合は、最初のバイトが 0 で始まる正の 256 バイトです。
modPow にモジュラスと指数を送信すると、次のようになります。
java.lang.ArithmeticException: BigInteger: modulus not positive
最初の 0 バイトを削除して他の 256 バイトを残そうとしても、この問題が発生します。
いくつかのコード例:
BigInteger p = new BigInteger(1024,20,new Random());
BigInteger q = new BigInteger(1024,20,new Random());
//multiplying p & q and inserting it to modulus
ByteArray modulus = new ByteArray( p.multiply(q).toByteArray());
if (modulus.length()==257)
{
//if the first byte is 00 then erasing it
flag =false;
ByteArray temp = new ByteArray(TypeUtils.subArray(modulus.getByteArray(), 1, 256));
modulus = temp;
}
BigInteger modulusInBig = new BigInteger(TypeUtils.Byte2byte( modulus.getByteArray()) );
BigInteger answer = inTextInBig.modPow(exponentInBig, modulusInBig);