Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Helpers/SecurityHelpers/RsaHelper.java
Show First 20 Lines • Show All 110 Lines • ▼ Show 20 Line(s) | 103 | while (serialized.length() > 0) { | |||
---|---|---|---|---|---|
111 | encryptedChunk = cipher.doFinal(chunkBytes); | 111 | encryptedChunk = cipher.doFinal(chunkBytes); | ||
112 | chunks.put(Base64.encodeToString(encryptedChunk, Base64.NO_WRAP)); | 112 | chunks.put(Base64.encodeToString(encryptedChunk, Base64.NO_WRAP)); | ||
113 | } | 113 | } | ||
114 | 114 | | |||
115 | //Log.i("NetworkPacket", "Encrypted " + chunks.length()+" chunks"); | 115 | //Log.i("NetworkPacket", "Encrypted " + chunks.length()+" chunks"); | ||
116 | 116 | | |||
117 | NetworkPacket encrypted = new NetworkPacket(NetworkPacket.PACKET_TYPE_ENCRYPTED); | 117 | NetworkPacket encrypted = new NetworkPacket(NetworkPacket.PACKET_TYPE_ENCRYPTED); | ||
118 | encrypted.set("data", chunks); | 118 | encrypted.set("data", chunks); | ||
119 | encrypted.setPayload(np.getPayload(), np.getPayloadSize()); | 119 | encrypted.setPayload(np.getPayload()); | ||
120 | return encrypted; | 120 | return encrypted; | ||
121 | 121 | | |||
122 | } | 122 | } | ||
123 | 123 | | |||
124 | public static NetworkPacket decrypt(NetworkPacket np, PrivateKey privateKey) throws GeneralSecurityException, JSONException { | 124 | public static NetworkPacket decrypt(NetworkPacket np, PrivateKey privateKey) throws GeneralSecurityException, JSONException { | ||
125 | 125 | | |||
126 | JSONArray chunks = np.getJSONArray("data"); | 126 | JSONArray chunks = np.getJSONArray("data"); | ||
127 | 127 | | |||
128 | Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); | 128 | Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); | ||
129 | cipher.init(Cipher.DECRYPT_MODE, privateKey); | 129 | cipher.init(Cipher.DECRYPT_MODE, privateKey); | ||
130 | 130 | | |||
131 | StringBuilder decryptedJson = new StringBuilder(); | 131 | StringBuilder decryptedJson = new StringBuilder(); | ||
132 | for (int i = 0; i < chunks.length(); i++) { | 132 | for (int i = 0; i < chunks.length(); i++) { | ||
133 | byte[] encryptedChunk = Base64.decode(chunks.getString(i), Base64.NO_WRAP); | 133 | byte[] encryptedChunk = Base64.decode(chunks.getString(i), Base64.NO_WRAP); | ||
134 | String decryptedChunk = new String(cipher.doFinal(encryptedChunk)); | 134 | String decryptedChunk = new String(cipher.doFinal(encryptedChunk)); | ||
135 | decryptedJson.append(decryptedChunk); | 135 | decryptedJson.append(decryptedChunk); | ||
136 | } | 136 | } | ||
137 | 137 | | |||
138 | NetworkPacket decrypted = NetworkPacket.unserialize(decryptedJson.toString()); | 138 | NetworkPacket decrypted = NetworkPacket.unserialize(decryptedJson.toString()); | ||
139 | decrypted.setPayload(np.getPayload(), np.getPayloadSize()); | 139 | decrypted.setPayload(np.getPayload()); | ||
140 | return decrypted; | 140 | return decrypted; | ||
141 | } | 141 | } | ||
142 | 142 | | |||
143 | 143 | | |||
144 | } | 144 | } |