Related Question Here:
http://www.experts-exchange.com/Programming/Languages/Java/Q_24644152.htmlI 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("abcdefg
habcdefgh"
, 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("abcdefg
habcdefgh"
, 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/CF
B/NoPaddin
g");
cipher.init(Cipher.ENCRYPT
_MODE, keyspec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(plaintext.g
etBytes())
;
String st = new String(encrypted);
System.out.println("Osterm
iller Encrypted 64: " + encrypted);
BASE64Encoder base64 = new BASE64Encoder();
String encodedString = base64.encodeBuffer(encryp
ted);
System.out.println("Osterm
iller 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++)