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

java.security.NoSuchAlgorithmException:RSA 簽名不可用

java.security.NoSuchAlgorithmException: RSA Signature not available(java.security.NoSuchAlgorithmException:RSA 簽名不可用)
本文介紹了java.security.NoSuchAlgorithmException:RSA 簽名不可用的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

這是個(gè)例外

<上一頁>線程主"java.security.NoSuchAlgorithmException 中的異常:RSA 簽名不可用在 java.security.Signature.getInstance(Signature.java:229)在 MailClient.main(MailClient.java:52)

這是我的代碼

import java.io.*;導(dǎo)入java.net.*;導(dǎo)入 java.nio.ByteBuffer;導(dǎo)入 java.util.*;導(dǎo)入java.security.*;公共類 MailClient {公共字符串 getMessage(郵件 m){返回 m.message;}公共靜態(tài) void main(String[] args) 拋出異常 {//初始化BufferedReader br = new BufferedReader(new InputStreamReader(System.in));字符串主機(jī) = args[0];int 端口 = Integer.parseInt(args[1]);字符串用戶 ID = args[2];而(真){//連接到服務(wù)器套接字 s = 新套接字(主機(jī),端口);DataInputStream dis = new DataInputStream(s.getInputStream());DataOutputStream dos = new DataOutputStream(s.getOutputStream());ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());oos.flush();ObjectInputStream ois = new ObjectInputStream(s.getInputStream());//待辦事項(xiàng):登錄//這兩行只是為了讓提供的程序運(yùn)行而不會(huì)崩潰.//你可能想改變它們,當(dāng)然在它們之后添加?xùn)|西dos.writeUTF(userid);字符串 userPrivateKeyFileName = 用戶 ID + ".prv";//獲取創(chuàng)建簽名的密鑰ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(userPrivateKeyFileName));PrivateKey privateKey = (PrivateKey)keyIn.readObject();keyIn.close();//創(chuàng)建時(shí)間戳和隨機(jī)數(shù)long t1 = (new Date()).getTime();雙 q1 = Math.random();//ByteBuffer 稍后轉(zhuǎn)換為字節(jié)ByteBuffer bb = ByteBuffer.allocate(16);bb.putLong(t1);bb.putDouble(q1);//創(chuàng)建簽名,使用時(shí)間戳和隨機(jī)數(shù)作為數(shù)據(jù)簽名 sig = Signature.getInstance("RSA");sig.initSign(privateKey);sig.update(bb.array());字節(jié)[] 簽名 = sig.sign();//發(fā)送數(shù)據(jù)和簽名DataOutputStream out = new DataOutputStream(s.getOutputStream());out.writeUTF(userid);out.writeLong(t1);out.writeDouble(q1);out.writeInt(signature.length);out.write(簽名);out.flush();布爾答案 = dis.readBoolean();//通過了驗(yàn)證登錄如果(回答){//接收多少條消息int numMsg = dis.readInt();System.out.println("你有 " + numMsg + " 收到的消息.");//TO DO:閱讀消息ArrayList<郵件>msg = new ArrayList<>(numMsg);for(int i=0;i) ois.readObject();}而(!msg.isEmpty()){//對(duì)于每封郵件,顯示發(fā)件人、時(shí)間戳、消息System.out.println(msg.get(0).sender);System.out.println(msg.get(0).timestamp);System.out.println(msg.get(0).message);MessageDigest md = MessageDigest.getInstance("SHA-1");md.update(msg.get(0).hashcash);字節(jié)[] 摘要 = md.digest();boolean normalMail = msg.get(0).checkHashcash(digest);如果(正常郵件){//檢查每封郵件是否為原始郵件未被修改//接收郵件System.out.println(msg.get(0).message);}else{System.out.println("這是垃圾郵件");System.out.println(msg.get(0).message);}msg.remove(0);}//發(fā)送信息System.out.println("您要發(fā)送消息[Y/N]?");String wantToSend = br.readLine();如果(!wantToSend.equals(Y")){dos.writeBoolean(false);返回 ;}dos.writeBoolean(true);System.out.println("請(qǐng)輸入收件人的用戶名:");字符串接收者 = br.readLine();System.out.println("請(qǐng)輸入您的信息:");字符串消息 = br.readLine();//TO DO:發(fā)送郵件郵件 m = 新郵件(用戶 ID、收件人、郵件);MessageDigest md = MessageDigest.getInstance("SHA-1");md.update(m.hashcash);字節(jié)[] 摘要 = md.digest();而(m.checkHashcash(摘要)){m.setHashcash(摘要);}out.write(摘要);out.flush();//發(fā)送時(shí)間戳和摘要到服務(wù)器長郵件時(shí)間戳 = m.timestamp.getTime();out.writeLong(mailTimestamp);oos.writeObject(m);}}}}

解決方案

如果你運(yùn)行下面的代碼,你會(huì)得到一個(gè)你的Java安裝支持的簽名算法列表.

TreeSet算法=新樹集<>();對(duì)于(提供者提供者:Security.getProviders())對(duì)于(服務(wù)服務(wù):provider.getServices())if (service.getType().equals("簽名"))算法.add(service.getAlgorithm());對(duì)于(字符串算法:算法)System.out.println(算法);

當(dāng)我運(yùn)行它(Windows,Java 1.8.0_65)時(shí),我得到:

MD2withRSAMD5 和 SHA1 和 RSA帶有 RSA 的 MD5NONEwithDSANONEwithECDSANONEwithRSA帶有 DSA 的 SHA1SHA1 和 ECDSASHA1withRSASHA224 和 DSASHA224 和 ECDSASHA224 和 RSA帶有 DSA 的 SHA256SHA256 和 ECDSASHA256 和 RSASHA3??84 和 ECDSASHA3??84 和 RSASHA512 和 ECDSASHA512 和 RSA

如您所見,RSA 不是有效的簽名算法.
也許 NONEwithRSA 是你所追求的?

this is exception

Exception in thread "main" java.security.NoSuchAlgorithmException: RSA Signature not available
    at java.security.Signature.getInstance(Signature.java:229)
    at MailClient.main(MailClient.java:52)

this is my code

import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.*;
import java.security.*;

public class MailClient {

    public String getMessage(Mail m){
        return m.message;
    }

    public static void main(String[] args) throws Exception {

        // Initialisation
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String host = args[0];
        int port = Integer.parseInt(args[1]);
        String userid = args[2];

        while(true) {
            // connect to server
            Socket s = new Socket(host,port);
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
            oos.flush();
            ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

            // TO DO: login

            // these two lines are here just to make the supplied programs run without crashing.
            // You may want to change them, and certainly add things after them
            dos.writeUTF(userid);

            String userPrivateKeyFileName = userid + ".prv";
            // Get the key to create the signature
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(userPrivateKeyFileName));
            PrivateKey privateKey = (PrivateKey)keyIn.readObject();
            keyIn.close();

            // create timeStamp and random number
            long t1 = (new Date()).getTime();
            double q1 = Math.random();
            // ByteBuffer to convert to bytes later
            ByteBuffer bb = ByteBuffer.allocate(16);
            bb.putLong(t1);
            bb.putDouble(q1);

            // create signature, using timeStamp and random number as data
            Signature sig = Signature.getInstance("RSA");
            sig.initSign(privateKey);
            sig.update(bb.array());
            byte[] signature = sig.sign();

            // send data and signature
            DataOutputStream out = new DataOutputStream(s.getOutputStream());
            out.writeUTF(userid);
            out.writeLong(t1);
            out.writeDouble(q1);
            out.writeInt(signature.length);
            out.write(signature);
            out.flush();

            boolean answer = dis.readBoolean();

            //passed the verifyLogin
            if (answer)
                {
                // receive how many messages
                int numMsg = dis.readInt();
                System.out.println("You have " + numMsg + " incoming messages.");

                // TO DO: read messages
                ArrayList<Mail> msg = new ArrayList<>(numMsg);
                for(int i=0;i<numMsg;i++){
                    //@Unchecked
                    msg = (ArrayList<Mail>) ois.readObject();
                }
                while(!msg.isEmpty()){
                    //for each mail, display sender,timestamp,message
                    System.out.println(msg.get(0).sender);
                    System.out.println(msg.get(0).timestamp);
                    System.out.println(msg.get(0).message);
                    MessageDigest md = MessageDigest.getInstance("SHA-1");
                    md.update(msg.get(0).hashcash);

                    byte[] digest = md.digest();
                    boolean normalMail = msg.get(0).checkHashcash(digest);
                    if(normalMail){
                    //check each mail is original that it isn't modified
                    //receive mail
                        System.out.println(msg.get(0).message);
                        }
                    else{System.out.println("it's a spam message.");
                        System.out.println(msg.get(0).message);
                        }
                    msg.remove(0);
                }


                // send messages
                System.out.println("Do you want to send a message [Y/N]?");
                String wantToSend = br.readLine();
                if (!wantToSend.equals("Y")) {
                    dos.writeBoolean(false);
                    return ;
                }
                dos.writeBoolean(true);

                System.out.println("Enter userid of recipient:");
                String recipient = br.readLine();
                System.out.println("Type your message:");
                String message = br.readLine();

                // TO DO: send mail
                Mail m = new Mail(userid, recipient, message);
                MessageDigest md = MessageDigest.getInstance("SHA-1");
                md.update(m.hashcash);

                byte[] digest = md.digest();
                while(m.checkHashcash(digest)){
                    m.setHashcash(digest);

                }

                out.write(digest);
                out.flush();
                // send timeStamp and digest to server
                long mailTimestamp = m.timestamp.getTime();
                out.writeLong(mailTimestamp);




                oos.writeObject(m);

                }
            }


    }

}

解決方案

If you run the following code, you will get a list of signature algorithms supported by your Java installation.

TreeSet<String> algorithms = new TreeSet<>();
for (Provider provider : Security.getProviders())
    for (Service service : provider.getServices())
        if (service.getType().equals("Signature"))
            algorithms.add(service.getAlgorithm());
for (String algorithm : algorithms)
    System.out.println(algorithm);

When I run it (Windows, Java 1.8.0_65), I get:

MD2withRSA
MD5andSHA1withRSA
MD5withRSA
NONEwithDSA
NONEwithECDSA
NONEwithRSA
SHA1withDSA
SHA1withECDSA
SHA1withRSA
SHA224withDSA
SHA224withECDSA
SHA224withRSA
SHA256withDSA
SHA256withECDSA
SHA256withRSA
SHA384withECDSA
SHA384withRSA
SHA512withECDSA
SHA512withRSA

As you can see, RSA is not a valid signature algorithm.
Maybe NONEwithRSA is what you're after?

這篇關(guān)于java.security.NoSuchAlgorithmException:RSA 簽名不可用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
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 錯(cuò)誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測(cè)不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | 桐城新闻网—桐城市融媒体中心主办 | 上海宿田自动化设备有限公司-双面/平面/单面贴标机 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 吉祥新世纪铝塑板_生产铝塑板厂家_铝塑板生产厂家_临沂市兴达铝塑装饰材料有限公司 | 海外整合营销-独立站营销-社交媒体运营_广州甲壳虫跨境网络服务 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 | 桥架-槽式电缆桥架-镀锌桥架-托盘式桥架 - 上海亮族电缆桥架制造有限公司 | 硬齿面减速机[型号全],ZQ减速机-淄博久增机械 | 道康宁消泡剂-瓦克-大川进口消泡剂供应商 | 酸度计_PH计_特斯拉计-西安云仪 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | 市政路灯_厂家-淄博信达电力科技有限公司 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 房屋质量检测-厂房抗震鉴定-玻璃幕墙检测-房屋安全鉴定机构 | 工业铝型材生产厂家_铝合金型材配件批发精加工定制厂商 - 上海岐易铝业 | 挤塑板-XPS挤塑板-挤塑板设备厂家[襄阳欧格] | 杭州厂房降温,车间降温设备,车间通风降温,厂房降温方案,杭州嘉友实业爽风品牌 | 华夏医界网_民营医疗产业信息平台_民营医院营销管理培训 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | 设定时间记录电子秤-自动累计储存电子秤-昆山巨天仪器设备有限公司 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 苏州教学设备-化工教学设备-环境工程教学模型|同科教仪 | 细胞染色-流式双标-试剂盒免费代做-上海研谨生物科技有限公司 | 365文案网_全网创意文案句子素材站| 国际高中-国际学校-一站式择校服务-远播国际教育 | 管理会计网-PCMA初级管理会计,中级管理会计考试网站 | 合肥汽车充电桩_安徽充电桩_电动交流充电桩厂家_安徽科帝新能源科技有限公司 | 实体店商新零售|微赢|波后|波后合作|微赢集团 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 | PCB接线端子_栅板式端子_线路板连接器_端子排生产厂家-置恒电气 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 样品瓶(色谱样品瓶)百科-浙江哈迈科技有限公司 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 新疆十佳旅行社_新疆旅游报价_新疆自驾跟团游-新疆中西部国际旅行社 | 【365公司转让网】公司求购|转让|资质买卖_股权转让交易平台 |