public static String calculateContentKeyChecksum(String uuid, byte[] aesKey) throws Exception { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey, "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptionResult = cipher.doFinal(uuid.getBytes("UTF8")); byte[] checksumByteArray = new byte[8]; System.arraycopy(encryptionResult, 0, checksumByteArray, 0, 8); String checksum = Base64.encode(checksumByteArray); return checksum; }
public static byte[] encryptSymmetricKey(String protectionKey, byte[] inputData) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding"); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); byte[] protectionKeyBytes = Base64.decode(protectionKey); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(protectionKeyBytes); Certificate certificate = certificateFactory.generateCertificate(byteArrayInputStream); Key publicKey = certificate.getPublicKey(); SecureRandom secureRandom = new SecureRandom(); cipher.init(Cipher.ENCRYPT_MODE, publicKey, secureRandom); byte[] cipherText = cipher.doFinal(inputData); return cipherText; }