[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 632
  • Last Modified:

How to work with JAVA crypto to encrypt a file/ a string

Hi Experts,
I am work on crypto to encrypt file and string,
I know java has supported crypto API but I don't know details about this and how to use
Can you help me to give some suggestion/examble that is using JAVA crypto to encrypt/decrypt a file and a string with DES-EDE2 or others

Thanks in advance
0
Mobinex
Asked:
Mobinex
1 Solution
 
CEHJCommented:
0
 
objectsCommented:
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
MobinexAuthor Commented:
Thank for all help,
I will try to work with http://www.exampledepot.com/egs/javax.crypto
For http://www.devx.com/Java/10MinuteSolution/21385/1954?pf=true I knew this, but it can not descrypt my string that I encrypt from other crypto library.

Can you all help again with below question:
String encryptedString = "880BF4E4946B940423B478A1F46FF79A89A20737EEDA652562C4FC43CAF0B1CF0A2818A5649969E3BA5C0A447B2C8E6A7CD5159CD2FB738275FB09A23047045EEC51F237F423B80BB6BD9DEC4B1957C9947DCB19FD7C8D90B815F3162F728F618CEF619196C8BEF3CEE459FEB5F8816596F35645EFE40B4CD9B1DFEFF529E81DDD4E8C6CD253F95800B97F001F740CC486E7918B2AD08D14F9F9CC312CCCB1586173BF920D9476A6156F7AC03B03D3BC8C1548FACEB9B81C96A5600E6A8A0A987BAAF1033B63453FE8BDE2A5077E9459FBE55011E307889A5A83802C48925169C2E70529EC81FD42B0E5E62F95E8D966A5FE46A05C2A0D4B564F47478864C7B743E136A64C000630CC4A74658FD1DB560755C53C204EAAB56149EC3A90C592D290DD41D7E316EC13713E1D80601ABF6CA0B79BA4DC113013C1C502AA3166C20D98EF4F63855FD12D03CFAFE48C07164D7CCC63A681E64D581EC975A2C5324E2C1ED17161F429529CC4735F2CA46E148AD6BAB19056678433391EAE57165C534995802536F34F23B2EDEAD012D900D919C000CA73CC5912AC183B24B8D1B4AD99E8BCB42FCCE59296D94CB6C3203AA9F31D81955D1D5E2C03A6D8DA9399761079459C3B4349CAE811C0761ED3A756600C342B0E705FF1A8A2E6601A656E135569";

Can you know what Encryption Scheme/Crypto Algorithm Name? (this string is encrypted from crypto C++  library version 5.0 using DefaultCrypto)

I used http://www.devx.com/Java/10MinuteSolution/21385/1954?pf=true to decrypt with DES, DES-EDE2 but it has problem with block invalide (javax.crypto.BadPaddingException)

Many thank again.
0
 
Bart CremersJava ArchitectCommented:
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Test {

    public static void main(String[] args) throws Exception {
        String testData;
        if (args.length > 0) {
            testData = args[0];
        } else {
            testData = "Test Data";
        }

        KeyGenerator kg = KeyGenerator.getInstance("DESede");
        SecretKey sKey = kg.generateKey();
        System.out.println("Key generation done.");

        Cipher myCipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        myCipher.init(Cipher.ENCRYPT_MODE, sKey);

        byte[] testBytes = testData.getBytes();

        byte[] encryptedText = myCipher.doFinal(testBytes);

        System.out.println("encryptedText = " + new String(encryptedText));

        myCipher.init(Cipher.DECRYPT_MODE, sKey);
        byte[] decryptedText = myCipher.doFinal(encryptedText);

        System.out.println("decryptedText = " + new String(decryptedText));

        // Using streams
        FileInputStream inputFile = new FileInputStream("in.txt");
        FileOutputStream outputFile = new FileOutputStream("out.txt");
        CipherOutputStream cipherOutputStream = new CipherOutputStream(outputFile, myCipher);

        for (int i = inputFile.read(); i != -1; i = inputFile.read()) {
            cipherOutputStream.write(i);
        }
        cipherOutputStream.close();
        outputFile.close();
        inputFile.close();
    }
}
0
 
MobinexAuthor Commented:
Can you help to write java program that use crypto encrypt/descrypt library to descrypt below string:

String encrypted ="36102D92CF26C8688140F2443DBE3320998197DB0C141FC38008FBDD69D70BB4049490F1026F2002BA3612D34D9F59A9A7CCA487654EF12EDAB1841D523271CB71B3EBB837BE405BFE1CFADE6C3388AF90CD58BBB55CAFC06CCE5DDB5AC364B5DEAE42EE711728CE67023D3D9E033CBCF3C955BA305922972B7DC6B22234E5D2C3A7CD630CC4784454E863E19F2BBA0CBBCC35DB2F250D0BAFB91B2DE717213546C7737A322374E65D6FA1FFF12A4D149DEACDD64D7650268A0680FB0A0257F76A899CE0C452DCD3A224A97FB7690B7E68D5FFDF5768B04EA970B4EAF5F4589571E79A23EA2B7B4339BC41A0177B31A8D061875D7A987AD95EF1DC8866346196E8E5E8AEBCBDF3AD";

with key is: String key ="TEST";


0
 
MobinexAuthor Commented:

Hi Experts
Do you know how to use crypto for JAVA and C++, I mean I need to use JAVA program to encrypt and C++ to descypt.

As I know crypto in C++ we can use crypto++ library, and for Java we have crypto library too.
But I don't know how to use compatible between C++ and JAVA.
Can you let me know some suggestion?

Thank in advance!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now