J2ME, Exception when casting from PemObject to AsymmetricKeyParameter with BouncyCastle - encryption

my question for today is about the BouncyCastle encryption Library for J2ME,
I have some code in C# in which I do the following cast:
AsymmetricKeyParameter pubKey = (AsymmetricKeyParameter)pubReader.ReadObject();
this works on the C# end, however when I do it in Java I get an error stating that pubReader doesn't have a ReadObject() method, I changed it to readObject(), still doesn't exist so I went ahead to search for a method that would return an Object type, I only found readPemObject() as follows:
AsymmetricKeyParameter pubKey = (AsymmetricKeyParameter)pubReader.readPemObject();
However... this is when I hit the big problem, as I cannot cast from PemObject to AsymmetricKeyParameter and end up having an error...
here's the majority of the code in question:
PemReader pubReader = new PemReader(new StringReader(pubKey));
AsymmetricKeyParameter nhe = (AsymmetricKeyParameter)pubReader.readPemObject();
Imported teste = new Imported();
PemObject pobj = pubReader.readPemObject();
System.out.println("PemObject: " + pobj);
byte[] output = teste.Encrypt(holyshizzle.getBytes(), nhe);
System.out.println(new String(output));
byte[] revvit = teste.Decrypt(output, (AsymmetricKeyParameter)blah.getPrivate());
System.out.println(new String(revvit));`
PS: The Imported class is where the encrypt/decrypt code is and all of that is working fine (tested it with generated keys), I'm attempting to do this with pre-made encryption keys
Regards,
Gonçalo Vieira

Try replacing
AsymmetricKeyParameter nhe = (AsymmetricKeyParameter)pubReader.readPemObject();
with
AsymmetricKeyParameter nhe = PublicKeyFactory.createKey(pubReader.readPemObject().getContent());

Related

SQLite connection string cannot contain a comma?

I'm getting an error from SQLite when the connection string contains a comma. I don't have full control over where the database will end up, so it's possible a user will place it into a directory containing arbitrary characters. In this case, it appears that the presence of a comma (,) in the path causes an error when connecting. Here is the connection string:
Data Source=C:/Users/Dan/AppData/LocalLow/Gravia Software, LLC/Gravia/exampleDatabase.db;
This results in the following exception when trying to connect:
ArgumentException: Invalid ConnectionString format for parameter "LLC/Gravia/exampleDatabase.db"
It appears that the presence of the comma in the connection string is the issue. I've tried escaping the command (\,), wrapping the whole thing in quotes, but it doesn't seem to matter. Any ideas?
Edit:
The actual code I was executing comes from this page: https://ornithoptergames.com/how-to-set-up-sqlite-for-unity/
var dbPath = "URI=file:" + Application.persistentDataPath + "/exampleDatabase.db";
using (var conn = new SqliteConnection(dbPath)) {
conn.Open(); // Error occurs here
// etc
}
I tried changing out URI=file: for Data Source=, as the result was the same.
Try this ?
"Data Source=" + "C:/Users/Dan/AppData/LocalLow/Gravia Software," + "LLC/Gravia/exampleDatabase.db";
I solved this way:
var currDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Path.GetDirectoryName(Application.streamingAssetsPath + "/DB/"));
SqliteConnection dbcon = null;
try
{
dbcon = new SqliteConnection(_Connection);
dbcon.Open();
}
catch (Exception e)
{
Debug.Log(e.Message);
return null;
}
finally
{
Directory.SetCurrentDirectory(currDir);
}
You can escape unsupported characters in ODBC connection variables using curly-brackets ({}).
So wrap your variable, like below
Data Source={C:/Users/Dan/AppData/LocalLow/Gravia Software, LLC/Gravia/exampleDatabase.db};
Source

Cannot identify language in SALT

I just started using SALT for a project i am working on. It is said to work with Python but I find quite a bit of difference in thier syntax and overall format. I have pasted a code for a simple task which just opens and imports and loads some libraries onto the SALT console. I hope someone can check abnd see if he/she can instantly identify the language being used and what the code does. This is because i have a suspicion that the code is a mixture between C, Python as well as Java... if that is so doen't that mean it should be a totally different language on its own?
code:
var rtwxlib = import'rtwxlib';
var string = import'string';
var monitor = rtwxlib.Monitor
{
EvActivate = func() { print "Activate\n"; };
EvShutdown = func() { print "Shutdown\n"; };
EvProgress = func(self, fDone, msg = "") {
print("Progress: %d %s \r"::format(fDone*100, msg));
};
EvEventMsg = func(self, msg) {
print("\nEvent: %s\n"::format(iStat, msg));
};
};
var solver = rtwxlib.Solver(monitor);
solver::Open("test.wrx");
solver::DelGeometry();
solver::SaveAs('testresults.wrx');
solver::Close();
I was able to indentify the syntax of the language used as C. I beieve the problem I had which confused me was the libraries used for this code which I later found out was for a particular program. Thank you to all those who tried to help though :)

How to get Binary data with javascript using FileReader api with correct encoding

I got a mp4 data using FileReader api, but I have a problem at encoding!
With this function,
var reader = new FileReader();
var blob = new Blob([this.response], {type : "video/mp4"});
reader.onload= function (evt) {
mp4text = evt.target.result;
mp4text = mp4text.toString()
//mp4text = mp4text.slice(22);
//mp4text = CryptoJS.AES.encrypt(mp4text, "test");
//mp4text = window.atob(mp4text);
var myBlob = new Blob([evt.target.result], {type : "video/mp4"});//NOT SAME contrast to blob!
var downloadUrl = URL.createObjectURL(myBlob);
document.getElementById('myVideo').src = downloadUrl;
}
reader.readAsBinaryString(blob);
I thought myBlob has same filedata as blob but some data changed! With more detail, Many of character are same but some hex code is different. How can I solve this problem?
Strings in JavaScript cannot represent arbitrary binary data, so doing readAsBinaryString may not be what you think.
What readAsBinaryString does is for each source byte it gives you a destination character(I don't which character encoding it uses off the top of my head).
So if you have a utf-8 character say ✔, then readAsBinaryString will give you â since that character is tree bytes long %E2%9C%94.
If you try to turn this back to binary/blob the string â is treated as utf-8 which is not 3 bytes but 7(%C3%A2%C5%93%E2%80%9D)
My suggestion would be to use readAsArrayBuffer, I'm sure CryptoJS supports arraybuffers.

String as xml data source in XQJ

I would like to parse my XML string using an XQJ implementation, for example, SAXON. All examples I could find refer to some database connections. Is it possible to use simple String as xml source?
Saxon has an XQJ interface, and you could either use the doc function() from XQuery e.g. :
XQDataSource ds = new SaxonXQDataSource();
XQConnection conn = ds.getConnection();
XQPreparedExpression exp = conn.prepareExpression("doc('file:/some/file.xml')/child::node()");
XQResultSequence result = exp.executeQuery();
while(result.next()) {
System.out.println(result.getItemAsString(null));
}
or directly inject in the XML into the query. e.g. -
XQDataSource ds = new SaxonXQDataSource();
XQConnection conn = ds.getConnection();
XQPreparedExpression exp = conn.prepareExpression("<a><b>test</b></a>/child::node()");
XQResultSequence result = exp.executeQuery();
while(result.next()) {
System.out.println(result.getItemAsString(null));
}
Try using
void XQExpression.bindDocument(javax.xml.namespace.QName varName, javax.xml.transform.Source value, XQItemType type)
with XQConstants.CONTEXT_ITEM as the first argument, and a StreamSource wrapping a StringReadeer as the second.

Using as3Crypto to encrypt/decrypt without ampersands

I was using as3Crypto with no probs
http://www.zedia.net/2009/as3crypto-and-php-what-a-fun-ride/
but then I saw some special characters and I realised I could encounter ampersands.
Which is a pain because they will be inserted into a query string.
Is there a way to ensure the as3Crypto encryption does not produce ampersands?
public function encrypt(txt:String = ''):String
{
var data:ByteArray = Hex.toArray(Hex.fromString(txt));
var pad:IPad = new PKCS5;
var mode:ICipher = Crypto.getCipher(type, key, pad);
pad.setBlockSize(mode.getBlockSize());
mode.encrypt(data);
return ''+Base64.encodeByteArray(data);
}
Assuming a standard base64 implementation, Base64.encodeByteArray(data); will not produce ampersands.

Resources