id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-encryption > apcsaEncryptPublicKey
prev  |  next  |  chance

Given a plaintext String and two numbers publicKey and mod, return an array of int values representing the characters in the String after public-key encryption using publicKey and mod. Public key encryption works by taking the numeric value of the char (not adjusted, so 'A' has the value 65 not 0 or 1), raising that numeric value to the power of publicKey and then calculating the remainder after division by mod. In the method after this, you will see how a different secret key (the "private" key) can be used to decrypt the encrypted characters.


Note, it would have been possible to return a String in this problem where each character had a numeric value based on the calculation described above, but most of the character values would have been non-alphanumeric and some might even be control sequences or other unrecognizable, non-ASCII characters.  Thus, to avoid visual confusion and preserve CodingBat compatibility, the int array was chosen instead.

This problem is based on, and helps to construct, the simplified public key encryption algorithm found at https://en.wikibooks.org/wiki/Cryptography/A_Basic_Public_Key_Example

Something you will discover with this problem is that, even with relatively small parameter values, the calculations will exceed the limits of primitive data types.  Java has a very useful class called BigInteger in the math package (must be referenced as java.math.BigInteger because CodingBat does not allow import statements), which even has a wonderful method designed explicitly for this sort of problem.  So, this challenge is suddenly more about managing the BigInteger objects than it is about the math.
https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html
HINT
Seriously, go look at the BigInteger documentation if you are stuck.

apcsaEncryptPublicKey("A", 113, 143) → [65]
apcsaEncryptPublicKey("A", 200344741, 206439349) → [27691901]
apcsaEncryptPublicKey("0", 113, 143) → [42]

...Save, Compile, Run (ctrl-enter)

public int[] apcsaEncryptPublicKey(String str, int publicKey, int mod) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 340

Copyright Nick Parlante 2017 - privacy