Android Question Encrypt using Vigenere cipher

sanjibnanda

Active Member
Licensed User
Longtime User
hello,
in one of my project i need to use Vigenere cipher. How can i convert it b4a equivalent, your help will be appreciated.

here is its java equivalent

B4X:
public class VigenereCipher {
    /**
     * Encrypt using Vigenere cipher
     * @param s open text
     * @param key key phrase (only capital letters)
     * @return ciphertext (only capital letters)
     */
    public static String encipher(String s, String key){
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < s.length(); i ++){
            if(s.charAt(i) < 65 || s.charAt(i) > 90){ //ASCII character (capital letter)
                throw new IllegalArgumentException("" +
                        "Open text must contain only capital letters");
            }
            //add shift modularly
            char encyphered = s.charAt(i) + getShift(key, i) > 90 ? (char)((s.charAt(i) + getShift(key, i)) - 26) : (char)(s.charAt(i) + getShift(key, i));
            builder.append(encyphered);
        }
        return builder.toString();
    }
    /**
     * Decrypt using Vigenere cipher
     * @param s cipher text (only capital letters)
     * @param key key phrase (only capital letters)
     * @return open text
     */
    public static String decipher(String s, String key){
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < s.length(); i ++){
            if(s.charAt(i) < 65 || s.charAt(i) > 90){ //ASCII character (capital letter)
                throw new IllegalArgumentException("" +
                        "Ciphertext must contain only capital letters");
            }
            //subtract shift modularly
            char decyphered = s.charAt(i) - getShift(key, i) < 65 ? (char)((s.charAt(i) - getShift(key, i)) + 26) : (char)(s.charAt(i) - getShift(key, i));
            builder.append(decyphered);
        }
        return builder.toString();
    }
    /**
     * Get shift
     * @param key key phrase
     * @param i position in the text
     * @return shift
     */
    private static int getShift(String key, int i) {
            if(key.charAt(i % key.length()) < 65 || key.charAt(i % key.length()) > 90){
                throw new IllegalArgumentException("" +
                        "Key phrase must contain only capital letters");
            }
        return ((int)key.charAt(i % key.length())) - 65;
    }
  
    public static void main(String[] args){
        String text = "ATTACKATDAWN";
        String key = "CAT";
        String enciphered = encipher(text, key);
        System.out.println(enciphered);
        System.out.println(decipher(enciphered, key));
    }
}
 

Jaames

Active Member
Licensed User
Longtime User
There you go
Attached is example and simple java library which contains only 2 method needed
Vigenere.decipher and Vigenere.encipher

How to use:
Copy both VignereCipher.jar and VignereCipher.xml to your additional library folder, add it as a reference , and use it like in attached example:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Vigenere As VigenereCipher
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Log(Vigenere.decipher("BASICFORANDROID","MYSECRET"))' decript the "BASICFORANDROID" using "MYSECRET" as a  key
    Log(Vigenere.encipher("PCAEAOKYOPLNMRZ","MYSECRET"))' encript decripted string back using the same key
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

Attachments

  • Vigenere.zip
    329.6 KB · Views: 234
Last edited:
Upvote 0

sanjibnanda

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim salt As String
   salt ="MySecretPhrase"
End Sub
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
  Dim Vigenere As VigenereCipher
End Sub
Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   salt.Trim
   salt.ToUpperCase
   Log(salt.ToUpperCase)
  Log(Vigenere.encipher("BASICFORANDROID",salt))' encript decripted string back using the same key
   Log(Vigenere.decipher("SAFLQRYVYTHEFIW",salt))' decript the "BASICFORANDROID" using "SEECRET" as a  key


why this give error : java.lang.IllegalArgumentException: Key phrase must contain only capital letters
at jamessoft.vigenerecipher.VigenereCipher.getShift(VigenereCipher.java:60)
 
Upvote 0
Top