I'm working with Java, and my colleague is working with .NET. We each have to link to a page on each other's websites. When connecting, SSO processing is required, and at this time, 'current time (yyyy-MM-dd HH:mm:ss)' must be sent and received through RSA encryption.
First of all, I'm testing whether RSA encryption and decryption work properly. My colleague shared a public key. When I encrypt and decrypt with this, I keep getting the error "Data must not be longer than 256 bytes". What's wrong with my code?
I read other similar posts. But those are not what I'm looking for. I want to implement this function without using AES algorithm.
The shared public key is like this: (I can't show you all, so I'll show you some.)
-xml type
<RSAKeyValue><Modulus>0Lvg8dDJA5z0VbYLVfzXT1pC7PJIY[...]AHuFqh/TQSuDENoLlOYfk4MSiNq2P5J6HMrj4MQ==</Modulus><Exponent>AQAB</Exponent><P>+IvTFBd3ldPsx5MYvxV[...]+kJQ==</D></RSAKeyValue>
-pem type
MIIBIjANBgkqhkiG9w0BAQ[...]5J6HMrj4MQIDAQAB
Encryption Code
@RequestMapping(value="/ezConn/getEncryptedAkey1.do")
@ResponseBody
public String getEncryptedAkey1(@RequestParam String userId, HttpServletRequest request, HttpServletResponse response) {
logger.debug("getEncryptedAkey1 started. userId=" + userId);
String encryptedAkey = "";
try {
//Modulus
String modulusInBase64 = "0Lvg8dDJA5z0VbYLVfzXT1pC7PJIY[...]AHuFqh/TQSuDENoLlOYfk4MSiNq2P5J6HMrj4MQ==";
//Exponent
String exponentInBase64 = "AQAB";
java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
String modulusInHex = toHexString(decoder.decode(modulusInBase64));
String exponentInHex = toHexString(decoder.decode(exponentInBase64));
BigInteger modulus = new BigInteger(modulusInHex, 16);
BigInteger pubExp = new BigInteger(exponentInHex, 16);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
//the current time
String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
byte[] cipherData = cipher.doFinal(now.getBytes());
encryptedAkey = toHexString(cipherData);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
logger.debug("encryptedAkey=" + encryptedAkey);
logger.debug("getEncryptedAkey1 ended.");
return encryptedAkey;
}
private String toHexString(byte[] array) {
return DatatypeConverter.printHexBinary(array);
}
Decryption Code
@RequestMapping(value = "/ezConn/getDecryptedAkey.do")
public String getDecryptedAkey(@RequestParam String Akey, HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.debug("getDecryptedAkey started");
logger.debug("Akey=" + Akey);
String decryptedAkey = "";
try {
String modulusInBase64 = "0Lvg8dDJA5z0VbYLVfzXT1pC7PJIY[...]AHuFqh/TQSuDENoLlOYfk4MSiNq2P5J6HMrj4MQ==";
String exponentInBase64 = "AQAB";
java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
String modulusInHex = toHexString(decoder.decode(modulusInBase64));
String exponentInHex = toHexString(decoder.decode(exponentInBase64));
BigInteger modulus = new BigInteger(modulusInHex, 16);
BigInteger pubExp = new BigInteger(exponentInHex, 16);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, pubExp);
RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] cipherData = cipher.doFinal(Akey.getBytes());
descryptedAkey = toHexString(cipherData);
}
catch (Exception e) {
logger.error(e.getMessage(), e);
}
logger.debug("decryptedAkey=" + decryptedAkey);
logger.debug("getDecryptedAkey ended");
return decryptedAkey;
}
Error Code
2023-08-03 13:53:10,221 ERROR [http-nio-8080-exec-111] [egovframework.ezEKP.ezConn.web.EzConnController] Data must not be longer than 256 bytes
javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:347) ~[sunjce_provider.jar:1.8.0_332]
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:404) ~[sunjce_provider.jar:1.8.0_332]
at javax.crypto.Cipher.doFinal(Cipher.java:2168) ~[?:1.8.0_332]
at egovframework.ezEKP.ezConn.web.RsaTestController.getDecryptedAkey(RsaTestController.java:165) [classes/:?]