简体   繁体   English

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

[英]java.security.NoSuchAlgorithmException: RSA Signature not available

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.如果运行以下代码,您将获得 Java 安装支持的签名算法列表。

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:当我运行它(Windows,Java 1.8.0_65)时,我得到:

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.如您所见, RSA不是有效的签名算法。
Maybe NONEwithRSA is what you're after?也许NONEwithRSA是你所追求的?

Please always refer to the documentation请始终参考文档在此处输入图片说明

Documentation 文档

Specify a valid algorithm.指定有效的算法。 The hash algorithm needs to be specified.需要指定哈希算法。 For example, SHA256withRSA .例如, SHA256withRSA

I've checked the algorithms supported by java versions(1.7) & (1.8) in my machine.我已经在我的机器上检查了 java 版本(1.7)和(1.8)支持的算法。 One of my project runs on jdk 1.7.0_80, which doesn't support SHA224withRSA algorithm, if you're in same situation then upgrade to newer version of java atleast to (Java SE 7 Update 131)as I've read it should have similar algorithms as java 8. If no option to update java then try adding org.bouncycastle bcprov-jdk15on maven dependency in pom or a jar file to your project & also in java code add where you build SSLContext/HttpClient include below line:我的一个项目在 jdk 1.7.0_80 上运行,它不支持 SHA224withRSA 算法,如果您处于相同的情况,那么至少升级到较新版本的 java 到(Java SE 7 Update 131),因为我读过它应该有与 java 8 类似的算法。如果没有更新 java 的选项,则尝试在 pom 或 jar 文件中添加org.bouncycastle bcprov-jdk15on maven 依赖项到您的项目,并在 java 代码中添加您构建 SSLContext/HttpClient 的位置,包括以下行:

Security.addProvider(new BouncyCastleProvider()); Security.addProvider(new BouncyCastleProvider());

Also tried installing Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 7, but it hasn't worked !!还尝试安装 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 7,但没有成功!!

暂无
暂无

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

相关问题 .java.security.NoSuchAlgorithmException:提供者Cryptix不提供算法RSA / PKCS1 - .java.security.NoSuchAlgorithmException: algorithm RSA/PKCS1 is not available from provider Cryptix java.security.NoSuchAlgorithmException: SHA224withRSA 签名不可用 Java 7 - java.security.NoSuchAlgorithmException: SHA224withRSA Signature not available Java 7 java.security.NoSuchAlgorithmException:未找到签名MD5WITHRSA实现 - java.security.NoSuchAlgorithmException: Signature MD5WITHRSA implementation not found Java 9使用“java.security.NoSuchAlgorithmException:SHA3-384 MessageDigest不可用” - “java.security.NoSuchAlgorithmException: SHA3-384 MessageDigest not available” with Java 9 java.security.NoSuchAlgorithmException: RIPEMD160 MessageDigest 不可用 - java.security.NoSuchAlgorithmException: RIPEMD160 MessageDigest not available java.security.NoSuchAlgorithmException:X509 KeyManagerFactory不可用 - java.security.NoSuchAlgorithmException: X509 KeyManagerFactory not available java.security.NoSuchAlgorithmException:算法PBKDF2WithHmacSHA1不可用 - java.security.NoSuchAlgorithmException: Algorithm PBKDF2WithHmacSHA1 not available java.security.NoSuchAlgorithmException:算法 x25519 不可用 - java.security.NoSuchAlgorithmException: Algorithm x25519 not available SSL java.security.NoSuchAlgorithmException - SSL java.security.NoSuchAlgorithmException java.security.NoSuchAlgorithmException:在jboss上部署时,AES KeyGenerator不可用 - java.security.NoSuchAlgorithmException: AES KeyGenerator not available while deploy on jboss
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM