Win32Exception: The parameter is incorrect - java

I am making an application that displays ur saved browser password(s) (right now I'm using Google Chrome) in an easy way. Everytime I run this code I get an error at byte[] newbyte = Crypt32Util.cryptUnprotectData(mybyte);. The code used is written below. This code provides some context. I never had this problem and after some research I can't find a clear solution. I hope someone can help me with it.
Code:
Connection connection = null;
connection = DriverManager.getConnection("jdbc:sqlite:" + path_to_copied_db);
PreparedStatement statement = connection.prepareStatement("SELECT `origin_url`,`username_value`,`password_value` FROM `logins`");
ResultSet re = statement.executeQuery();
StringBuilder builder = new StringBuilder();
while (re.next()) {
String pass = "";
try {
byte[] mybyte = (byte[])re.getBytes("password_value");
byte[] newbyte = Crypt32Util.cryptUnprotectData(mybyte); //Error on this line:71
pass = new String(newbyte);
}catch(Win32Exception e){
e.printStackTrace();
}
builder.append(user + ": " + re.getString("origin_url") + " " + re.getString("username_value") + " " + re.getBinaryStream("password_value") + "\n");
}
Error:
com.sun.jna.platform.win32.Win32Exception: The parameter is incorrect.
at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:128)
at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:103)
at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:90)
at Client.Client.main(Client.java:71)

Related

com.google.code.gson cannot parse tamil results

So, I'm trying to fetch JSON results from https://api-thirukkural.vercel.app/api?num=1139 using Java-Telegram-Bot-Api and send it to telegram. I use com.google.code.gson dependency for parsing JSON.
The expected results from API:
{"number":1139,"sect_tam":"காமத்துப்பால்","chapgrp_tam":"களவியல்","chap_tam":"நாணுத் துறவுரைத்தல்","line1":"அறிகிலார் எல்லாரும் என்றேஎன் காமம்","line2":"மறுகின் மறுகும் மருண்டு.","tam_exp":"என்னைத் தவிர யாரும் அறியவில்லை என்பதற்காக என் காதல் தெருவில் பரவி மயங்கித் திரிகின்றது போலும்!","sect_eng":"Love","chapgrp_eng":"The Pre-marital love","chap_eng":"Declaration of Love's special Excellence","eng":"My perplexed love roves public street Believing that none knows its secret","eng_exp":"And thus, in public ways, perturbed will rove"}
Here is a piece of my java code:
String results = "";
Random random = new Random();
SendMessage message = new SendMessage();
String apiUrl = "https://api-thirukkural.vercel.app/api?num=" + random.nextInt(1329 + 1);
try {
URL url = new URL(apiUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
Scanner sc = new Scanner(url.openStream());
while (sc.hasNext()) {
results += sc.nextLine();
}
sc.close();
JSONArray jsonArray = new JSONArray("[" + results + "]");
JSONObject object = jsonArray.getJSONObject(0);
message.setChatId(update.getMessage().getChatId().toString());
message.setText("Number: " + object.getInt("number") + "\n\n" + object.getString("line1") + "\n"
+ object.getString("line2") + "\n\n" + object.getString("tam_exp") + "\n\n" + object.getString("eng_exp"));
conn.disconnect();
execute(message);
} catch (Exception e) {
e.printStackTrace();
}
The result in telegram:
Number: 1139
அறிகிலார� எல�லார�ம� என�றேஎன� காமம�
மற�கின� மற�க�ம� மர�ண�ட�.
என�னைத� தவிர யார�ம� அறியவில�லை என�பதற�காக என� காதல� தெர�வில� பரவி மயங�கித� திரிகின�றத� போல�ம�!
And thus, in public ways, perturbed will rove
Is this a problem in gson dependency? Can someone help me fix this? Thanks.
You need to specify the Charset on Scanner. That is probably the problem.
Example:
new Scanner(url.openStream(), StandardCharsets.UTF_8.name());
You should use the Charset that fits.

FileWriter not writing into directory properly

I have a simple program that reads in commands and performs them. Rightnow I have this code for inserting certain text into a text file:
Example command:
INSERT "John Smith" INTO college.student
My main method:
else if(command.substring(0,6).equalsIgnoreCase("INSERT")){
String string = command.substring(7, command.indexOf("INTO") - 1);
String DBNameTBName = command.substring(command.indexOf("INTO") + 5);
String tableName = DBNameTBName.substring(DBNameTBName.indexOf(".") + 1);
String DBName = DBNameTBName.substring(0, DBNameTBName.indexOf("."));
if(DBCommands.insert(string, DBName, tableName)){
statfileWriter.println("Inserted " + string + " into table " + tableName + " in " + DBName);
statfileWriter.println("(" + command + ")");
statfileWriter.flush();
}
else{
errfileWriter.println("Error: Could not insert " + string + " into table " + tableName + " in " + DBName);
errfileWriter.println("(" + command + ")");
errfileWriter.flush();
}
And the insert method it calls:
public static boolean insert(String string, String DBName, String tableName){
try{
string = string.substring(string.indexOf('"') + 1, string.lastIndexOf('"')); //removes quotes
File tableToWriteTo = new File(DBName + "/" + tableName + ".txt");
if (!tableToWriteTo.exists()){
return false;
}
PrintWriter writer = new PrintWriter(new FileWriter
(tableToWriteTo, true));
writer.println(string);
writer.close();
return true;
}
catch(Exception e){
return false;
}
}
I am getting very weird behavior with my insert method. It returns true as it always prints to my status log and not error log. I know the method to create the .txt file is working perfectly, I have tested it many times and the student.txt file is always there. With my insert command, if I change the File = new File line to this:
File tableToWriteTo = new File(tableName + ".txt");
Then it unsurprisingly creates a .txt file called "student" with my example command, but not in the "DBName" folder. If I change it to this:
File tableToWriteTo = new File(DBName + "/" + tableName);
Then it creates a file called "student" with no type (as in, Windows asks what I want to open it with) but puts in the string I want to insert into it. I should note that if there are multiple INSERT commands then it writes all the strings as I would like it to.
I have tried declaring PrintWriter and File in my main method and passing them in, but that doesn't work either.
How can I get it to write into students.txt in the directory college?
EDIT: Oh my goodness, I'm the stupidest person on Earth. I didn't look at the full commands list I received for this assignment and I forgot there was a delete command and they were BOTH working. I would delete this question but I'll leave this up in case anyone in the future wants to see an example of FileWriter.
I changed the if condition in the insert method. The file is not expected to exist. So ideally the condition should not be negated. I used the following code and it is working for me.
public class InsertToWriteTo {
public static void main(String[] args) {
boolean ret = insert("\"hello\"", "college", "student");
System.out.println(ret);
}
public static boolean insert(String string, String DBName, String tableName) {
try {
string = string.substring(string.indexOf('"') + 1, string.lastIndexOf('"')); // removes quotes
File tableToWriteTo = new File(DBName + "/" + tableName + ".txt");
if (tableToWriteTo.exists()) { // changed condition
System.out.println("File exists");
return false;
}
PrintWriter writer = new PrintWriter(new FileWriter(tableToWriteTo, true));
writer.println(string);
writer.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
Hope this helps!

JTOpen ProgramCall Socket Timeout

I'm working on a web app (running on Tomcat) that calls programs on an IBM i (AS/400) using the JTOpen ProgramCall class (com.ibm.as400.access.ProgramCall). My problem is with program calls that take more than 30s to respond, which are triggering a java.net.SocketTimeoutException: Read timed out exception.
There is a setTimeout() method available for this class, but it doesn't seem to have an effect on the socket timeout. I've also checked my Tomcat configurations and didn't see anything that would cause this behavior.
Does anyone know of a way to alter the timeout for such an implementation?
Code:
pgmCall.setProgram(getCompleteName(), parmList);
initializeAS400TextParameters();
// Run the AS/400 program.
try {
Trace.setTraceDiagnosticOn(true);
Trace.setTraceInformationOn(true);
Trace.setTraceWarningOn(true);
Trace.setTraceErrorOn(true);
Trace.setTraceDatastreamOn(true);
if (pgmCall.run() != true) {
messageList = pgmCall.getMessageList();
for (int i = 0; i < messageList.length; i++) {
log.debug("Error Message " + i + " " + messageList[i]);
}
setCompletionMsg("Program call failed.");
log.debug("442 Program call failed.");
return false;
} else {
messageList = pgmCall.getMessageList();
for (int i = 0; i < messageList.length; i++) {
log.debug("Success Message " + i + " " + messageList[i]);
}
setCompletionMsg("Program called ok.");
log.debug("452 Program called ok.");
return true;
}
} catch (Exception e) {
// This is where the timeout exception is thrown
log.debug("Error Running Program: " + e.getMessage() + " " + e.getLocalizedMessage());
setCompletionMsg(e.getMessage());
}
Well, after several more hours I've found the solution. Apparently the original developer added a socket timeout parameter to the JDBC connection string - simply removing the parameter did the trick as the default value is 0, or infinite timeout.
Before:
String connectionStr = "jdbc:as400://" + systemInfo.getIPAddress() + ":1527" + ";naming=system;socket timeout=30000;thread used=false;errors=full;prompt=false;date format=iso;block size=128;transaction isolation=none;user=" + systemInfo.getUserName() + ";password=" + systemInfo.getPassword();
After:
String connectionStr = "jdbc:as400://" + systemInfo.getIPAddress() + ":1527" + ";naming=system;thread used=false;errors=full;prompt=false;date format=iso;block size=128;transaction isolation=none;user=" + systemInfo.getUserName() + ";password=" + systemInfo.getPassword();
:\

Java FTP LIST command

Hello I am in the process of making a FTP client for a uni project and before starting I am experimenting with the basic commands of the FTP protocol. So far I have been able to login successfully to a FTP server. Now, what I want to do is to list the root directory in order to get it's contents.
I have implemented this method to do what I describe but I can't seem to make it work. As far as I can tell it does go into passive mode, but when I issue the list command nothing happens. I cannot understand why this is happening. Can anyone help?
Here is the code in question:
public synchronized boolean list() throws IOException{
sendLine("PASV");
String response = readLine();
if(!response.startsWith("227 "))
throw new IOException("Could not request PASSIVE mode: " + response);
String ip = null;
int port = -1;
int opening = response.indexOf('(');
int closing = response.indexOf(')', opening + 1);
if(closing > 0){
String dataLink = response.substring(opening + 1, closing);
StringTokenizer tokenizer = new StringTokenizer(dataLink, ",");
try{
ip = tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken();
port = Integer.parseInt(tokenizer.nextToken()) * 256 + Integer.parseInt(tokenizer.nextToken());
}catch(Exception e){
throw new IOException("Received bad data information: " + response);
}
}
sendLine("LIST");
response = readLine();
return (response.startsWith("200 "));
}

Getting image from server and saving to MySQL DB

Im getting an image from server as InputStream and then saving it to mySQL database. It works when I use Thread.sleep(5000);. But if I dont use it no picture is saved to the DB or only one picture and half of it or less. So I understand that the program needs time writing image to the database, but how much time? This is the question, I would like to know exactly when it finished writing image to the database and can start with the next image. Below is my code:
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
int ID = rs.getInt(1);
String myName = rs.getString(2);
try {
String myCommand = "take picture and save /mydir/mydir2/mydir3" + myName + ".png";
telnet.sendCommand(myCommand); // Here taking a picture via telnet
// Thread.sleep(5000);// If I uncomment this line it works
String sqlCommand = "UPDATE my_table SET Picture = ? WHERE ID ='" + ID +"';";
PreparedStatement statement = conn.prepareStatement(sqlCommand);
String ftpUrl = "ftp://"+server_IP+"/mydir/mydir2/mydir3" + myName + ".png;type=i";
URL url = new URL(ftpUrl);
URLConnection connUrl = url.openConnection();
//Thread.sleep(5000); // If I uncomment this line, it works too.
InputStream inputStreamTelnet = connUrl.getInputStream();
statement.setBlob(1, inputStreamTelnet);
int row = statement.executeUpdate();
if (row > 0) {
System.out.println("A picture was inserted into DB.");
System.out.println("Value of row(s) : " + row);
}
} catch (Exception e) {
e.printStackTrace();
}
} // End of while
I would expect to put the waiting(sleep) after InputStream inputStreamTelnet = connUrl.getInputStream(); but it doesnt work when I put the sleep after this line. It works only when the sleep is before. Could someone explain me why and I would like to avoid using Thread.sleep(5000); and instead would like to wait exact time or not wait at all which will make the program faster also there might be a case saving the picture can take more than 5 seconds or maybe saving the picture doesnt take time but opening the url connection. There are 2 sleep lines on the code when I uncomment one of them the program works(saves the images to mysql DB successfully). I also verified on the server that the images exist but in the end I dont see them in the mysql DB.
UPDATE : I removed the try block and telnet stuff now it works without waiting but I really need the telnet stuff...
UPDATE 2: After inspecting my telnet class found out that I forgot to apply a change I made to single line... now it works without wait!
Huh, I tested my code on JDK 1.7.0_67 / PostgreSQL 9.2 and it works well:
public class ImageLoader {
private static final int START_IMAGE_ID = 1;
private static final int END_IMAGE_ID = 1000;
private static final String IMAGE_URL = "http://savepic.net/%d.jpg";
public static void main(String[] args) throws SQLException, IOException {
Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "username", "password");
PreparedStatement imageStatement = connection.prepareStatement("INSERT INTO public.image VALUES(?, ?)");
for (int i = START_IMAGE_ID; i <= END_IMAGE_ID; i++) {
String imageUrl = String.format(IMAGE_URL, i);
URL url = new URL(imageUrl);
URLConnection urlConnection = url.openConnection();
imageStatement.setLong(1, i);
imageStatement.setBytes(2, read(urlConnection.getInputStream()));
int count = imageStatement.executeUpdate();
if (count != 1) {
throw new IllegalStateException("Image with ID = " + i + " not inserted");
} else {
System.out.println("Image (" + imageUrl + ") saved to database");
}
}
imageStatement.close();
connection.close();
}
private static byte[] read(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1 << 15); // assume image average size ~ 32 Kbytes
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] buffer = new byte[1 << 10];
int read = -1;
while ((read = bufferedInputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, read);
}
return byteArrayOutputStream.toByteArray();
}
}

Categories

Resources