How to Code Encryption Algorithm in Java?

Java script to verify an algorithm

  • I am trying to create anspreadsheet which I can use to verify the validity of a routing number: I have found a Java script online which I will post below. My question is that I am not sure how to contruct the spreadsheet (which cells?) for the script to apply... The routing number is a 9-digit number: I will need a spreadsheet where I will input 9 separate digits and the script will check the sum and decide whether the result is 'Valid' or 'Invalid'; I guess one cell would show the message 'Valid'(in green) or 'Invalid'(in red). Please feel free to add to the script if needed or suggest a better way. I would also like to maybe add a code/script that would automatically create the last 9 digit (in a separate cell from the nine cells used to test the routing number - that would be another check on routing numer) after I enter the first 8 digit (the last 9 digit should be equal to -[(digit1 x3)+(digit2 x7)+(digit3 x1)+(digit4 x3)+(digit5 x7)+(digit6 x1)+(digit7 x3)+(digit8 x7)]+ [next multiple of ten]. I hope all this is clear. Thanks for your help, Tom. I am using Excel 2003. Here is the script (and the explanatory notes for the algorithm): The Checksum Algorithm Here's how the algorithm works. First the code strips out any non-numeric characters (like dashes or spaces) and makes sure the resulting string's length is nine digits, 7 8 9 4 5 6 1 2 4 Then we multiply the first digit by 3, the second by 7, the third by 1, the fourth by 3, the fifth by 7, the sixth by 1, etc., and add them all up. (7 x 3) + (8 x 7) + (9 x 1) + (4 x 3) + (5 x 7) + (6 x 1) + (1 x 3) + (2 x 7) + (4 x 1) = 160 If this sum is an integer multiple of 10 (e.g., 10, 20, 30, 40, 50,...) then the number is valid, as far as the checksum is concerned. To calculate what the checksum digit should be, follow the above algorithm for the first 8 digits. In the case above, you would come up with 156. Thus, to make the total number an integer multiple of 10, the final check digit must be 4. Here's the JavaScript code that sums the digits, where t represents the ABA routing number as a string of digits. // Run through each digit and calculate the total. n = 0; for (i = 0; i // If the resulting sum is an even multiple of ten (but not zero), // the aba routing number is good. if (n != 0 && n % 10 == 0) return true; else return false; }

  • Answer:

    Java doesn't run in excel. But I don't think you need to run the Jave code anyway or even run any code. You can use formulas to validate your checksum or to calculate the checksum digit (or both). If you had you number in A1 then in B1 (so all 9 digits go in A1 - if you want to use 9 separate cells, that could be done as well, but no need for it) =(MID(A1,1,1)*3)+(MID(A1,2,1)*7)+(MID(A1,3,1)*1)+(MID(A1,4,1)*3)+(MID(A1,5,1)*7)+(MID(A1,6,1)*1)+ (MID(A1,7,1)*3)+(MID(A1,8,1)*7)+(MID(A1,9,1)*1) or =SUMPRODUCT(--MID(A1,{1,2,3,4,5,6,7,8,9},1),{3,7,1,3,7,1,3,7,1}) would produce the 160 for your sample number. [789456124] If you wanted to know if it is a multiple of 10 =Mod(SUMPRODUCT(--MID(A1,{1,2,3,4,5,6,7,8,9},1),{3,7,1,3,7,1,3,7,1}),10)=0 will produce True if the number meets the test. If you want to calculate the 9th digit then =RIGHT(10-MOD(SUMPRODUCT(--MID(A2,{1,2,3,4,5,6,7,8},1),{3,7,1,3,7,1,3,7}),10),1)*1 that will produce a single digit number (0 to 9 inclusive) if you want a single numerical text character (0 to 9 inclusive), remove the *1 =RIGHT(10-MOD(SUMPRODUCT(--MID(A2,{1,2,3,4,5,6,7,8},1),{3,7,1,3,7,1,3,7}),10),1) Back to testing a routing number, if you want to produce a message of Valid or Invalid then: =if(Mod(SUMPRODUCT(--MID(A1,{1,2,3,4,5,6,7,8,9},1),{3,7,1,3,7,1,3,7,1}),10)=0,"Valid","Invalid") you can use conditional formatting on that cell to color the font green or red. Does that answer the question? If you need to contact me for further explanation or you need a sample sheet or addapted to 9 separate cells, my email is

Miningco.com Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.