bouncycastle - BOUNCY CAStLE AES 256 decryption problem in java -
really buggy question , hee code:
import java.io.ioexception; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; public class encryption { public static final int = 0x9f224; public static final int b = 0x98c36; public static final int c = 0x776a2; public static final int d = 0x87667; private string premaster; ivparameterspec ivspec = new ivparameterspec(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }); private byte[] text; private secretkey secret; private byte[] skey; protected secretkey passwordkey; protected pbeparameterspec paramspec; public static final string encyt_algorithm = "aes/cbc/pkcs7padding"; public static final string key_algorithm = "pbewithsha256and256bitaes-cbc-bc" ; //bencyt_algorithmse64encoder encod = new bencyt_algorithmse64encoder(); //bencyt_algorithmse64decoder decod = new bencyt_algorithmse64decoder(); public encryption(string premaster,string text,int x){ this.premaster=premaster; this.text=encoder.decode(text.tochararray()); try { keygenerator kg = keygenerator.getinstance("aes"); kg.init(256); secret = kg.generatekey(); } catch (exception e) { // todo encyt_algorithmuto-generated catch block e.printstacktrace(); } } public encryption(string key,string text){ try { this.text = encoder.decode(text.tochararray()); this.skey = encoder.decode(key.tochararray()); } catch (exception e) { e.printstacktrace(); } } public string premaster() { byte[] keys = null; keys = premaster.getbytes(); int x = -1; int process = 0; while (x < keys.length - 2) { x++; switch (x) { case 1: process = keys[x + 1] | ^ c & (d | keys[x] % a); case 2: process += | (keys[x] ^ c) & d; case 3: process += keys[x] ^ (keys[x + 1] / a) % d ^ b; default: process += keys[x + 1] / (keys[x] ^ c | d); } } byte[] xs = new byte[] { (byte) (process >>> 24), (byte) (process >> 16 & 0xff), (byte) (process >> 8 & 0xff), (byte) (process & 0xff) }; premaster = new string(xs); keygenerators key = new keygenerators(premaster); string tomaster = key.calculatesecurityhash("md5") + key.calculatesecurityhash("md2") + key.calculatesecurityhash("sha-512"); return tomaster; } public string keywrapper(){ security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider()); key sharedkey = secret; string key = null; char[] premaster = this.premaster().tochararray(); try { byte[]salt={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; paramspec = new pbeparameterspec(salt,20); pbekeyspec keyspec = new pbekeyspec(premaster); secretkeyfactory factory = secretkeyfactory.getinstance(key_algorithm); passwordkey = factory.generatesecret(keyspec); cipher c = cipher.getinstance(key_algorithm); c.init(cipher.wrap_mode, passwordkey, paramspec); byte[] wrappedkey = c.wrap(sharedkey); key=encoder.encode(wrappedkey); }catch(exception e){ e.printstacktrace(); } return key; } public key keyunwrapper(){ security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider()); byte[] wrappedkey = skey; key unwrapped = null; try{ cipher c = cipher.getinstance(key_algorithm,"bc"); c.init(cipher.unwrap_mode, passwordkey, paramspec); unwrapped = c.unwrap(wrappedkey, encyt_algorithm, cipher.secret_key); }catch(exception e){ e.printstacktrace(); } return unwrapped; } public string encrypt(){ security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider()); secretkey key = secret; string result=null; try{ cipher cipher = cipher.getinstance(encyt_algorithm); cipher.init(cipher.encrypt_mode, key); result =encoder.encode(cipher.dofinal(text)); }catch(exception e){ e.printstacktrace(); } return result; } public string decrypt(){ security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider()); string result = null; secretkey key = (secretkey) keyunwrapper(); try{ cipher cipher = cipher.getinstance(encyt_algorithm, "bc"); cipher.init(cipher.decrypt_mode, key); result = encoder.encode(cipher.dofinal(text)); }catch(exception e){ e.printstacktrace(); } return result; } public static void main(string[] args) throws ioexception{ encryption en = new encryption("123456","hello world",0); string entext = en.encrypt(); string key = en.keywrapper(); system.out.println(key); system.out.println(entext); encryption de = new encryption(key,entext); string plaintext = de.decrypt(); system.out.println(plaintext); } and , result ... tried combinations can, none of them works ..
f63de3ee8ceecf4df76836ca6d69a3903bd87b5726656c54c1c8ec30b6653b2c0e5c7672be3cf4be7b2dc7ac5d07dea0 f1c8d92e5f74019c569d54d70045add6 java.lang.nullpointerexception @ org.bouncycastle.jce.provider.jceblockcipher.engineinit(unknown source) @ javax.crypto.cipher.init(dashoa13*..) @ javax.crypto.cipher.init(dashoa13*..) @ fiador.authentication.util.encryption.keyunwrapper(encryption.java:114) @ fiador.authentication.util.encryption.decrypt(encryption.java:142) @ fiador.authentication.util.encryption.main(encryption.java:160) java.lang.nullpointerexception @ org.bouncycastle.jce.provider.jceblockcipher.engineinit(unknown source) @ javax.crypto.cipher.a(dashoa13*..) @ javax.crypto.cipher.a(dashoa13*..) @ javax.crypto.cipher.init(dashoa13*..) @ javax.crypto.cipher.init(dashoa13*..) @ fiador.authentication.util.encryption.decrypt(encryption.java:145) @ fiador.authentication.util.encryption.main(encryption.java:160) null
the first nullpointerexception occurs inside method call (in keyunwrapper method):
c.init(cipher.unwrap_mode, passwordkey, paramspec); have look: 1 of these arguments null?
looking @ code, seems passwordkey assigned in keywrapper, not called on instance of class.
Comments
Post a Comment