bad*_*key 6 .net java encryption rsa
在.NET中,我生成了以下公钥文件:
<RSAKeyValue>
<Modulus>xTSiS4+I/x9awUXcF66Ffw7tracsQfGCn6g6k/hGkLquHYMFTCYk4mOB5NwLwqczwvl8HkQfDShGcvrm47XHKUzA8iadWdA5n4toBECzRxiCWCHm1KEg59LUD3fxTG5ogGiNxDj9wSguCIzFdUxBYq5ot2J4iLgGu0qShml5vwk=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
Run Code Online (Sandbox Code Playgroud)
.NET很乐意使用它的常规方法进行加密.
我正在尝试使用此密钥在Java中编码字符串.当我尝试加密字符串时,我遇到了算术异常.
以下是我用来加密的代码:
byte[] modulusBytes = Base64.decode(this.getString(R.string.public_key_modulus));
byte[] exponentBytes = Base64.decode(this.getString(R.string.public_key_exponent));
BigInteger modulus = new BigInteger( modulusBytes );
BigInteger exponent = new BigInteger( exponentBytes);
RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal( new String("big kitty dancing").getBytes() );
Run Code Online (Sandbox Code Playgroud)
它是代码块中的最后一行失败.
我看了很多例子,这是我能想到的最好的例子.如果不明显,则R.string.public_key_modulus是Modulus元素中文本的复制/粘贴,同样适用于指数.
我做错了什么?
Tho*_*nin 11
试试这个:
BigInteger modulus = new BigInteger(1, modulusBytes );
BigInteger exponent = new BigInteger(1, exponentBytes);
Run Code Online (Sandbox Code Playgroud)
否则你最终得到负模数.该BigInteger(byte[])
构造假定签署大端表示.该BigInteger(int, byte[])
构造函数使用所提供的符号位(这里是"1"为"正").
另外,要小心String.getBytes()
,它使用平台"default charset",它取决于运行应用程序的机器的配置."UTF-8"
如果要获得可重现的结果,最好指定显式字符集(例如).
归档时间: |
|
查看次数: |
3460 次 |
最近记录: |