diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php
index ab50fba69774430016d7313b565d6aa3aea6bd52..9dc95ea67640c5384519a60e86cc40ee8fc44ca7 100644
--- a/apps/files_encryption/lib/keymanager.php
+++ b/apps/files_encryption/lib/keymanager.php
@@ -60,9 +60,23 @@ class Keymanager {
 	 * @param string user name of the file owner
 	 * @return string file key or false
 	 */
-	public static function getFileKey($user, $file) {
+	public static function getFileKey($userId, $path) {
+		
+		$keypath = ltrim($path, '/');
+		$user = $userId;
+
+		// update $keypath and $user if path point to a file shared by someone else
+		$query = \OC_DB::prepare( "SELECT uid_owner, source, target FROM `*PREFIX*sharing` WHERE target = ? AND uid_shared_with = ?" );
+		$result = $query->execute( array ('/'.$userId.'/files/'.$keypath, $userId));
+		if ($row = $result->fetchRow()){
+			$keypath = $row['source'];
+			$keypath_parts=explode('/',$keypath);
+			$user = $keypath_parts[1];
+			$keypath = str_replace('/'.$user.'/files/', '', $keypath);
+		}
+		
 		$view = new \OC_FilesystemView('/'.$user.'/files_encryption/keyfiles/');
-		return $view->file_get_contents($file.'.key');
+		return $view->file_get_contents($keypath.'.key');
 	}	
 	
 	/**
@@ -115,13 +129,27 @@ class Keymanager {
 	 * @return bool true/false
 	 */
 	public static function setFileKey( $userId, $path, $key ) {
-	
+
 		\OC_FileProxy::$enabled = false;
+
+		$targetpath = ltrim($path, '/');
+		$user = $userId;
+		
+		// update $keytarget and $user if key belongs to a file shared by someone else
+		$query = \OC_DB::prepare( "SELECT uid_owner, source, target FROM `*PREFIX*sharing` WHERE target = ? AND uid_shared_with = ?" );
+		$result = $query->execute( array ('/'.$userId.'/files/'.$targetpath, $userId));
+		if ($row = $result->fetchRow()){
+			$targetpath = $row['source'];
+			$targetpath_parts=explode('/',$targetpath);
+			$user = $targetpath_parts[1];
+			$targetpath = str_replace('/'.$user.'/files/', '', $targetpath);
+		}
+		
+		$view = new \OC_FilesystemView( '/' . $user . '/files_encryption/keyfiles' );
+		$path_parts = pathinfo($targetpath);
 		
-		$view = new \OC_FilesystemView( '/' . $userId . '/' . 'files_encryption/keyfiles' );
-		$path_parts = pathinfo($path);
 		if (!$view->file_exists($path_parts['dirname'])) $view->mkdir($path_parts['dirname']);
-		$result = $view->file_put_contents( '/' . $path . '.key', $key );
+		$result = $view->file_put_contents( '/' . $targetpath . '.key', $key );
 		
 		\OC_FileProxy::$enabled = true;	
 		
diff --git a/lib/ocs.php b/lib/ocs.php
index 526688b430908438d4e035892b844105ae3c64be..168121965010bbf3afab9b3956121c67ccd73097 100644
--- a/lib/ocs.php
+++ b/lib/ocs.php
@@ -192,8 +192,8 @@ class OC_OCS {
 				
 		// keygetfiles
 		}elseif(($method=='get') and ($ex[$paracount-7] == 'v1.php') and ($ex[$paracount-6]=='cloud') and ($ex[$paracount-5] == 'user') and ($ex[$paracount-3] == 'filekey')){
-			$user=$ex[$paracount-4];
-			$file = urldecode($ex[$paracount-2]);
+			$user=$ex[$paracount-4];
+			$file = urldecode($ex[$paracount-2]);
 			OC_OCS::fileKeyGet($format,$user, $file);
 		
 		//keysetfiles