Here is the reference link of code
https://developer.mpesa.vm.co.mz/documentation/#restfull-api
generate the encrypted authorization Bearer from the API Key and the Public Key
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
// Download: https://commons.apache.org/proper/commons-codec/download_codec.cgi
import org.apache.commons.codec.binary.Base64;
public class Main {
public static void main(String[] args) {
String publicKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAszE+xAKVB9HRarr6/uHYYAX/RdD6KGVIGlHv98QKDIH26ldYJQ7zOuo9qEscO0M1psSPe/67AWYLEXh13fbtcSKGP6WFjT9OY6uV5ykw9508x1sW8UQ4ZhTRNrlNsKizE/glkBfcF2lwDXJGQennwgickWz7VN+AP/1c4DnMDfcl8iVIDlsbudFoXQh5aLCYl+XOMt/vls5a479PLMkPcZPOgMTCYTCE6ReX3KD2aGQ62uiu2T4mK+7Z6yvKvhPRF2fTKI+zOFWly//IYlyB+sde42cIU/588msUmgr3G9FYyN2vKPVy/MhIZpiFyVc3vuAAJ/mzue5p/G329wzgcz0ztyluMNAGUL9A4ZiFcKOebT6y6IgIMBeEkTwyhsxRHMFXlQRgTAufaO5hiR/usBMkoazJ6XrGJB8UadjH2m2+kdJIieI4FbjzCiDWKmuM58rllNWdBZK0XVHNsxmBy7yhYw3aAIhFS0fNEuSmKTfFpJFMBzIQYbdTgI28rZPAxVEDdRaypUqBMCq4OstCxgGvR3Dy1eJDjlkuiWK9Y9RGKF8HOI5a4ruHyLheddZxsUihziPF9jKTknsTZtF99eKTIjhV7qfTzxXq+8GGoCEABIyu26LZuL8X12bFqtwLAcjfjoB7HlRHtPszv6PJ0482ofWmeH0BE8om7VrSGxsCAwEAAQ==";
String apiKey = "aaaab09uz9f3asdcjyk7els777ihmwv8";
System.out.println(getBearerToken(apiKey, publicKey));
}
private static String getBearerToken(String apiKey, String publicKey) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA");
byte[] encodedPublicKey = Base64.decodeBase64(publicKey);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
PublicKey pk = keyFactory.generatePublic(publicKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, pk);
byte[] encryptedApiKey = Base64.encodeBase64(cipher.doFinal(apiKey.getBytes("UTF-8")));
return new String(encryptedApiKey, "UTF-8");
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
}
```nodejs
I already try the below code but it was not working
const ecKeyUtils = require('eckey-utils');
var crypto = require('crypto');
const curveName = 'secp256k1';
const pems = ecKeyUtils.generateDer({curveName, publicKey: publicKeyString});
var publicKey = pems.publicKey
const { generateKeyPair } = require('crypto');
generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: "aaaab09uz9f3asdcjyk7els777ihmwv8"
}
// Handle errors and use the generated key pair
}, (err, publicKey, privateKey) => {
console.log(err, publicKey, privateKey)
publicKey
});
const publickKeyObject = crypto.createPublicKey(publicKey);
publickKeyObject.export({ format: 'pem', type: 'pkcs1' });
console.log(publickKeyObject)
error – node:internal/crypto/keys:607handle.init(kKeyTypePublic, data, format, type, passphrase);
Error: error:1E08010C:DECODER routines::unsupportedat Object.createPublicKey (node:internal/crypto/keys:607:12)at Object. (E:reactTestnodetestindex.js:195:33)at Module._compile (node:internal/modules/cjs/loader:1159:14)at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)at Module.load (node:internal/modules/cjs/loader:1037:32)at Module._load (node:internal/modules/cjs/loader:878:12)at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)at node:internal/main/run_main_module:23:47 {library: ‘DECODER routines’,reason: ‘unsupported’,code: ‘ERR_OSSL_UNSUPPORTED’}