RSA Encryption for Passwords
RSA Verschlüsseltes passwort kann über die Klasse RSA enschlüsselt werden. Keys für public - und private key liegen in den Dateien private.kfs und public.kfs
This commit is contained in:
Binary file not shown.
@@ -1482,3 +1482,75 @@ java.sql.SQLClientInfoException: Client info name 'ApplicationName' not supporte
|
||||
2023-05-09 18:42:56 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 19:24:03 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 19:24:10 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 19:24:12 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 19:24:13 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 19:28:20 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:11:00 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:11:59 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:12:54 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:13:35 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:14:49 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:18:00 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:20:34 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:21:44 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:22:01 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:24:05 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:27:20 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 20:46:16 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLNonTransientException: Keine Daten verfügbar
|
||||
No data is available [2000-214]
|
||||
2023-05-09 18:55:35 jdbc[3]: exception
|
||||
java.sql.SQLClientInfoException: Client info name 'ApplicationName' not supported.
|
||||
at org.h2.jdbc.JdbcConnection.setClientInfo(JdbcConnection.java:1573)
|
||||
at com.intellij.database.remote.jdbc.impl.RemoteConnectionImpl.setClientInfo(RemoteConnectionImpl.java:469)
|
||||
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
||||
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
|
||||
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|
||||
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
|
||||
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
|
||||
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
|
||||
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
|
||||
at java.base/java.security.AccessController.doPrivileged(AccessController.java:712)
|
||||
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
|
||||
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:587)
|
||||
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
|
||||
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:705)
|
||||
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
|
||||
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:704)
|
||||
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
|
||||
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
|
||||
at java.base/java.lang.Thread.run(Thread.java:833)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,10 +1,10 @@
|
||||
package com.example.backend.anwprj;
|
||||
|
||||
import com.example.backend.anwprj.Encryption.RSA;
|
||||
import com.example.backend.anwprj.Days.Day;
|
||||
import com.example.backend.anwprj.Entries.Entry;
|
||||
import com.example.backend.anwprj.Semesters.Semester;
|
||||
import com.example.backend.anwprj.Users.User;
|
||||
import com.example.backend.anwprj.Users.UserSessionIDHandler;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
@@ -29,6 +29,7 @@ public class DataBaseHandler
|
||||
private Connection connection;
|
||||
//sql statement
|
||||
private Statement st;
|
||||
private boolean encrypted = false;
|
||||
|
||||
public DataBaseHandler()
|
||||
{
|
||||
@@ -380,7 +381,18 @@ public class DataBaseHandler
|
||||
|
||||
try
|
||||
{
|
||||
st.executeUpdate("insert into users (firstname, lastname, mail, password, permission) values (" + "'" + user.getFirstName() + "', '" + user.getLastName() + "', '" + user.getMail() + "', '" + user.getPassword() + "', " + 0 + ")");
|
||||
String password = user.getPassword();
|
||||
if(encrypted)
|
||||
{
|
||||
RSA rsa = RSA.getInstance();
|
||||
byte[] psw = rsa.loadByteArray(password);
|
||||
try {
|
||||
password = rsa.decrypt(psw);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
st.executeUpdate("insert into users (firstname, lastname, mail, password, permission) values (" + "'" + user.getFirstName() + "', '" + user.getLastName() + "', '" + user.getMail() + "', '" + password + "', " + 0 + ")");
|
||||
return getUser(user.getMail(), user.getPassword());
|
||||
} catch (Exception e)
|
||||
{
|
||||
@@ -393,13 +405,25 @@ public class DataBaseHandler
|
||||
{
|
||||
try
|
||||
{
|
||||
String password = user.getPassword();
|
||||
if(encrypted)
|
||||
{
|
||||
RSA rsa = RSA.getInstance();
|
||||
byte[] psw = rsa.loadByteArray(password);
|
||||
try {
|
||||
password = rsa.decrypt(psw);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if(!elevationRequestByAdmin)
|
||||
{
|
||||
st.executeUpdate("update users set " +
|
||||
"firstname='" + user.getFirstName() + "', " +
|
||||
"lastname='" + user.getLastName() + "', " +
|
||||
"mail='" + user.getMail() + "', " +
|
||||
"password='" + user.getPassword() + "' " +
|
||||
"password='" + password + "' " +
|
||||
"where id=" + id);
|
||||
}
|
||||
else
|
||||
@@ -408,7 +432,7 @@ public class DataBaseHandler
|
||||
"firstname='" + user.getFirstName() + "', " +
|
||||
"lastname='" + user.getLastName() + "', " +
|
||||
"mail='" + user.getMail() + "', " +
|
||||
"password='" + user.getPassword() + "'," +
|
||||
"password='" + password + "'," +
|
||||
"permission=" + user.getPermissions() +
|
||||
"where id=" + id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.example.backend.anwprj.Encryption;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.*;
|
||||
|
||||
public class RSA {
|
||||
|
||||
private PublicKey publicKey;
|
||||
private PrivateKey privateKey;
|
||||
|
||||
private static RSA instance = null;
|
||||
|
||||
public static RSA getInstance()
|
||||
{
|
||||
if(instance == null)
|
||||
{
|
||||
instance = new RSA();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private RSA(){}
|
||||
|
||||
public String decrypt(byte[] text) throws Exception
|
||||
{
|
||||
readKeyPairs();
|
||||
var cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
var plainText = cipher.doFinal(text);
|
||||
return new String(plainText, StandardCharsets.US_ASCII);
|
||||
}
|
||||
|
||||
private void readKeyPairs() throws Exception
|
||||
{
|
||||
var publicInput = new ObjectInputStream(new FileInputStream(new File("./public.kfs")));
|
||||
publicKey = (PublicKey) publicInput.readObject();
|
||||
publicInput.close();
|
||||
|
||||
var privateInput = new ObjectInputStream(new FileInputStream(new File("./private.kfs")));
|
||||
privateKey = (PrivateKey) privateInput.readObject();
|
||||
privateInput.close();
|
||||
}
|
||||
|
||||
public byte[] loadByteArray(String text)
|
||||
{
|
||||
String[] split = text.split(",");
|
||||
byte[] output = new byte[split.length];
|
||||
for(int i = 0; i < split.length; i++)
|
||||
{
|
||||
output[i] = Byte.parseByte(split[i]);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.example.backend.anwprj.controller;
|
||||
|
||||
import com.example.backend.anwprj.Encryption.RSA;
|
||||
import com.example.backend.anwprj.DataBaseHandler;
|
||||
import com.example.backend.anwprj.Users.User;
|
||||
import com.example.backend.anwprj.Users.UserPermissions;
|
||||
@@ -44,6 +45,18 @@ public class UserController {
|
||||
@GetMapping("/users")
|
||||
public ResponseEntity<String> login(@RequestParam(name = "mail") String mail, @RequestParam(name = "password") String password) {
|
||||
//generate a session id for the user trying to login
|
||||
boolean isEncrypted = true;
|
||||
if(isEncrypted)
|
||||
{
|
||||
RSA rsa = RSA.getInstance();
|
||||
byte[] psw = rsa.loadByteArray(password);
|
||||
try {
|
||||
password = rsa.decrypt(psw);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
String sessionID = UserSessionIDHandler.addUser(mail, password);
|
||||
|
||||
return new ResponseEntity<>(sessionID, HttpStatus.OK);
|
||||
|
||||
Reference in New Issue
Block a user