简体   繁体   English

Java-使用AES / CBC / NoPadding时出现InvalidKeyException

[英]Java - InvalidKeyException when using AES/CBC/NoPadding

When I switched Cipher.getInstance("AES") to Cipher.getInstance("AES/CBC/NoPadding") I started getting this error: 当我将Cipher.getInstance("AES")切换为Cipher.getInstance("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)

when decrypting. 解密时。 It worked fine with just AES (without the "CBC/NoPadding" part). 仅使用AES(没有“ CBC / NoPadding”部分)就可以正常工作。 I changed this because not all of my packets are a multiple of 16 bytes. 我更改了此设置,因为并非我的所有数据包都是16字节的倍数。

The output of key.getAlgorithm() is "AES". 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();
    }
}

指定IvParameterSpec解决了该问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#和Java之间的AES / CBC / NoPadding - AES/CBC/NoPadding between C# and Java 使用 AES/CFB/NoPadding、AES/OFB/NoPadding 或 AES/CBC/NoPadding 的密码太短 - Password too short using AES/CFB/NoPadding, AES/OFB/NoPadding or AES/ CBC/NoPadding 使用AES / CBC / NOPADDING在Node中加密并使用相同算法在JAVA中解密会产生一些垃圾,例如eJJ,.d。* 5。 - Encrypted in Node using AES/CBC/NOPADDING and decrypted in JAVA using same algorithm gives some junk like e�J�,�d�|*�5Ҝ�� 带有OpenResty的resty.aes模块的Java Cipher.getInstance(“ AES / CBC / NoPadding”)解密结果失败 - Decrypt result of Java Cipher.getInstance(“AES/CBC/NoPadding”) failed with OpenResty's resty.aes module AES 256 CBC 加密错误 java.security.InvalidKeyException:无效的 AES 密钥长度:44 字节错误 - AES 256 CBC encryption error java.security.InvalidKeyException: Invalid AES key length: 44 bytes error AES / CBC / NoPadding解密中的输出乱码 - Garbled output in AES/CBC/NoPadding Decryption AES / CBC / NoPadding和IllegalBlockSizeException-自定义填充处理 - AES/CBC/NoPadding and IllegalBlockSizeException - Custom padding handling 加密AES / CBC / NoPadding无法产生正确的结果 - Encryption AES/CBC/NoPadding not yielding the correct results 使用密码短语的CBC的Java AES - Java AES with CBC using passphrase 使用 TDCP_rijndael AES/CBC/NoPadding 算法在 DelphiXE7 中加密并在 Java 中解密 - Crypt in DelphiXE7 with TDCP_rijndael AES/CBC/NoPadding algorithm and decrypt in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM