當我切換到Cipher.getInstance("AES")
當Cipher.getInstance("AES/CBC/NoPadding")
我開始收到此錯誤:爪哇 - InvalidKeyException將使用AES/CBC/NoPadding
12:18:20 [SEVERE] java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
12:18:20 [SEVERE] at javax.crypto.Cipher.chooseProvider(Cipher.java:878)
12:18:20 [SEVERE] at javax.crypto.Cipher.init(Cipher.java:1213)
12:18:20 [SEVERE] at javax.crypto.Cipher.init(Cipher.java:1153)
12:18:20 [SEVERE] at net.azidea.bungee.netty.packet.codec.Encryption.decrypt(Encryption.java:52)
12:18:20 [SEVERE] at net.azidea.bungee.netty.packet.codec.PacketDecrypter.decode(PacketDecrypter.java:20)
12:18:20 [SEVERE] at net.azidea.bungee.netty.packet.codec.PacketDecrypter.decode(PacketDecrypter.java:12)
12:18:20 [SEVERE] at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89)
12:18:20 [SEVERE] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
12:18:20 [SEVERE] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
12:18:20 [SEVERE] at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:161)
12:18:20 [SEVERE] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
12:18:20 [SEVERE] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
12:18:20 [SEVERE] at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
12:18:20 [SEVERE] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
12:18:20 [SEVERE] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
12:18:20 [SEVERE] at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787)
12:18:20 [SEVERE] at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
12:18:20 [SEVERE] at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
12:18:20 [SEVERE] at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
12:18:20 [SEVERE] at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
12:18:20 [SEVERE] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
12:18:20 [SEVERE] at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
12:18:20 [SEVERE] at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
12:18:20 [SEVERE] at java.lang.Thread.run(Thread.java:745)
解密時。它只用AES就沒問題(沒有「CBC/NoPadding」部分)。我改變了這一點,因爲不是我的所有數據包都是16字節的倍數。
key.getAlgorithm()
的輸出是「AES」。
Decryption
public static ByteBuf decrypt(ByteBuf encrypted)
{
try
{
cipher.init(Cipher.DECRYPT_MODE, key);
return Unpooled.copiedBuffer(cipher.doFinal(NettyUtils.toArray(encrypted)));
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("[Netty] Error decrypting packet.");
}
return null;
}
Cipher
private static Cipher cipher;
private static SecretKeySpec key;
static
{
try
{
key = new SecretKeySpec(NettyServer.getSecretKey(), "AES");
cipher = Cipher.getInstance("AES/CBC/NoPadding");
}
catch(Exception e)
{
e.printStackTrace();
}
}
可能的重複[Java AES:沒有安裝的提供程序支持此項:javax.crypto.spec.SecretKeySpec](http://stackoverflow.com/questions/8362262/java-aes-no-installed-provider-supports- this-key-javax-crypto-spec-secretkeysp) –
當你試圖加密時你是否得到了錯誤 – Turtle
或者你是否試圖創建密鑰而得到錯誤?另外你爲什麼要切換到AES/CBC/NoPadding? – Turtle