private static byte[] encrypt(String text, Key key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { return encrypt(text.getBytes(StandardCharsets.UTF_8), key); }
public static void main(String[] args) throws Exception { String key = genKey(); System.out.println("Key is: " + key); String message = "long message to be encrypted"; System.out.println("Value clean: " + message); String res = KeyHelper.encryptNew(message, key); System.out.println("Value encrypted: " + res); message = KeyHelper.decryptNew(res, key); System.out.println("Value cleat: " + message); } }
public static String decryptNew(String encryptedValue, String keyValue) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { Key key = generateKey(keyValue.getBytes(StandardCharsets.UTF_8)); Cipher c = Cipher.getInstance(AES_ALGORITHM); c.init(Cipher.DECRYPT_MODE, key); byte[] decodedValue = decodeBase64(encryptedValue); byte[] decValue = c.doFinal(decodedValue); return new String(decValue); }
KeyHelper.encryptNew( "{\"name\":\"" + serviceName + "\",\"action\":\"register\"}", serviceKey); httpPost.releaseConnection(); String decryptedToken = KeyHelper.decryptNew(encryptedToken, serviceKey); log.trace("Token is: " + decryptedToken); this.token = decryptedToken;
@Override public String generateKey(String projectId, String name) throws IOException, NoSuchAlgorithmException, AlreadyExistingException { log.debug("Generating keypair"); if (keyRepository.findKey(projectId, name) != null) throw new AlreadyExistingException("A key with the name " + name + " exists already."); KeyPair keyPair = KeyHelper.generateRSAKey(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); String publicKeyString = KeyHelper.encodePublicKey(publicKey, name); Key key = new Key(); key.setName(name); key.setProjectId(projectId); key.setFingerprint(KeyHelper.calculateFingerprint(publicKey.getEncoded())); key.setPublicKey(publicKeyString); log.debug(publicKeyString); keyRepository.save(key); log.info("Added new key: " + key); return KeyHelper.parsePrivateKey(privateKey.getEncoded()); }
@Override public boolean isService(String tokenToCheck) { for (ServiceMetadata serviceMetadata : serviceRepository.findAll()) { if (serviceMetadata.getToken() != null && !serviceMetadata.getToken().equals("")) { String encryptedServiceToken = serviceMetadata.getToken(); String keyData = serviceMetadata.getKeyValue(); try { if (KeyHelper.decryptNew(encryptedServiceToken, keyData).equals(tokenToCheck)) return true; } catch (Exception e) { e.printStackTrace(); } } } return false; }
@Override public Key addKey(String projectId, String name, String key) throws NoSuchAlgorithmException, BadFormatException, AlreadyExistingException { if (keyRepository.findKey(projectId, name) != null) throw new AlreadyExistingException("A key with the name " + name + " exists already."); Key keyToAdd = new Key(); keyToAdd.setName(name); keyToAdd.setProjectId(projectId); keyToAdd.setPublicKey(key); keyToAdd.setFingerprint(KeyHelper.calculateFingerprint(KeyHelper.parsePublicKey(key))); keyRepository.save(keyToAdd); return keyToAdd; } }
serviceMetadata.setKeyValue(KeyHelper.genKey()); log.debug("Saving ServiceMetadata: " + serviceMetadata); serviceRepository.save(serviceMetadata);
private String encodePublicKey(RSAPublicKey key, String keyname) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); /* encode the "ssh-rsa" string */ byte[] sshrsa = new byte[] {0, 0, 0, 7, 's', 's', 'h', '-', 'r', 's', 'a'}; out.write(sshrsa); /* Encode the public exponent */ BigInteger e = key.getPublicExponent(); byte[] data = e.toByteArray(); encodeUInt32(data.length, out); out.write(data); /* Encode the modulus */ BigInteger m = key.getModulus(); data = m.toByteArray(); encodeUInt32(data.length, out); out.write(data); return "ssh-rsa " + Base64.getEncoder().encodeToString(out.toByteArray()) + " " + keyname + "@openbaton"; }
for (ServiceMetadata serviceMetadata : serviceRepository.findAll()) { try { unencryptedBody = KeyHelper.decryptNew(body, serviceMetadata.getKeyValue()); } catch (NoSuchPaddingException | NoSuchAlgorithmException service.setToken(KeyHelper.encryptNew(token.getValue(), service.getKeyValue())); service.setTokenExpirationDate(token.getExpiration().getTime()); serviceRepository.save(service);
public static String encryptNew(String valueToEnc, String keyValue) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { Key key = generateKey(keyValue.getBytes(StandardCharsets.UTF_8)); Cipher c = Cipher.getInstance(AES_ALGORITHM); c.init(Cipher.ENCRYPT_MODE, key); byte[] encValue = c.doFinal(valueToEnc.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encValue); }