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

Posted on 2007-10-06
Last Modified: 2011-10-03
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
Question by:Mobinex
    LVL 86

    Expert Comment

    LVL 92

    Expert Comment

    LVL 92

    Expert Comment

    LVL 1

    Author Comment

    Thank for all help,
    I will try to work with
    For 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 to decrypt with DES, DES-EDE2 but it has problem with block invalide (javax.crypto.BadPaddingException)

    Many thank again.
    LVL 13

    Accepted Solution

    import javax.crypto.Cipher;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;

    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 =; i != -1; i = {
    LVL 1

    Author Comment

    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";

    LVL 1

    Author Comment


    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Suggested Solutions

    Title # Comments Views Activity
    Annotation to do Dependency injection in spring 3 30
    withoutTen challenge 14 68
    java JDK Download 7 49
    mergeTwo  challenge 13 53
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now