How do you encrypt a password by using the RSA Alg

2020-06-30 00:41发布

I am new to encryption and I got these instructions

Use the RSA algorithm, and use PKCS #1.5 padding (not OAEP), and add the result to the encrypted stream –this becomes the encrypted password." String os from my code is a public key.

I have imported some jars; org-apache-commons-codec.jar and bcprov-jdk15-130.jar.

For some reasons I get an error on this line System.out.println("\n"+ "\n" +"encryptionresult" + "\n" + "\n" + Base64.toBase64String(cipher.doFinal(initiatorpassword.getBytes())) + "\n");

The error highlights toBase64String only on the whole string.

ERROR: "cannot find symbol method toBase64String."

import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.bouncycastle.util.encoders.Base64;
import sun.misc.BASE64Decoder;

public class encrypt {

    public static void main(String[] args) {

        encrypt obj = new encrypt();

        obj.process();
    }

    public void process() {


        try {

            String initiatorpassword = "giddibon!";    


            String os = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqLcFdVcV7HdEOotsNLoMPhD74CX1ejzcgfNuiJNy9pTySxbszRBCWxmok3Unul4rX/zyVD/6vDb9nbqRywZIgR46UOn+tR3vGXXPX6igxgS6DYTaQV8W858yOGLuoYwRi5xeQJfczAMU4o+sCxlBbMCqYs4nzW81fi8iF2OEUdrfJcbamhSnksdgfD/nomWy9MESAz1QufrOBnaRX2N0CKsi8SNmzsghpfP15VLiIVV8YXPFKtd9sY37FpY28OKGjKG5wdije/bzFL8qEcPDhqYGuVaGkhX1bkI0iH+UcFtYYrZv/Fyb5jRHXmNLiq4mMG0fMH8ENxNACFtRZTDIIQIDAQAB";


            PublicKey publickeyos = rpos(os);


            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");


            cipher.init(Cipher.ENCRYPT_MODE, publickeyos);

             //error on this line
            System.out.println("\n"+ "\n" +"encryptionresult" + "\n" + "\n" + Base64.toBase64String(cipher.doFinal(initiatorpassword.getBytes())) + "\n");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public PublicKey rpos(String file) throws InvalidKeySpecException, NoSuchAlgorithmException, IOException {


        byte[] keyBytes = file.getBytes();

        String pubKey = new String(keyBytes);

        BASE64Decoder decoder = new BASE64Decoder();

        keyBytes = decoder.decodeBuffer(pubKey);

        // generate public key
        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PublicKey publicKey = keyFactory.generatePublic(spec);        

        return publicKey;

    }
}

2条回答
一纸荒年 Trace。
2楼-- · 2020-06-30 01:36

I guess you will have to use the methoad as below and check if it compiles

Base64.getEncoder().encodeToString(cipher.doFinal(initiatorpassword.getBytes()))
查看更多
一夜七次
3楼-- · 2020-06-30 01:40

The method you are looking for is this

org.bouncycastle.util.encoders.Base64.encode(byte[] data)

I'm not sure where you got the reference to Base64.toBase64String, but a quick search shows that is very close to a similar C# method.

查看更多
登录 后发表回答