【发布时间】:2010-04-22 14:14:44
【问题描述】:
在 .NET 中,我生成了以下公钥文件:
<RSAKeyValue>
<Modulus>xTSiS4+I/x9awUXcF66Ffw7tracsQfGCn6g6k/hGkLquHYMFTCYk4mOB5NwLwqczwvl8HkQfDShGcvrm47XHKUzA8iadWdA5n4toBECzRxiCWCHm1KEg59LUD3fxTG5ogGiNxDj9wSguCIzFdUxBYq5ot2J4iLgGu0qShml5vwk=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
.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() );
这是代码块中失败的最后一行。
我看过很多例子,这是我能想到的最好的例子。如果不明显,R.string.public_key_modulus 是 Modulus 元素中文本的复制/粘贴,同样适用于指数。
我做错了什么?
【问题讨论】:
标签: java .net encryption rsa