Reputation: 31
I need to multiply between a string and a GT( e(g,g)^n ) type element in the group. I convert the string to element first and then I have a problem when I want to decrypt a message.
Here is the code in simple version:
TypeACurveGenerator pg = new TypeACurveGenerator(160,512);
//generate the parameters of the elliptic curve
PairingParameters typeAParams = pg.generate();
//initialize the pairing
PairingFactory.getInstance().setUsePBCWhenPossible(true);
Pairing p= PairingFactory.getPairing(typeAParams);
String s="hahaha test";
Element g = p.getGT().newElementFromBytes(Base64.encodeBytes(s.getBytes()).getBytes());
byte[] s3 = Base64.decode(g.toString());
System.out.println(s);
System.out.println(g);
System.out.println(s3);
then the compile displays:
Bad Base64 input character decimal 123 in array position 0 (error line: byte[] s3 = Base64.decode(g.toString()); )
I'm confused about this result and I don't have the ability to resolve it.
I then tried another method, but it is always different after decode(compare with original string)
here is a simple version of my code:
TypeACurveGenerator pg = new TypeACurveGenerator(160,512);
//generate the parameters of the elliptic curve
PairingParameters typeAParams = pg.generate();
//initialize the pairing
PairingFactory.getInstance().setUsePBCWhenPossible(true);
Pairing p= PairingFactory.getPairing(typeAParams);
String s="hahaha test";
Element g = p.getGT().newElementFromBytes(Base64.decode(s));
String s3 = Base64.encodeBytes(g.toBytes());
System.out.println(s);
System.out.println(g);
System.out.println(s3);
and the compile display
hahaha test
{x=146958119709534,y=0}
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACFqFqFq14AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
I have no idea about why it is different between original string and after decode...
can anyone know the problem or give me some advice?
Upvotes: 0
Views: 208
Reputation: 488
You may be confusing the Base64 encoding method for the encryption primitive you seem to be trying to use. In the first code sample, you attempt to Base64-decode the string produced by Element.toString, and in the second you attempt to Base64-decode your test string. Neither string is base64-encoded to begin with, so this will most likely fail.
If you are trying to encrypt data, I think you should be looking for a much higher-level API than the one you are working with. Low-level cryptographic primitives are extremely easy to use in an insecure manner, even if you do get valid decryption and it appears to work.
Upvotes: 1