Encrypting A String With Crypto++ (C++) and Decrypting With AESCrypt (Android/java) -


i have been working on way long. @ rate wont have hair left. have situation trying encrypt c++ , decrypt java thought easy standard aes, or thought. here code

the encrypted c++ not decrypt correctly in java code.

c++ using crypto++

byte key[ cryptopp::aes::max_keylength ], iv[ cryptopp::aes::blocksize ]; memset( key, 0x00, cryptopp::aes::max_keylength ); memset( iv, 0x00, cryptopp::aes::blocksize );  string plaintext = "this test string"; string ciphertext; string decryptedtext;  cryptopp::aes::encryption aesencryption(key, cryptopp::aes::max_keylength); cryptopp::cbc_mode_externalcipher::encryption cbcencryption( aesencryption, iv );  cryptopp::streamtransformationfilter stfencryptor(cbcencryption, new cryptopp::stringsink( ciphertext ) ); stfencryptor.put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 ); stfencryptor.messageend();  std::cout << "cipher text (" << ciphertext() << " bytes)" << std::endl; 

my android/java code - mainactivity.java

            string sencryptedmsg = "paste encrypted string here crypto++";             try {                 string messageafterdecrypt = aescrypt.decrypt(password, sencryptedmsg);                 log.i("decrypted --> ", messageafterdecrypt);             }catch (generalsecurityexception e){              } 

android/java code - aescrypt.java

import android.util.base64; import android.util.log;  import java.io.unsupportedencodingexception; import java.security.generalsecurityexception; import java.security.messagedigest; import java.security.nosuchalgorithmexception;  import javax.crypto.cipher; import javax.crypto.spec.ivparameterspec; import javax.crypto.spec.secretkeyspec;   public final class aescrypt {       private static final string tag = "aescrypt";      //aescrypt-objc uses cbc , pkcs7padding     private static final string aes_mode = "aes/cbc/pkcs7padding";     private static final string charset = "utf-8";      //aescrypt-objc uses sha-256 (and 256-bit key)     private static final string hash_algorithm = "sha-256";      //aescrypt-objc uses blank iv (not best security, aim here compatibility)     private static final byte[] ivbytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};      //togglable log option (please turn off in live!)     public static boolean debug_log_enabled = false;       /**      * generates sha256 hash of password used key      *      * @param password used generated key      * @return sha256 of password      */     private static secretkeyspec generatekey(final string password) throws nosuchalgorithmexception, unsupportedencodingexception {         final messagedigest digest = messagedigest.getinstance(hash_algorithm);         byte[] bytes = password.getbytes("utf-8");         digest.update(bytes, 0, bytes.length);         byte[] key = digest.digest();          log("sha-256 key ", key);          secretkeyspec secretkeyspec = new secretkeyspec(key, "aes");         return secretkeyspec;     }       /**      * encrypt , encode message using 256-bit aes key generated password.      *      *      * @param password used generated key      * @param message thing want encrypt assumed string utf-8      * @return base64 encoded ciphertext      * @throws generalsecurityexception if problems occur during encryption      */      /**      * decrypt , decode ciphertext using 256-bit aes key generated password      *      * @param password used generated key      * @param base64encodedciphertext encrpyted message encoded base64      * @return message in plain text (string utf-8)      * @throws generalsecurityexception if there's issue decrypting      */     public static string decrypt(final string password, string base64encodedciphertext)             throws generalsecurityexception {          try {             final secretkeyspec key = generatekey(password);              log("base64encodedciphertext", base64encodedciphertext);             byte[] decodedciphertext = base64.decode(base64encodedciphertext, base64.no_wrap);             log("decodedciphertext", decodedciphertext);              byte[] decryptedbytes = decrypt(key, ivbytes, decodedciphertext);              log("decryptedbytes", decryptedbytes);             string message = new string(decryptedbytes, charset);             log("message", message);               return message;         } catch (unsupportedencodingexception e) {             if (debug_log_enabled)                 log.e(tag, "unsupportedencodingexception ", e);              throw new generalsecurityexception(e);         }     }       /**      * more flexible aes decrypt doesn't encode      *      * @param key aes key typically 128, 192 or 256 bit      * @param iv initiation vector      * @param decodedciphertext in bytes (assumed it's been decoded)      * @return decrypted message cipher text (not encoded)      * @throws generalsecurityexception if goes wrong during encryption      */     public static byte[] decrypt(final secretkeyspec key, final byte[] iv, final byte[] decodedciphertext)             throws generalsecurityexception {         final cipher cipher = cipher.getinstance(aes_mode);         ivparameterspec ivspec = new ivparameterspec(iv);         cipher.init(cipher.decrypt_mode, key, ivspec);         byte[] decryptedbytes = cipher.dofinal(decodedciphertext);          log("decryptedbytes", decryptedbytes);          return decryptedbytes;     }         private static void log(string what, byte[] bytes) {         if (debug_log_enabled)             log.d(tag, + "[" + bytes.length + "] [" + bytestohex(bytes) + "]");     }      private static void log(string what, string value) {         if (debug_log_enabled)             log.d(tag, + "[" + value.length() + "] [" + value + "]");     }       /**      * converts byte array hexidecimal useful logging , fault finding      * @param bytes      * @return      */     private static string bytestohex(byte[] bytes) {         final char[] hexarray = {'0', '1', '2', '3', '4', '5', '6', '7', '8',                 '9', 'a', 'b', 'c', 'd', 'e', 'f'};         char[] hexchars = new char[bytes.length * 2];         int v;         (int j = 0; j < bytes.length; j++) {             v = bytes[j] & 0xff;             hexchars[j * 2] = hexarray[v >>> 4];             hexchars[j * 2 + 1] = hexarray[v & 0x0f];         }         return new string(hexchars);     }      private aescrypt() {     } 

thanks in advance can provide!


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -