RSA encrypted data generated in nativescript is different from data generated with java code


#1

Hi,
I have translated java code to encrypt text to nativescript code.

The data encrypted (Base64) is different.

The data produced by java code is OK, I tested it in other internet page.

The data produced by nativescript yields error in the internet page decrypter.

Plase, con you help me to find the iusse?

MTIA.

Java code

package mx.bit.com;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.apache.commons.codec.binary.Base64;

public class RSA {
private static final String CA_COMPONENT_MODULUS = “vavijDCnhkws3QMkq7S4U0OdKV4XIQVhuNZuzaZ3+P5uhzJ2OLS6pexWGE4TNP1jL3WQQ7cN2tGJeeGbjMfKQnJmcF7x6+RLu9kqJ2JjkB/qB8197LWmjivEnFa8LlfCcgwiQsNwoLauIqaKAe6rTiWOty4MYBYfaEt1kapg/qarghEO+jtx+rXO8dEsX5Y5iGDmFwvjuWgLrRQfBnu5/J3vjnz5mT46mRe1VXPpjXPBtPKYSoRwt2UQZ8nwgVyycZgK33ncQx3lvHwciUXvsRcoc9IuE/sj7TaSBAWNu/UJ0Cs7lFgDh9/Os2J1/x9pZ7pMw+X1dNaCq6qTJ7MdQQ==”;
private static final String CA_COMPONENT_EXPONENT = “AQAB”;
private static final String INPUT = “Cuenta:1234-9876-4563-1234”;

public static PublicKey getCaComponentPubkey() throws NoSuchAlgorithmException, InvalidKeySpecException, UnsupportedEncodingException {
	  BigInteger modulus=new BigInteger(1,Base64.decodeBase64(CA_COMPONENT_MODULUS));
	  BigInteger exponent=new BigInteger(1,Base64.decodeBase64(CA_COMPONENT_EXPONENT));
	  RSAPublicKeySpec publicKeySpec=new RSAPublicKeySpec(modulus,exponent);
	  KeyFactory keyFactory=KeyFactory.getInstance("RSA");
	  return keyFactory.generatePublic(publicKeySpec);
}

public static void main(String args[]) {
	try {
		PublicKey pk =  getCaComponentPubkey();
		System.out.println(pk.toString());
		System.out.println(pk.getAlgorithm());
		Cipher cipher = Cipher.getInstance("RSA");
		try {
			cipher.init(Cipher.ENCRYPT_MODE, pk);
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte[] cipherData = cipher.doFinal(INPUT.getBytes());
		
		System.out.println(Base64.encodeBase64String(cipherData));
		
	} catch (NoSuchAlgorithmException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InvalidKeySpecException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchPaddingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalBlockSizeException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (BadPaddingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}

Nativescript Code

const Base64 = android.util.Base64;
const Cipher = javax.crypto.Cipher;
const KeyPairGenerator = java.security.KeyPairGenerator;
const RSAPublicKeySpec = java.security.spec.RSAPublicKeySpec;
const X509EncodedKeySpec = java.security.spec.X509EncodedKeySpec;
const KeyFactory = java.security.KeyFactory;
const Math = java.math;

export class CryptoBIT {

public static generaPublicKey() {
let modulus = “vavijDCnhkws3QMkq7S4U0OdKV4XIQVhuNZuzaZ3+P5uhzJ2OLS6pexWGE4TNP1jL3WQQ7cN2tGJeeGbjMfKQnJmcF7x6+RLu9kqJ2JjkB/qB8197LWmjivEnFa8LlfCcgwiQsNwoLauIqaKAe6rTiWOty4MYBYfaEt1kapg/qarghEO+jtx+rXO8dEsX5Y5iGDmFwvjuWgLrRQfBnu5/J3vjnz5mT46mRe1VXPpjXPBtPKYSoRwt2UQZ8nwgVyycZgK33ncQx3lvHwciUXvsRcoc9IuE/sj7TaSBAWNu/UJ0Cs7lFgDh9/Os2J1/x9pZ7pMw+X1dNaCq6qTJ7MdQQ==”;
let exponent = “AQAB”;

let modulusBase64 = new java.lang.String( modulus );
let exponentBase64 = new java.lang.String( exponent );

let modulusBytes = Base64.decode(modulusBase64.getBytes(),Base64.NO_WRAP);
let exponentBytes = Base64.decode(exponentBase64.getBytes(),Base64.NO_WRAP);

let modulusBigInt = new Math.BigInteger(1, modulusBytes);
let exponentBigInt = new Math.BigInteger(1, exponentBytes);

let keySpec = new RSAPublicKeySpec(modulusBigInt, exponentBigInt);
let fact = KeyFactory.getInstance("RSA");
let pubKey = fact.generatePublic(keySpec); 

return pubKey;  // This is the key will be used to encrypt...

}

public static encriptaRSA(publicRSAKey:java.security.PublicKey): java.lang.String {

let textToEncrypt = "Cuenta:1234-9876-4563-1234";
let textToEncryptBytes = new java.lang.String(textToEncrypt).getBytes();

//let cipher = Cipher.getInstance("RSA/NONE/OAEPPadding");
let cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicRSAKey);

let encryptedByteData = cipher.doFinal(textToEncryptBytes);

let textEncryptedBase64 = new java.lang.String( Base64.encodeToString(encryptedByteData,Base64.NO_WRAP) );
return textEncryptedBase64;

}

}