添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Getting Exception:javax.crypto.IllegalBlockSizeException: Data must not be longer than 64 bytes

Pradosh Dec 23 2013

Hi ,

I am getting this error while trying to decrypt the message using RSA private Key . I am using the default Integrated Weblogic Server for the Certificates and Private Key ,Public Key.

Any insight will be helpful .

Here is my code .

      

package mypackage;

import java.io.FileInputStream;

import java.io.IOException;

import java.security.Key;

import java.security.KeyStore;

import java.security.NoSuchAlgorithmException;

import java.security.cert.X509Certificate;

import javax.crypto.Cipher;

import javax.security.cert.CertificateException;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class MyEncClass {

public static final String DEMO_IDENTITY_KS =

"C:\\Oracle\\Middleware\\wlserver_10.3\\server\\lib\\DemoIdentity.jks"; //private key

public static final  String DEMO_IDENTITY_PASS =

"DemoIdentityKeyStorePassPhrase"; //trust store password

public static final  String DEMO_TRUST_KS =

"C:\\Oracle\\Middleware\\wlserver_10.3\\server\\lib\\DemoTrust.jks"; //certificates

public static final  String DEMO_TRUST_KS_PASS =

"DemoTrustKeyStorePassPhrase"; //trust store password

public static final String TEST_MSG = "1111-2222-3333-4444";

public static void main(String[] args) throws Exception {

MyEncClass certEncDec = new MyEncClass();

X509Certificate cert =

certEncDec.getCertificate("wlsdemocanew1", DEMO_TRUST_KS);

System.out.println("cert" + cert);

Key publicKey = getPublicKey("wlsdemocanew1", DEMO_TRUST_KS);

System.out.println("publicKey" + publicKey);

Key privateKey =

getPrivateKey("demoidentity", "DemoIdentityPassPhrase",

DEMO_IDENTITY_KS);

System.out.println("privateKey = " + privateKey);

String encryptedStr = certEncDec.encryptStrToStr(TEST_MSG);

System.out.println("Encrypted String " + encryptedStr);

String decryptedStr = certEncDec.decryptStrToStr(encryptedStr);

System.out.println("Decrypted String " + decryptedStr);

}

//

private static String encodeBASE64(byte[] bytes)

{

BASE64Encoder b64 = new BASE64Encoder();

return b64.encode(bytes);

}

//

private static byte[] decodeBASE64(String text) throws IOException

{

BASE64Decoder b64 = new BASE64Decoder();

return b64.decodeBuffer(text);

}

private static Key getPrivateKey(String alias, String password,

String keystore) throws Exception {

KeyStore ks = KeyStore.getInstance("JKS");

ks.load(new FileInputStream(keystore),

DEMO_IDENTITY_PASS.toCharArray());

Key result = ks.getKey(alias, password.toCharArray());

return result;

}

private static X509Certificate getCertificate(String alias,

String keystore) throws Exception {

KeyStore ks = KeyStore.getInstance("JKS");

ks.load(new FileInputStream(keystore),

DEMO_TRUST_KS_PASS.toCharArray());

X509Certificate result = (X509Certificate)ks.getCertificate(alias);

return result;

}

private static Key getPublicKey(String alias,

String keystore) throws Exception {

KeyStore ks = KeyStore.getInstance("JKS");

ks.load(new FileInputStream(keystore),

DEMO_TRUST_KS_PASS.toCharArray());

X509Certificate cert = (X509Certificate)ks.getCertificate(alias);

if (cert != null)

return cert.getPublicKey();

return null;

}

private String encryptStrToStr(String message) {

byte[] messageBytes = null;

byte[] ciphertextBytes = null;

try {

messageBytes = message.getBytes("UTF-8");

//Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

Key publicKey = getPublicKey("wlsdemocanew1", DEMO_TRUST_KS);

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

ciphertextBytes = cipher.doFinal(messageBytes);

} catch (IOException e) {

System.out.println("IOException:" + e);

} catch (CertificateException e) {

System.out.println("CertificateException:" + e);

} catch (NoSuchAlgorithmException e) {

System.out.println("NoSuchAlgorithmException:" + e);

} catch (Exception e) {

System.out.println("Exception:" + e);

}

return  encodeBASE64(ciphertextBytes);

}

private String decryptStrToStr(String message) {

byte[] dectyptedText = null;

String result=null;

try {

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

Key privateKey = getPrivateKey("demoidentity", "DemoIdentityPassPhrase", DEMO_IDENTITY_KS);

cipher.init(Cipher.DECRYPT_MODE, privateKey);

dectyptedText = cipher.doFinal(decodeBASE64(message));

result = new String(dectyptedText, "UTF8");

} catch (IOException e) {

System.out.println("IOException:" + e);

} catch (CertificateException e) {

System.out.println("CertificateException:" + e);

} catch (NoSuchAlgorithmException e) {

System.out.println("NoSuchAlgorithmException:" + e);

} catch (Exception e) {

System.out.println("Exception:" + e);

e.printStackTrace();

}

return result;

}

}

Output is like this

===============

cert[

[

Version: V3

Subject: [email protected] , CN=Demo Certificate Authority Constraints, OU=Security, O=BEA WebLogic, L=San Francisco, ST=California, C=US

Signature Algorithm: MD5withRSA, OID = 1.2.840.113549.1.1.4

Key:  Sun RSA public key, 1024 bits

modulus: 156710490549361998307157784248321138052815240593647721920612234707636672474518428785627598279270666973708373499238042897073583063141391196686695714613451965982321202029488267174706489666542331075371055156322429926478110953568954643597391019270758359531772459952225396781945510442536454329769308555261743601031

public exponent: 65537

Validity: [From: Sat Nov 02 01:32:11 IST 2002,

To: Tue Oct 17 01:32:11 IST 2006]

Issuer: [email protected] , CN=Demo Certificate Authority Constraints, OU=Security, O=BEA WebLogic, L=San Francisco, ST=California, C=US

SerialNumber: [    00]

Certificate Extensions: 1

[1]: ObjectId: 2.5.29.19 Criticality=true

BasicConstraints:[

CA:true

PathLen:1

]

]

Algorithm: [MD5withRSA]

Signature:

0000: 52 FB 4A 2A 5B 11 B2 A9   B6 4E D3 6F 33 7F DB 50  R.J*[....N.o3..P

0010: 4F 64 4A A2 37 EE C3 2C   2B 0E 81 96 F7 A5 27 E4  OdJ.7..,+.....'.

0020: 8A A9 16 70 47 22 EE 5A   B6 58 89 A3 73 94 A6 21  ...pG".Z.X..s..!

0030: F5 F0 A9 30 2C DF 03 54   FF F7 B7 72 B7 3B 79 17  ...0,..T...r.;y.

0040: 74 8B 8B 6E A0 0B D2 2E   71 C8 19 40 86 BE E0 13  t..n....q..@....

0050: 97 95 84 8C 9E 32 0E C4   4C 13 CB C8 63 B6 04 5D  .....2..L...c..]

0060: 77 63 6E 0B 96 C3 BE 31   7F 63 5B B7 F1 32 B6 8F  wcn....1.c[..2..

0070: 37 AD 3C 41 E2 57 91 9C   34 13 47 06 C2 BC 36 01  7.<A.W..4.G...6.

]

publicKeySun RSA public key, 1024 bits

modulus: 156710490549361998307157784248321138052815240593647721920612234707636672474518428785627598279270666973708373499238042897073583063141391196686695714613451965982321202029488267174706489666542331075371055156322429926478110953568954643597391019270758359531772459952225396781945510442536454329769308555261743601031

public exponent: 65537

privateKey = Sun RSA private CRT key, 512 bits

modulus:          9436695505325102895578204061434242302275442710877846689834639378436107179909413564167457388512745989064904472628923233814244489684454717710931439837606787

public exponent:  65537

private exponent: 6502028201450483640580578644096077717953978532010925814824800887630817031844312415339134097873578467521500399028479795573481660222809945500550764663220193

prime p:          99713031281870580620598436771329694073758123055465722546049677182167437423991

prime q:          94638538052757449781692648760044096832814284083266897015953925555555867862357

prime exponent p: 20829018695527843030593292329516204767837232473706848675640021371498118899773

prime exponent q: 42772685614579179128335692147527444774523690351196507157980305399325034806033

crt coefficient:  63491303759104876081808156169222794756646094193349851654383050149493652264437

Encrypted String SBDGLh7W042fftr9HtcpS4OH4NQAQ4FqULzIxX6JeBUoJeXIvoPEcZn1x9lhkyOChXVcchECANeH

8RZfnXldXmNf6cnx0cTvHD+VtlaTpeyyaXz7tHcSw2cyXDhhRhoHQF/2GJtj3aGclBY6Igl6m9T7

t8ncvLwSCfqLbK1a61o=

Exception:javax.crypto.IllegalBlockSizeException: Data must not be longer than 64 bytes

javax.crypto.IllegalBlockSizeException: Data must not be longer than 64 bytes

at com.sun.crypto.provider.RSACipher.a(DashoA13*..)

at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)

at javax.crypto.Cipher.doFinal(DashoA13*..)

at mypackage.MyEncClass.decryptStrToStr(MyEncClass.java:134)

at mypackage.MyEncClass.main(MyEncClass.java:50)

Decrypted String null

Process exited with exit code 0.