Skip to Content
0

ABAP to encrypt password with a RSA public-key string

Feb 07, 2017 at 09:11 AM

228

avatar image

Hi guys

Is there any possible to implement the following java code in ABAP?

As you can see, I have a public-key string and users will input their password.

Now I need to get the encrypted password with PKCS#1 protocol.

I have read a lot of articles like https://archive.sap.com/discussions/thread/3587670.

However I have no idea how to use SSF_KRN_ENVELOPE function.

public class Main {
    static final String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7JL0DcaMUHumSdhxXTxqiABBC\n" +
            "DERhRJIsAPB++zx1INgSEKPGbexDt1ojcNAc0fI+G/yTuQcgH1EW8posgUni0mcT\n" +
            "E6CnjkVbv8ILgCuhy+4eu+2lApDwQPD9Tr6J8k21Ruu2sWV5Z1VRuQFqGm/c5vaT\n" +
            "OQE5VFOIXPVTaa25mQIDAQAB";
    public String userid;
    public String tokenid;

    private static String RSAEncode(String pass) throws IOException, GeneralSecurityException {
        KeyFactory kf = KeyFactory.getInstance( "RSA" );
        PublicKey publicKey = kf.generatePublic( new X509EncodedKeySpec( new BASE64Decoder().decodeBuffer( pubKey ) ) );

        Cipher cipher = Cipher.getInstance( "RSA/ECB/PKCS1Padding" );
        cipher.init( Cipher.ENCRYPT_MODE, publicKey );
        return new BASE64Encoder().encode( cipher.doFinal( pass.getBytes( "UTF-8" ) ) ).replace( "\r\n", "\n" );
    }

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

0 Answers