Java AES:没有安装的提供程序支持此键:javax.crypto.spec.SecretKeySpec [英] Java AES: No installed provider supports this key: javax.crypto.spec.SecretKeySpec

查看:2746
本文介绍了Java AES:没有安装的提供程序支持此键:javax.crypto.spec.SecretKeySpec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置128位AES加密,我在我的Cipher.init上抛出一个异常:



没有已安装的提供程序支持此键:javax.crypto.spec.SecretKeySpec



我使用以下代码在客户端生成密钥:

  private KeyGenerator kgen; 
try {
kgen = KeyGenerator.getInstance(AES);
} catch(NoSuchAlgorithmException e){
// TODO自动生成的catch块
e.printStackTrace();
}
kgen.init(128);
}
SecretKey skey = kgen.generateKey();

然后将此键作为标头传递到服务器。它是Base64编码使用这个功能:

  public String secretKeyToString(SecretKey s){
Base64 b64 = new Base64 );
byte [] bytes = b64.encodeBase64(s.getEncoded());
return new String(bytes);
}

服务器拉出标题,然后



protected static byte [] encrypt(byte [] data,String base64encodedKey)throws InvalidKeyException,IllegalBlockSizeException,BadPaddingException {
Cipher cipher;
try {
cipher = Cipher.getInstance(AES);
} catch(NoSuchAlgorithmException ex){
//日志错误
} catch(NoSuchPaddingException ex){
//日志错误
}
SecretKey key = b64EncodedStringToSecretKey(base64encodedKey);
cipher.init(Cipher.ENCRYPT_MODE,key); //这是它失败的地方
data = cipher.doFinal(data);
return data;
}
private static SecretKey b64EncodedStringToSecretKey(String base64encodedKey){
SecretKey key = null;

try {
byte [] temp = Base64.decodeBase64(base64encodedKey.getBytes());
key = new SecretKeySpec(temp,SYMMETRIC_ALGORITHM);
} catch(Exception e){
// Do nothing
}

return key;
}

为了调试这个,我在客户端,并且在服务器端的cipher.init之前。根据Netbeans,组成SecretKeys的字节是相同的,并且长度为16个字节(事实上,据我所知,对象是相同的)。



我知道无限强度的JCE东西,但我不是在为128位AES需要它的印象。



客户端:java版本1.6。 0_26



服务器端:java版本1.6.0_20



任何想法?

解决方案

我使用不同的方式运行你的代码:Java 1. {5,6,7}(使用AES);不同的Base64编解码器(Apache Commons Codec,DatatypeConverted,Base64);不同的字符集;在不同的JVM之间(通过套接字)…没有效果。我没有错误。



要缩小问题,您可以在两端上运行以下代码?

  static {
System.out.println(System.getProperty(java.version));
for(Provider provider:Security.getProviders())
System.out.println(provider);
}

public static void main(String [] args)throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
}

(我知道你已经说过JDK版本了'



假设密钥在从客户端传输到服务器时不会被破坏(或者可能是相反的),那么如果:




  • 客户端抛出,但服务器不会 - 错误是在客户端;

  • 客户端不会抛出,而是服务器发生错误,错误在服务器端;

  • 客户端和服务器都抛出


  • 在任何情况下,如果发生错误,请将全部

em>堆栈跟踪某处。错误没有安装的提供程序支持这个键:javax.crypto.spec.SecretKeySpec 告诉我们什么(至少对我来说不是,我无法重现这个特定错误)。


I'm trying to set up 128 bit AES encryption, and I'm getting an exception thrown on my Cipher.init:

No installed provider supports this key: javax.crypto.spec.SecretKeySpec

I'm generating the Key on the client side using the following code:

private KeyGenerator kgen;
try {
        kgen = KeyGenerator.getInstance("AES");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    kgen.init(128);
}
SecretKey skey = kgen.generateKey();

This key is then passed to the server as a header. it is Base64 encoded using this function:

public String secretKeyToString(SecretKey s) {
        Base64 b64 = new Base64();
        byte[] bytes = b64.encodeBase64(s.getEncoded());
        return new String(bytes);
}

The server pulls the header, and does

protected static byte[] encrypt(byte[] data, String base64encodedKey) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES");
    } catch (NoSuchAlgorithmException ex) {
        //log error
    } catch (NoSuchPaddingException ex) {
        //log error
    }
    SecretKey key = b64EncodedStringToSecretKey(base64encodedKey);
    cipher.init(Cipher.ENCRYPT_MODE, key); //THIS IS WHERE IT FAILS
    data = cipher.doFinal(data);
    return data;
}
private static SecretKey b64EncodedStringToSecretKey(String base64encodedKey) {
    SecretKey key = null;

    try {
        byte[] temp = Base64.decodeBase64(base64encodedKey.getBytes());
        key = new SecretKeySpec(temp, SYMMETRIC_ALGORITHM);
    } catch (Exception e) {
        // Do nothing
    }

    return key;
}

To debug this, I put breakpoints after both the key generation on the client side, and just before the cipher.init on the server side. According to Netbeans, the bytes that make up the SecretKeys are identical and are 16 bytes in length (In fact, as far as I can tell, the objects are identical).

I am aware of the unlimited strength JCE stuff, but I'm not under the impression I needed it for 128 bit AES.

Client Side: java version "1.6.0_26"

Server Side: java version "1.6.0_20"

Any Ideas?

解决方案

I've run your code in different ways, with: Java 1.{5,6,7} (using AES); different Base64 codecs (Apache Commons Codec, DatatypeConverted, Base64); different character sets; between different JVMs (through sockets) … to no avail. I got no errors.

To narrow down the problem, can you run the following code on both ends?

static {
  System.out.println(System.getProperty("java.version"));
  for (Provider provider : Security.getProviders())
    System.out.println(provider);
}

public static void main(String[] args) throws Exception {
  KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  keyGenerator.init(128);
  SecretKey secretKey = keyGenerator.generateKey();
  Cipher cipher = Cipher.getInstance("AES");
  cipher.init(Cipher.ENCRYPT_MODE, secretKey);
}

(I know that you've already stated the JDK versions you're using and stuff, but it can't hurt.)

Given that the key doesn't get corrupted while you transfer it from client to server (or maybe in reverse), then if:

  • the client throws, but the server doesn't—the error is on the client side;
  • the client doesn't throw, but the server does—the error is on the server side;
  • the client and server both throws or neither of them—needs further investigation.

In any case, if an error is thrown, please post the whole stack trace somewhere. The error No installed provider supports this key: javax.crypto.spec.SecretKeySpec tells us nothing (at least for me it doesn't, and I couldn't reproduce this particular error either).

这篇关于Java AES:没有安装的提供程序支持此键:javax.crypto.spec.SecretKeySpec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆