Hallo,
Ich habe folgende Code in C:
in Java convertiert:
Leider das Ergebnis in Java ist nicht gleich wie in C code.
decstr in C code ist nur 4 byte lang
cipherData in java code aber 32 bytes lang ?!
Was habe ich denn falsch gemacht ? Gruss
Ich habe folgende Code in C:
Code:
static unsigned char modulus[] = {
0x00,0xcd,0x40,0x06,0x94,0xef,0xd2,0xd3,0xc0,0x65,0xca,0xfa,0x15,0x66,0x63,
0x50,0x1f,0x8e,0x44,0xbe,0x6b,0x79,0x9a,0x21,0x42,0x86,0xf9,0x89,0x90,0xdd,
0x95,0x9f,0xe3 };
static unsigned char publicExponent[] = { 0x01,0x00,0x01 };
void *
get_public_key (void)
{
RSA *pub;
if ((pub = RSA_new()) != NULL) {
#define BN_INIT(name) BN_bin2bn(name,sizeof(name),NULL)
pub->n = BN_INIT(modulus);
pub->e = BN_INIT(publicExponent);
// RSA_print_fp (stdout, pub, 0);
return ((void *) pub);
}
return (NULL);
}
int
encode (const char *cleartext, int length, char *buf, int bufsize)
{
int size;
unsigned char *decstr, *encstr;
static RSA *pub = NULL;
if ((pub == NULL) && ((pub = get_public_key()) == NULL)) {
return (-ENOMEM);
}
size = RSA_size (pub);
if (length > size) {
return (-ENOSPC);
}
encstr = alloca (size+4);
decstr = alloca (size+4);
memset (decstr, 0, size);
memcpy (decstr, cleartext, length);
// decstr[length] = '\0';
size = RSA_public_encrypt (size, decstr, encstr, pub, RSA_NO_PADDING);
if ((size > 0) && (bufsize > ((4*size)/3) + 4)) {
size = base64_encode (encstr, size, buf);
} else {
return ((size > 0)? -ENOSPC: -EINVAL);
}
return (size);
}
in Java convertiert:
Code:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
String text ="1234";
// openssl rsa -pubin -in pubkey.pem -modulus -noout
BigInteger modulus = new BigInteger("CD400694EFD2D3C065CAFA156663501F8E44BE6B799A214286F98990DD959FE3", 16);
//openssl rsa -pubin -in pubkey.pem -text -noout
BigInteger pubExp = new BigInteger("010001", 16);
KeyFactory keyFactory = keyFactory = KeyFactory.getInstance("RSA","BC");
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
RSAPublicKey key = key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherData = cipher.doFinal(text.getBytes());
Base64 base64 = new Base64();
System.out.println(base64.encodeToString(cipherData));
Leider das Ergebnis in Java ist nicht gleich wie in C code.
decstr in C code ist nur 4 byte lang
cipherData in java code aber 32 bytes lang ?!
Was habe ich denn falsch gemacht ? Gruss