Question

C++/Java AES Encryption: CFB IV key issues?

Asked by: Valleriani

Related Question Here: http://www.experts-exchange.com/Programming/Languages/Java/Q_24644152.html

I have finished with Java and C++ encryption using ECB, one of the more simple encryptions of AES.

However, CBC and CFB require a IvParameterSpec/Chain to function properly.

I am not sure how to go about this, for C++, I've done, when making the key:

      oRijndael.MakeKey("abcdefghabcdefgh", CRijndael::sm_chain0, 16, 16);

   sm_chain0 being a simple: \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 for use.  I also have to reset the chain using     oRijndael.ResetChain(); every time I need to encrypt/decrypt.


C++ sample:"
      CRijndael oRijndael;
      oRijndael.MakeKey("abcdefghabcdefgh", CRijndael::sm_chain0, 16, 16);
      char szDataIn_Orig[] = "aaaaaaaaaaaaaaaa";

        //Test ECB
      int nLen = strlen(szDataIn_Orig);
      char *szDataIn = new char[nLen + 1];
      memset((char *)szDataIn, 0, nLen + 1);
      strcpy(szDataIn, szDataIn_Orig);

      // To encrypt
      char szDataOut[17] ="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    oRijndael.ResetChain();
      oRijndael.Encrypt(szDataIn, szDataOut, nLen, CRijndael::CFB);

      std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(szDataOut), nLen);
"


java sample:

import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.io.*;
import java.util.prefs.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

class test2 {

public static void main(String[] args) throws Exception {


String plaintext = "abcdefghabcdefgh";
String key = "abcdefghabcdefgh";

            byte iv[16];

for (int i =0;i<iv.length;i++)
{
iv[i]=0;
}

IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");


Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
      cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(plaintext.getBytes());
String st = new String(encrypted);
System.out.println("Ostermiller Encrypted 64: " + encrypted);

BASE64Encoder base64 = new BASE64Encoder();
String encodedString = base64.encodeBuffer(encrypted);
System.out.println("Ostermiller Encrypted 64: " + encodedString);


         }
    }


Would be example coding for the encoded. Using  CRijndael::ECB for C++ and AES/ECB/NoPadding for java works great. However, swappingt o CFB or CBC that eneds the IV key does not work well.

Any suggestions on how I can get the 'keys' synced up? I am maybe thinking the java side key isn't working properly, as I'm not to well (just started a couple days ago) with java.

The general attached files like before are still at http://stellarfrontier.net/encrypt.zip (Contains 4 files for C++)

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-12 at 03:23:08ID24645910
Topics

Encryption for Network Security

,

C++ Programming Language

,

Java Programming Language

Participating Experts
1
Points
500
Comments
9

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. AES_DECRYPT query
    I have a silly question. I'm implementing additional security to some of my code libraries by utilizing MySQL's AES_ENCRYPT and AES_DECRYPT functions in place of the MD5 and SHA1 features. I have been able to use an INSERT query to put a sample credit card into the database....
  2. I need to change an AES file encryption program to encrypt …
    /* ------------------------------------------------------------------------- Copyright (c) 2001, Dr Brian Gladman < >, Worcester, UK. All rights reserved. LICENSE TERMS The free distribution and use of this software in both source and binary for...
  3. Encrypting file using AES encryption in JAVA
    I have a program to encrypt and decrypt a file using AES encryption standard. It works fine within the same class. Now I want to implement the same in different classes in different machines. File will be encrypted in one machine and will be decrypted in another machine. An...
  4. Encryption/Decryption of file in C++ (unsigned char or b…
    I need an example code which have encrypt and decrypt function which takes unsigned char as input, not char! All codes I found in internet uses char and because of that, I cannot encrypt files, because files have unprintable chars and char 0, so I need unsigned char array as...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: VallerianiPosted on 2009-08-12 at 03:24:31ID: 25077245

Oh ignore the comment of //test ECB. I still haven't changed much from the last coding for comments and junk. Just been trying to get it to work first!

 

by: objectsPosted on 2009-08-12 at 03:31:14ID: 25077279

 

by: VallerianiPosted on 2009-08-12 at 04:38:13ID: 25077600

Hey there, I actually got a similar example but I'm not sure how IV works.

I mean, I have it working java side too, but again like the last question, they aren't matching up with CFB. However, the IV is something that has to be similar i'm guessing too, much like the key.

private static byte[] iv = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6,
                  0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };

Though I see that, which I guss would be
0123456789ABCDEF (Caps)as the IV.

And for C++ side, I thought while using the key       oRijndael.MakeKey("abcdefghabcdefgh", "0123456789ABCDEF", 16, 16);

But apparently is different.

But I think the C++ 'chain' as they say and the IV are 'similar', or so I think.

 

by: objectsPosted on 2009-08-12 at 04:41:37ID: 25077620

> 0123456789ABCDEF (Caps)as the IV.

no, they are values not chars

 

by: VallerianiPosted on 2009-08-12 at 04:49:26ID: 25077682

Ah okay, so I will adjust and give it a go and see how it does ;)

 

by: objectsPosted on 2009-08-12 at 04:57:59ID: 25077730

from memory would be something like:

"\000\001\002...

 

by: VallerianiPosted on 2009-08-12 at 05:07:51ID: 25077799

Heheh, well I declared

      char testval[16] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6,
                  0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };

In C++ and used that, it worked great. My bad, I must be getting tired. :) Works good now though.

 

by: VallerianiPosted on 2009-08-12 at 05:08:36ID: 31614685

Seems like this is solved! Thank you!

 

by: VallerianiPosted on 2009-08-12 at 05:19:44ID: 25077888

Just a quick question on you.

What would you use length wise for a IV and key? Rather, what would be good key/iv length values? These above are just small examples and such.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...