1

We are facing issue with the encryption-decryption of data.In server-side we are having decryption java code like this

public static String decrypt(byte[] data, PrivateKey base64PrivateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    return new String(cipher.doFinal(data));
}

For encryption in the client-side we are using jsencrypt. code looks like this

encrypt(numInfo) {
 this.readKeytext('assets/public.pem');
 getKey = getKey.replace(/(^"|"$)/g, ''); 
 if (cardNum.length > 117) { 
 } else {
   this.$encrypt.setPublicKey(getKey);
   this.cypherCardnum = this.$encrypt.encrypt(numInfo); 
   return this.cypherCardnum;
 }
}

We are using the RSA-3072 key generation technique for creating public and private keys. Can anyone suggest me the best way of doing the encryption in angularjs that will suit with the server-side implementation?


Update:

Sample:

Original data : 4000000000000002

Private key :

MIIG/AIBADANBgkqhkiG9w0BAQEFAASCBuYwggbiAgEAAoIBgQCj19SkjHjzHdIv
CSOWMvXLFfLj2wKRcgZakSVSmqB/K1yeXE/ZMGVv/cnZD0qNbg29PHUIm6DkUKoY
xg6nkqdyzMDAIpEb1dIhzDctIqqPYOnl2FNwUY6h5F98G7v0cEKKYdXpFRfYgrLZ
CmqDxIV7O5TDfJXMIDkXBzootPPxrWCkQ/FjbIN04SNeiwiKmO6exdPEYUklHR8H
BPQLeooSMVJ3kQYOQzV6HcXmSm7AEzx4tn4hcHy6yV5GXkXrcJxRUaRYYR5p/lG9
FgCTZVGRGuXYnxKGcmRzYZgB6vbdxjT7lUgaeZ+YR8EjrVOZ+ath+quNot9TBFXL
e1k2Ympdf6UwTckDWl+qovFyV7bxEWKBCVuECVatcMXlaBKaAGBdTc2WKXrUCnok
0bgTti7SscXwgZwb+Qa/XNes0PXPn/obpc9WfOQXLFi4Wd7ybRdHwck3NxB231VN
uinL9hk8EvFcWiSutTBZClBGSH3xxJ3yk5dWDqL0dVvOpW6dzU8CAwEAAQKC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8usleRl5F63CcUVGkWGEeaf5RvRYAk2VR
kRrl2J8ShnJkc2GYAer23cY0+5VIGnmfmEfBI61TmfmrYfqrjaLfUwRVy3tZNmJq
XX+lME3JA1pfqqLxcle28RFigQlbhAlWrXDF5WgSmgBgXU3Nlil61Ap6JNG4E7Yu
0rHF8IGcG/kGv1zXrND1z5/6G6XPVnzkFyxYuFne8m0XR8HJNzcQdt9VTbopy/YZ
PBLxXFokrrUwWQpQRkh98cSd8pOXVg6i9HVbzqVunc1PAgMBAAE=

Encrypted data:

MtZahearfNk7awePM/B11fsx0DcUVnjWF9vrVkAG4g7wNNeZyMAN0F2JFO2SesfjAlv5THtycRh/5DZNBQDQDeWL2O0FEF4ByY/ZsBnJAA16n9iICK17ECuIAl2Yzlra2S+10uS5A7YFkzDRfQpwfv3S8FZ/srqT8kdTDHb5Rn+3XA18h39qyxvwL6+7WueTkE8bcRTKtaOfANspsxxa28NfwnR3yhyPQsETHWFklJChFE08ULWKf9ILT3j1+rME8ZNyUZ25B9VKJyaQ6Dk/u2M127SfQNvJf0ATlM/WRTIiVJ5Xx+k4sW9WsAGu2pQnrN43eGor5fbrkwnY7sNA12oLkGvP40UCsB2OOW1tNd0PiwAb3IN49xtPvKDFCjJiKBK8zz5DOmGvUdAIpeK2WoU6OeYIkouXlFWTRFQjbbS37vHWVz2IWB+4gyBGBGjOX3uWT3y+V5O729BWNf0j95je3EnX2fcEbXVi4FyHcrheTgSID/FLPqW/dVhO56yO

Exception:

java.lang.IllegalArgumentException: Illegal base64 character 2e at
java.util.Base64$Decoder.decode0(Base64.java:714) at 
java.util.Base64$Decoder.decode(Base64.java:526) at
com.ibsplc.ps.common.utils.RSAUtil.getPrivateKey(RSAUtil.java:63)

Update:

public static PublicKey getPublicKey(String base64PublicKey){ 
    PublicKey publicKey = null; 
    try{ 
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes()));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
        publicKey = keyFactory.generatePublic(keySpec); 
        return publicKey; 
    } catch (NoSuchAlgorithmException e) { 
        e.printStackTrace(); 
    } catch (InvalidKeySpecException e) { 
        e.printStackTrace(); 
    } 
    return publicKey; 
}
Topaco
  • 18,591
  • 2
  • 12
  • 39
Süresh AK
  • 325
  • 1
  • 18
  • I can't reproduce the issue. Encryption with JSEncrypt and decryption with Java using a 3072 bit keypair worked for me. You should describe the problem in more detail: How does the error occur, is there an exception? Does the problem affect all keypairs or only certain ones? Can you provide sample data: test keypair, plaintext, ciphertext (e.g. Base64 encoded)? – Topaco Mar 12 '20 at 19:30
  • You should also justify why you're using this for transport security instead of TLS... – Luke Joshua Park Mar 12 '20 at 19:39
  • java.lang.IllegalArgumentException: Illegal base64 character 2e at java.util.Base64$Decoder.decode0(Base64.java:714) at java.util.Base64$Decoder.decode(Base64.java:526) at com.ibsplc.ps.common.utils.RSAUtil.getPrivateKey(RSAUtil.java:63) This is the we are getting. – Süresh AK Mar 13 '20 at 07:12
  • If switching to TLS is an option, it should be done, which would also eliminate the need for troubleshooting. Anyway: The posted keys are valid and belong together. The private key is in PKCS8 format, the public key in X509 format, both as PEM, but without header and footer. On my machine I can use this keypair to encrypt data with JSEncrypt and decrypt it in Java. – Topaco Mar 13 '20 at 12:11
  • The decryption of the ciphertext posted by you results in an _empty_ string (and not `4000000000000002` as expected). Here you would have to check if the posted test data might be wrong or if the plaintext to be encrypted gets lost somewhere _before_ encryption. – Topaco Mar 13 '20 at 12:15
  • The exception was thrown in `com.ibsplc.ps.common.utils.RSAUtil.getPrivateKey`, which may indicate that there is a problem when importing the private key. If possible, post the method or debug through the implementation to determine the reason more precisely. – Topaco Mar 13 '20 at 12:20
  • Is it possible to share a working sample (both frontend and backend logic) to sureshak07@gmail.com ? – Süresh AK Mar 13 '20 at 14:45
  • I'm sorry, this isn't the way SO wants it. I'd try to solve the key import issue first. Debug or post the corresponding code, more precisely the `getPrivateKey`-method. Alternatively, you can test other implementations, e.g. [here](https://stackoverflow.com/a/9755391) (with `RSA` instead of `DSA`). – Topaco Mar 13 '20 at 18:39
  • https://travistidwell.com/jsencrypt/ this is the front end code we are refering. – Süresh AK Mar 14 '20 at 01:52
  • public static PublicKey getPublicKey(String base64PublicKey){ PublicKey publicKey = null; try{ X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes())); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); publicKey = keyFactory.generatePublic(keySpec); return publicKey; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } return publicKey; } – Süresh AK Mar 14 '20 at 01:54
  • 1
    Please post relevant informations in the question and not as a comment (especially methods, as formatting is restricted in comments). Furthermore, the problematic function is rather `getPrivateKey` and not `getPublicKey` (see the exception posted before). Also, please post a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so that the issue can be reproduced. – Topaco Mar 14 '20 at 07:17

1 Answers1

0

I made changes in both the client and server side. Then it worked. Client side change : this.cypherCardnum = this.$encrypt.encrypt(cardNum.toString());

In server side : Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding);

Süresh AK
  • 325
  • 1
  • 18