How the original rfc2898derivebytes decryption is implemented through Java
This looking for a half-day, or do not understand, the password is not understand, directly up to ask the rfc2898derivebytes corresponds to the pbkdf2withhmacsha1,rfc2898derivebytes default number of iterations is 1000, the remaining do not know
The following is the C # code Public byte[] Decryptdata (byte[] data) {aesmanaged Managed=NULL; MemoryStream Stream=NULL; CryptoStream stream2=NULL; Try{rfc2898derivebytes bytes=NewRfc2898derivebytes ( This. Fpassword, This. Salt); Managed=Newaesmanaged (); Managed.set_key (bytes. GetBytes (Managed.get_keysize ()/8)); Managed.set_iv (bytes. GetBytes (Managed.get_blocksize ()/8)); Stream=NewMemoryStream (); Stream2=NewCryptoStream (Stream, managed. CreateDecryptor (),1); Stream2. Write (data,0, data. Length); Stream2. FlushFinalBlock (); returnStream. ToArray (); } Catch { returndata; The following code cannot be implemented to decrypt Java code Public byte[] DEPBE (byte[] data) { Try { Char[] Password = This. Fpassword.tochararray (); byte[] Salt = This. Fsalt; KeySpec Spec=NewPbekeyspec (Password,salt, +, -); Secretkeyfactory Factory= Secretkeyfactory.getinstance ("PBKDF2WITHHMACSHA1"); Secretkey tmp=Factory.generatesecret (spec); Secretkey Secret=NewSecretkeyspec (tmp.getencoded (),"AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding"); Cipher.init (Cipher.decrypt_mode, Secret,NewIvparameterspec (New byte[Cipher.getblocksize ()])); byte[] ciphertext =cipher.dofinal (data); returnciphertext; }Catch(Exception e) {e.printstacktrace (); return NULL; } }
Java Solutions:
Find a Rfc2898derivebytes class that is re-engraved
Http://www.itstrike.cn/Question/fc038543-6a4e-4796-8333-6b1c1b5cce12.html
Import Java.io.unsupportedencodingexception;import Java.security.invalidkeyexception;import Java.security.nosuchalgorithmexception;import Javax.crypto.mac;import Javax.crypto.spec.SecretKeySpec; /** * RFC 2898 Password derivation compatible with. NET Rfc2898derivebytes class. */Public class Rfc2898derivebytes {private Mac _hmacsha1; Privatebyte[] _salt; Privateint_iterationcount; Privatebyte[] _buffer =New byte[20]; Privateint_bufferstartindex = 0; Privateint_bufferendindex = 0; Privateint_block = 1; /** * Creates new instance. * @param password The password used to derive the key. * @param salt The key salt used to derive the key. * @param iterations the number of iterations for the operation. * @throws nosuchalgorithmexception HmacSHA1 algorithm cannot be found. * @throws InvalidKeyException Salt must is 8 bytes or more. -or-password cannot be null. */Public rfc2898derivebytes (byte[] Password,byte[] Salt,intiterations) throws NoSuchAlgorithmException, InvalidKeyException {if(Salt = =NULL) || (Salt.length < 8)) {Throw NewInvalidKeyException ("Salt must is 8 bytes or more."); } if(Password = =NULL) {Throw NewInvalidKeyException ("Password cannot is null.")); } This. _salt =Salt; This. _iterationcount =iterations; This. _HMACSHA1 = Mac.getinstance ("HmacSHA1"); This. _hmacsha1.init (NewSecretkeyspec (password, "HmacSHA1")); } /** * Creates new instance. * @param password The password used to derive the key. * @param salt The key salt used to derive the key. * @param iterations the number of iterations for the operation. * @throws nosuchalgorithmexception HmacSHA1 algorithm cannot be found. * @throws InvalidKeyException Salt must is 8 bytes or more. -or-password cannot be null. * @throws unsupportedencodingexception UTF-8 encoding is not supported. */Public rfc2898derivebytes (String password,byte[] Salt,intiterations) throws InvalidKeyException, NoSuchAlgorithmException, unsupportedencodingexception { This(Password.getbytes ("UTF8"), salt, iterations); } /** * Creates new instance. * @param password The password used to derive the key. * @param salt The key salt used to derive the key. * @throws nosuchalgorithmexception HmacSHA1 algorithm cannot be found. * @throws InvalidKeyException Salt must is 8 bytes or more. -or-password cannot be null. * @throws unsupportedencodingexception UTF-8 encoding is not supported. */Public rfc2898derivebytes (String password,byte[] salt) throws NoSuchAlgorithmException, InvalidKeyException, unsupportedencodingexception { This(password, salt, 0x3e8); } /** * Returns a pseudo-random key from a password, salt and iteration count. * @param count number of bytes to return. * @return Byte Array. */ Publicbyte[] GetBytes (intcount) { byte[] result =New byte[Count]; intResultoffset = 0; intBufferCount = This. _bufferendindex- This. _bufferstartindex; if(BufferCount > 0) {//If there is some data in buffer if(Count < BufferCount) {//If there is enough data in bufferSystem.arraycopy ( This. _buffer, This. _bufferstartindex, result, 0, Count); This. _bufferstartindex + =count; returnresult; } system.arraycopy ( This. _buffer, This. _bufferstartindex, result, 0, BufferCount); This. _bufferstartindex = This. _bufferendindex = 0; Resultoffset+=BufferCount; } while(Resultoffset <count) { intNeedcount = count-Resultoffset; This. _buffer = This. Func (); if(Needcount > 20) {//we one (or more) additional passesSystem.arraycopy ( This. _buffer, 0, result, Resultoffset, 20); Resultoffset+ = 20; } Else{system.arraycopy ( This. _buffer, 0, result, Resultoffset, needcount); This. _bufferstartindex =Needcount; This. _bufferendindex = 20; returnresult; } } returnresult; } Privatebyte[] func () { This. _hmacsha1.update ( This. _salt, 0, This. _salt.length); byte[] Temphash = This. _hmacsha1.dofinal (Getbytesfromint ( This. _block)); This. _hmacsha1.reset (); byte[] Finalhash =Temphash; for(inti = 2; I <= This. _iterationcount; i++) {Temphash= This. _hmacsha1.dofinal (Temphash); for(intj = 0; J < 20; J + +) {Finalhash[j]= (byte) (Finalhash[j] ^Temphash[j]); } } if( This. _block = = 2147483647) { This. _block = 2147483648; } Else { This. _block + = 1; } returnFinalhash; } private staticbyte[] Getbytesfromint (inti) {return New byte[] { (byte) (I >>> 24), (byte) (I >>> 16), (byte) (I >>> 8), (byte) i}; } }
as long as this can solve the Java code Publicbyte[] Derfc2898derivebytes (byte[] data,string fpassword,byte[] fsalt) {rfc2898derivebytes Keygenerator=NULL; Try{keygenerator=NewRfc2898derivebytes (Fpassword, Fsalt, 1000); } Catch(invalidkeyexception E1) {E1.printstacktrace (); } Catch(nosuchalgorithmexception E1) {E1.printstacktrace (); } Catch(unsupportedencodingexception E1) {E1.printstacktrace (); } //log.i ("key =", DecodeUTF8 (Keygenerator.getbytes )); byte[] Bkey = Keygenerator.getbytes (32); byte[] BIv = Keygenerator.getbytes (16); Try{secretkeyspec Sekey=NewSecretkeyspec (Bkey, "AES"); Algorithmparameterspec param=NewIvparameterspec (BIV); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding"); Cipher.init (Cipher.decrypt_mode,sekey,param); byte[] decrypted =cipher.dofinal (data); returndecrypted; } Catch(Exception e) {e.printstacktrace (); return NULL; }}
How Rfc2898derivebytes decryption is implemented through Java