我正在使用RSA加密JAVA并尝试使用.NET解密.我包含了我的JAVA代码和.NET代码,希望有人有这方面的经验.
JAVA代码:
byte[] modulusBytes = Base64.decode("xTSiS4+I/x9awUXcF66Ffw7tracsQfGCn6g6k/hGkLquHYMFTCYk4mOB5NwLwqczwvl8HkQfDShGcvrm47XHKUzA8iadWdA5n4toBECzRxiCWCHm1KEg59LUD3fxTG5ogGiNxDj9wSguCIzFdUxBYq5ot2J4iLgGu0qShml5vwk=");
byte[] exponentBytes = Base64.decode("AQAB");
BigInteger modulus = new BigInteger(1, modulusBytes );
BigInteger exponent = new BigInteger(1, 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[] plainBytes = new String("big kitty dancing").getBytes("UTF-8");
byte[] cipherData = cipher.doFinal( plainBytes );
String encryptedString = Base64.encodeBytes(cipherData);
Run Code Online (Sandbox Code Playgroud)
从这个JAVA代码我得到的encryptedString的结果恰好是:
FoP4 + AAIH6hcabXnrvNG5YUk/nBv9n9HU0CAgZjkIWQIDjbOpSwoPVBFERrZ6641x2QaoJw5yv18XAay + 0WrCaSw4sveRX + hmPm5qeVUPcjoR4slsVZ/hBFJtAHj9tva4hOugWDZa9s3RVJlxkNfE + U +的Kt/YKLOi2EYbH05HjeM =
并尝试使用以下.NET代码进行解密
const int PROVIDER_RSA_FULL = 1;
const string …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何在调试期间判断我的Android应用程序中是否有特定的ipaddress(我没有在实际设备上尝试过).
从阅读开始,InetAddress.isReachable似乎应该为我做这件事.
最初我认为我可以编写类似的代码:
InetAddress address = InetAddress.getByAddress(new byte [] {(byte)192,(byte)168,(byte)254,(byte)10); success = address.isReachable(3000);
即使我有理由确定它是可到达的地址,这也会返回false.
我发现,如果我将其更改为127,0,0,1,则返回成功.
我的下一次尝试是相同的代码,但我使用了从www.google.com的ping获得的地址(截至撰写本文时为72.167.164.64).没有成功.
那么我尝试了另一个例子:
int timeout = 2000;
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress address : addresses)
{
if ( address.isReachable(timeout))
{
success = true; // just set a break point here
}
}
Run Code Online (Sandbox Code Playgroud)
我对Java和Android比较陌生,所以我怀疑我错过了什么,但我找不到任何可以表明它是什么的东西.
我正在出现一个意外的(对我而言)行为,其中显示垂直滚动条.我不明白容器高度的限制是什么.
通过更改LI的边距:1或设置UL的线高:正常而不是1,我能够解决这个问题.
谁能解释实际发生的事情?也就是说我超过了什么高度需要垂直滚动条?
我创建了一个非常简单的JSFIDDLE来说明我遇到的问题.
HTML代码:
<div class="content-section" >
<ul >
<li>cheese</li>
<li>crackers</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS代码:
body {
line-height: 1;
}
ul {
margin: 0;
}
.content-section {
overflow-y: auto;
}
Run Code Online (Sandbox Code Playgroud) 在.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元素中文本的复制/粘贴,同样适用于指数.
我做错了什么?