favetelinguis
Need help converting Java encryption example to Elixir
Im writing an integration in Elixir and as one part I need to create an encrypted login string. However I have zero experience with encryption and im not able to understand the Erlang docs well enogh to understand. I would rather use pure Erlang to do this that add some dependency to a Elexir wrapper if possible.
The Java code I need to write in Elixir looks as follows.
private String encryptAuthParameter(String user, String password)
throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// Construct the base for the auth parameter
String login =
Base64.getEncoder().encodeToString(user.getBytes())
+ ":"
+ Base64.getEncoder().encodeToString(password.getBytes())
+ ":"
+ Base64.getEncoder()
.encodeToString(String.valueOf(System.currentTimeMillis()).getBytes());
// RSA encrypt it using NNAPI public key
PublicKey pubKey = getKeyFromPEM(Main.PUBLIC_KEY_FILENAME);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encryptedBytes = cipher.doFinal(login.getBytes("UTF-8"));
// Encode the encrypted data in Base64
String encodedEncryptedBytes = Base64.getEncoder().encodeToString(encryptedBytes);
return URLEncoder.encode(encodedEncryptedBytes, "UTF-8");
}
private static PublicKey getKeyFromPEM(String filename)
throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filename));
String line = null;
String key = "";
while (true) {
line = reader.readLine();
if (line == null) break;
else if (line.startsWith("-----BEGIN PUBLIC KEY-----")) continue;
else if (line.startsWith("-----END PUBLIC KEY-----")) continue;
else key += line.trim();
}
byte[] binary = Base64.getDecoder().decode(key);
X509EncodedKeySpec spec = new X509EncodedKeySpec(binary);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
} finally {
if (reader != null) {
reader.close();
}
}
}
The only thing I have so far is:
def encryptAuthParameter(user, password) do
now = DateTime.utc_now() |> DateTime.to_unix(:millisecond) |> Integer.to_string()
# Convert to Base64
login = Base.encode64(user) <> ":" <> Base.encode64(password) <> ":" <> Base.encode64(now)
# Use public key to encode message
File.read!()
# Base 64 encode the encrypted string
login
end
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First Post!
favetelinguis
Turns out it was not that hard. Here is the code that got it working.