pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Java AES:沒有安裝的提供程序支持此密鑰:javax.cry

Java AES: No installed provider supports this key: javax.crypto.spec.SecretKeySpec(Java AES:沒有安裝的提供程序支持此密鑰:javax.crypto.spec.SecretKeySpec)
本文介紹了Java AES:沒有安裝的提供程序支持此密鑰:javax.crypto.spec.SecretKeySpec的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試設(shè)置 128 位 AES 加密,但我的 Cipher.init 出現(xiàn)異常:

沒有安裝的提供者支持這個密鑰:javax.crypto.spec.SecretKeySpec

我正在使用以下代碼在客戶端生成密鑰:

私鑰生成器 kgen;嘗試 {kgen = KeyGenerator.getInstance("AES");} 捕捉(NoSuchAlgorithmException e){//TODO 自動生成的 catch 塊e.printStackTrace();}kgen.init(128);}SecretKey skey = kgen.generateKey();

然后,此密鑰作為標(biāo)頭傳遞給服務(wù)器.它是使用此函數(shù)進(jìn)行 Base64 編碼的:

public String secretKeyToString(SecretKey s) {Base64 b64 = 新 Base64();byte[] bytes = b64.encodeBase64(s.getEncoded());返回新字符串(字節(jié));}

服務(wù)器拉頭,然后做

protected static byte[] encrypt(byte[] data, String base64encodedKey) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {密碼密碼;嘗試 {cipher = Cipher.getInstance("AES");} 捕捉(NoSuchAlgorithmException ex){//記錄錯誤} 捕捉(NoSuchPaddingException 前){//記錄錯誤}SecretKey 密鑰 = b64EncodedStringToSecretKey(base64encodedKey);cipher.init(Cipher.ENCRYPT_MODE,密鑰);//這是失敗的地方數(shù)據(jù) = cipher.doFinal(data);返回數(shù)據(jù);}私有靜態(tài) SecretKey b64EncodedStringToSecretKey(String base64encodedKey) {密鑰密鑰 = 空;嘗試 {byte[] temp = Base64.decodeBase64(base64encodedKey.getBytes());密鑰 = 新的 SecretKeySpec(臨時,SYMMETRIC_ALGORITHM);} 捕捉(異常 e){//沒做什么}返回鍵;}

為了調(diào)試它,我在客戶端的密鑰生成之后和服務(wù)器端的 cipher.init 之前都放置了斷點(diǎn).根據(jù) Netbeans 的說法,組成 SecretKey 的字節(jié)是相同的,長度為 16 個字節(jié)(事實(shí)上,據(jù)我所知,對象是相同的).

我知道無限強(qiáng)度的 JCE 東西,但我并不認(rèn)為我需要它來實(shí)現(xiàn) 128 位 AES.

客戶端:java版本1.6.0_26"

服務(wù)器端:java 版本1.6.0_20"

有什么想法嗎?

解決方案

我以不同的方式運(yùn)行您的代碼,包括:Java 1.{5,6,7}(使用 AES);不同的 Base64 編解碼器(Apache Commons Codec、DatatypeConverted、Base64);不同的字符集;在不同的 JVM 之間(通過套接字) …無濟(jì)于事.我沒有錯誤.

為了縮小問題范圍,您可以在兩端運(yùn)行以下代碼嗎?

靜態(tài){System.out.println(System.getProperty("java.version"));對于(提供者提供者:Security.getProviders())System.out.println(提供者);}公共靜態(tài) void main(String[] args) 拋出異常 {KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");keyGenerator.init(128);SecretKey secretKey = keyGenerator.generateKey();密碼密碼 = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE,secretKey);}

(我知道您已經(jīng)說明了您正在使用的 JDK 版本等內(nèi)容,但不會有什么壞處.)

鑒于在您將密鑰從客戶端傳輸?shù)椒?wù)器(或者可能是反向傳輸)時密鑰沒有損壞,那么如果:

  • 客戶端拋出,但服務(wù)器沒有——錯誤在客戶端;
  • 客戶端不會拋出,但服務(wù)器會拋出——錯誤在服務(wù)器端;
  • 客戶端和服務(wù)器都拋出或都不拋出——需要進(jìn)一步調(diào)查.

在任何情況下,如果拋出錯誤,請將整個堆棧跟蹤發(fā)布到某處.錯誤 No installed provider supports this key: javax.crypto.spec.SecretKeySpec 告訴我們什么都沒有(至少對我來說沒有,而且我也無法重現(xiàn)這個特定的錯誤).p>

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).

這篇關(guān)于Java AES:沒有安裝的提供程序支持此密鑰:javax.crypto.spec.SecretKeySpec的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 粘度计,数显粘度计,指针旋转粘度计| 气动机械手-搬运机械手-气动助力机械手-山东精瑞自动化设备有限公司 | 政府回应:200块在义乌小巷能买到爱情吗?——揭秘打工族省钱约会的生存智慧 | 注塑机-压铸机-塑料注塑机-卧式注塑机-高速注塑机-单缸注塑机厂家-广东联升精密智能装备科技有限公司 | ptc_浴霸_大巴_干衣机_呼吸机_毛巾架_电动车加热器-上海帕克 | 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 哔咔漫画网页版在线_下载入口访问指引 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 成都LED显示屏丨室内户外全彩led屏厂家方案报价_四川诺显科技 | 济南冷库安装-山东冷库设计|建造|冷库维修-山东齐雪制冷设备有限公司 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 纯化水设备-EDI-制药-实验室-二级反渗透-高纯水|超纯水设备 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 耐火砖厂家,异形耐火砖-山东瑞耐耐火材料厂 | 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | 知企服务-企业综合服务(ZiKeys.com)-品优低价、种类齐全、过程管理透明、速度快捷高效、放心服务,知企专家! | 范秘书_懂你的范文小秘书| 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 成人纸尿裤,成人尿不湿,成人护理垫-山东康舜日用品有限公司 | 苏商学院官网 - 江苏地区唯一一家企业家自办的前瞻型、实操型商学院 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 上海风淋室_上海风淋室厂家_上海风淋室价格_上海伯淋 | 高压分散机(高压细胞破碎仪)百科-北京天恩瀚拓 | 武汉创亿电气设备有限公司_电力检测设备生产厂家 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 上海公众号开发-公众号代运营公司-做公众号的公司企业服务商-咏熠软件 | 论文查重_免费论文查重_知网学术不端论文查重检测系统入口_论文查重软件 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 福建成考网-福建成人高考网| Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 安徽集装箱厂-合肥国彩钢结构板房工程有限公司 |