Last active
March 31, 2020 14:33
-
-
Save slugmandrew/bc84d53710149539d00da53984a1bc81 to your computer and use it in GitHub Desktop.
Scala and Java encryption using javax.crypto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.efstech.pdm.blobstorage.upload | |
import com.efstech.pdm.blobstorage.encryption.FileEncrypterDecrypter | |
import com.efstech.pdm.common.util.UsesLogger | |
import javax.crypto.{Cipher, KeyGenerator} | |
import org.testng.Assert | |
import org.testng.annotations.Test | |
class EncryptionTest extends UsesLogger { | |
val scalaFile = "scala.txt" | |
val javaFile = "java.txt" | |
val javaFile2 = "java2.txt" | |
@Test | |
def checkJavaScala(): Unit = { | |
val originalContent1 = "foobar111" | |
val originalContent2 = "foobar222" | |
val originalContent3 = "foobar333" | |
val secretKey = KeyGenerator.getInstance("AES").generateKey | |
val transformation = "AES/CBC/PKCS5Padding" | |
val cipher = Cipher.getInstance(transformation) | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey) | |
val javaImpl = new FileEncrypterDecrypterJava(secretKey, cipher) | |
val javaImpl2 = new FileEncrypterDecrypterJava(secretKey, cipher) | |
val scalaImpl = new FileEncrypterDecrypter(secretKey, cipher) | |
javaImpl.encrypt(originalContent1, javaFile) | |
javaImpl2.encrypt(originalContent2, javaFile2) | |
scalaImpl.encrypt(originalContent3, scalaFile) | |
log.info("Encrypted Content (J): " + javaImpl.read(javaFile)) | |
log.info("Encrypted Content (J2): " + javaImpl.read(javaFile2)) | |
log.info("Encrypted Content (S): " + scalaImpl.read(scalaFile)) | |
val decryptedContentJava = javaImpl.decrypt(javaFile) | |
val decryptedContentJava2 = javaImpl2.decrypt(javaFile2) | |
val decryptedContentScala = javaImpl.decrypt(scalaFile) | |
log.info("Decrypted Content (J): " + decryptedContentJava) | |
log.info("Decrypted Content (J2): " + decryptedContentJava2) | |
log.info("Decrypted Content (S): " + decryptedContentScala) | |
Assert.assertEquals(decryptedContentJava, originalContent1) | |
Assert.assertEquals(decryptedContentJava2, originalContent2) | |
Assert.assertEquals(decryptedContentScala, originalContent3) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.efstech.pdm.blobstorage.encryption | |
import java.io._ | |
import com.efstech.pdm.common.util.UsesLogger | |
import javax.crypto._ | |
import javax.crypto.spec.IvParameterSpec | |
import scala.util.control.Exception.ultimately | |
class FileEncrypterDecrypter(secretKey: SecretKey, cipher: Cipher) extends UsesLogger { | |
def encrypt(content: String, fileName: String): Unit = { | |
// initialization vector | |
val iv = cipher.getIV | |
val fileOut = new FileOutputStream(fileName) | |
val cipherOut = new CipherOutputStream(fileOut, cipher) | |
try { | |
fileOut.write(iv) | |
cipherOut.write(content.getBytes()) | |
} catch { | |
case e: Exception => e.printStackTrace() | |
} finally { | |
cipherOut.close() | |
fileOut.close() | |
} | |
} | |
def read(fileName: String): String = { | |
val fis = new FileInputStream(fileName) | |
val inputReader = new InputStreamReader(fis) | |
val reader = new BufferedReader(inputReader) | |
ultimately { | |
if (fis != null) fis.close() | |
if (inputReader != null) inputReader.close() | |
if (reader != null) reader.close() | |
} { | |
var sb = new StringBuilder | |
reader.lines().forEach(line => { | |
sb.append(line) | |
}) | |
sb.toString | |
} | |
} | |
def decrypt(fileName: String): String = { | |
val fileIn = new FileInputStream(fileName) | |
val cipherIn = new CipherInputStream(fileIn, cipher) | |
val inputReader = new InputStreamReader(cipherIn) | |
val reader = new BufferedReader(inputReader) | |
try { | |
val fileIv = new Array[Byte](16) | |
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv)) | |
fileIn.read(fileIv) | |
try { | |
var sb = new StringBuilder | |
reader.lines().forEach(line => { | |
sb.append(line) | |
}) | |
sb.toString | |
} finally { | |
if (fileIn != null) fileIn.close() | |
if (cipherIn != null) cipherIn.close() | |
if (inputReader != null) inputReader.close() | |
if (reader != null) reader.close() | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.efstech.pdm.blobstorage.upload; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.crypto.*; | |
import javax.crypto.spec.IvParameterSpec; | |
import java.io.*; | |
import java.security.InvalidAlgorithmParameterException; | |
import java.security.InvalidKeyException; | |
import java.security.SecureRandom; | |
import java.util.Arrays; | |
public class FileEncrypterDecrypterJava { | |
private SecretKey secretKey; | |
private Cipher cipher; | |
private static final Logger log = LoggerFactory.getLogger(FileEncrypterDecrypterJava.class); | |
public FileEncrypterDecrypterJava(SecretKey secretKey, Cipher cipher) { | |
this.secretKey = secretKey; | |
this.cipher = cipher; | |
} | |
public void encrypt(String content, String fileName) throws InvalidKeyException, IOException { | |
byte[] iv = cipher.getIV(); | |
try ( | |
FileOutputStream fileOut = new FileOutputStream(fileName); | |
CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher) | |
) { | |
// write the IV as plaintext | |
fileOut.write(iv); | |
// and the rest of the file as encrypted | |
cipherOut.write(content.getBytes()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public String read(String fileName) throws IOException { | |
String content; | |
try (FileInputStream fis = new FileInputStream(fileName); | |
InputStreamReader inputReader = new InputStreamReader(fis); | |
BufferedReader reader = new BufferedReader(inputReader) | |
) { | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
sb.append(line); | |
} | |
content = sb.toString(); | |
} | |
return content; | |
} | |
public String decrypt(String fileName) throws InvalidAlgorithmParameterException, InvalidKeyException, IOException { | |
String content; | |
try (FileInputStream fileIn = new FileInputStream(fileName)) { | |
byte[] fileIv = new byte[16]; | |
fileIn.read(fileIv); | |
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv)); | |
try ( | |
CipherInputStream cipherIn = new CipherInputStream(fileIn, cipher); | |
InputStreamReader inputReader = new InputStreamReader(cipherIn); | |
BufferedReader reader = new BufferedReader(inputReader) | |
) { | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
sb.append(line); | |
} | |
content = sb.toString(); | |
} | |
} | |
return content; | |
} | |
private void copy(InputStream is, OutputStream os) throws IOException { | |
int i; | |
byte[] b = new byte[1024]; | |
while ((i = is.read(b)) != -1) { | |
os.write(b, 0, i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment