Crypto++ .net java compatibility

371 views
Skip to first unread message

Setori

unread,
Sep 12, 2007, 5:24:44 AM9/12/07
to Crypto++ Users
Hi All

I am rather new to crypto++ hence this post.

problem:

We have a contactless memory card project. Single sign on. We wish to
encrypt user details (standard 2.0 .net stuff) then gina.dll will
decrypt the ciphertext with crypto++ then encrypt it again and send it
off to another backend server written in java. which will then decrypt
it using java (Java Cryptography Extension).

I have managed to get .net and java to get the same output. except
crypto++.

My question: is it possible for crypto++ to decrypt .net aes
encryption then encrypt it again first with aes then RSA (or ECC) to
send off to java server.... then will java server be able to decrypt
using crypto++?

(alternative solution: is it better simply to just write a crypto+
+ .net wrapper for windows administration program. keep crypto++
untouched in gina.dll and use Java's JNI to talk with a the crypto++
library on the server thus keeping everything rather standard.)

Look forward to your response

Stewart

some examples of what I am doing.

here is the out put of each example

Java
INTEGRATED encrypted is rJnW+7kr4mukfoNkv2QvJw==
rJnW+7kr4mukfoNkv2QvJw== decrypted is INTEGRATED

Crypto++
INTEGRATED encrypted is 瑱蛀?鈑僤縟/'
瑱蛀?鈑僤縟/'decrypted is INTEGRATED

.NET
INTEGRATED encrypted is rJnW+7kr4mukfoNkv2QvJw==
rJnW+7kr4mukfoNkv2QvJw== decrypted is INTEGRATED


.NET_________________________________________

using System;
using System.Security.Cryptography;
using System.Text;


namespace aes1
{
class CryptTest
{
static void Main(string[] args)
{
try
{
byte[] key = new byte[16] { 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01 };
byte[] iv = new byte[16] { 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01 };

string text="INTEGRATED";
System.Console.WriteLine(".NET");
string encrypted = encrypt(text,iv,key);
System.Console.WriteLine( text + " encrypted is " + encrypted);
string decrypted = decrypt(encrypted,iv,key);
System.Console.WriteLine( encrypted + " decrypted is " + decrypted);
Console.ReadLine();

}
catch (Exception e)
{
System.Console.WriteLine(e.StackTrace);
}

}

public static string encrypt(string text,byte[] iv, byte[] key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();

rijndaelCipher.Mode = CipherMode.CBC;

rijndaelCipher.Padding = PaddingMode.PKCS7;

rijndaelCipher.KeySize = 128;

rijndaelCipher.BlockSize = 128;

rijndaelCipher.Key = key;

rijndaelCipher.IV = iv;

ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

byte [] plainText = Encoding.UTF8.GetBytes(text);

byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0,
plainText.Length);

return Convert.ToBase64String(cipherBytes);

}

public static string decrypt(string text,byte[] iv, byte[] key)

{
RijndaelManaged rijndaelCipher = new RijndaelManaged();

rijndaelCipher.Mode = CipherMode.CBC;

rijndaelCipher.Padding = PaddingMode.PKCS7;

rijndaelCipher.KeySize = 128;

rijndaelCipher.BlockSize = 128;

byte[] encryptedData = Convert.FromBase64String(text);

rijndaelCipher.Key = key;

rijndaelCipher.IV = iv;

ICryptoTransform transform = rijndaelCipher.CreateDecryptor();

byte[] plainText = transform.TransformFinalBlock(encryptedData, 0,
encryptedData.Length);

return Encoding.UTF8.GetString(plainText);

}


}
}
_________________________________________

Java
_________________________________________

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class aes1 {
public static void main(String[] args) {
try{

byte[] key = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
byte[] iv = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};

String text = "INTEGRATED";
String encrypted = encrypt(text, iv, key);
System.out.println("Java");
System.out.println(text + " encrypted is " + encrypted );
String decrypted = decrypt(encrypted,iv,key);
System.out.println(encrypted + " decrypted is " + decrypted );
}catch (Exception e){
e.printStackTrace();
}
}

public static String encrypt(String text, byte[] iv, byte[] key)
throws Exception{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv);

cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);
byte [] results = cipher.doFinal(text.getBytes("UTF-8"));
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(results);
}

public static String decrypt(String text, byte[] iv, byte[] key)
throws Exception{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);

BASE64Decoder decoder = new BASE64Decoder();
byte [] results = cipher.doFinal(decoder.decodeBuffer(text));
return new String(results,"UTF-8");


}

}
_________________________________________________
C++ Crypto++
_________________________________________________
#include "StdAfx.h"

// Runtime Includes
#include <iostream>
#include <iomanip>

// Crypto++ Includes
#include "cryptlib.h"
#include "aes.h" // AES
#include "modes.h" // CBC_Mode< >
#include "filters.h" // StringSource

int main(int argc, char* argv[]) {


byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ] =
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };

byte iv[ CryptoPP::AES::BLOCKSIZE ] =
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };

std::string PlainText = "INTEGRATED";
std::cout << "\n" << "Crypto++" << std::endl;
std::cout << PlainText << " encrypted is " ;

// Cipher Text Sink
std::string CipherText;

// Encryption
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption
Encryptor( key, sizeof(key), iv );

CryptoPP::StringSource( PlainText, true,
new CryptoPP::StreamTransformationFilter( Encryptor,
new CryptoPP::StringSink( CipherText )
) // StreamTransformationFilter
); // StringSource

// Debug
std::cout << CipherText << std::endl;


///////////////////////////////////////
// DMZ //
///////////////////////////////////////

// Recovered Text Sink
std::string RecoveredText;

// Decryption
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption
Decryptor( key, sizeof(key), iv );

CryptoPP::StringSource( CipherText, true,
new CryptoPP::StreamTransformationFilter( Decryptor,
new CryptoPP::StringSink( RecoveredText )
) // StreamTransformationFilter
); // StringSink

// Debug
std::cout << CipherText << "decrypted is " << RecoveredText <<
std::endl;
std::cout << std::endl;

return 0;
}

Setori

unread,
Sep 12, 2007, 5:29:36 AM9/12/07
to Crypto++ Users
darn noticed something incorrect

"My question: is it possible for crypto++ to decrypt .net aes
encryption then encrypt it again first with aes then RSA (or ECC) to
send off to java server.... then will java server be able to decrypt
using crypto++? "

should be

My question: is it possible for crypto++ to decrypt .net aes
encryption then encrypt it again first with aes then RSA (or ECC) to
send off to java server.... then will java server be able to decrypt

the crypto++ ciphertext?

Floor Slob

unread,
May 15, 2013, 9:15:24 AM5/15/13
to cryptop...@googlegroups.com, seto...@gmail.com
Dear Setori,

I am also new to crypto++. 
For my current project, I need encryption between C++ and Java. 
I was trying to fix your c++ code. 
Since this post is a few years old, I was wondering if you already managed to fix the code?
I will be happy to hear from you.

Greetings,
Florian

Op woensdag 12 september 2007 11:24:44 UTC+2 schreef Setori het volgende:
Reply all
Reply to author
Forward
0 new messages