<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kriptic;

import java.util.ArrayList;

/**
 *
 * @author akhfa
 */
public class Enkripsi {
    String text;
    String kunci;
    
    public Enkripsi(String _text, String _kunci)
    {
        this.text = _text;
        this.kunci = _kunci;
    }
    
    public void setPlain(String _text)
    {
        this.text = _text;
    }
    
    public void setKunci(String _kunci)
    {
        this.kunci = _kunci;
    }
    
    public String vigenere(boolean encrypt)
    {
        char [] huruf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        
        // Hilangkan spasi dan ubah jadi uppercase
        this.text = this.text.toUpperCase();
        this.kunci = this.kunci.toUpperCase();
        
        char [] text_char = this.text.toCharArray();
        char [] kunci_char = this.kunci.toCharArray();
        StringBuilder result = new StringBuilder(text_char.length);
        char result_char = '\0';
        
        int indexKunci = 0;
        for(int i = 0; i < text_char.length; i++)
        {
            if(getIndexOf(huruf, text_char[i]) != -1)
            {
                System.out.println("getIndex " + getIndexOf(huruf, text_char[i]));
                if(encrypt)
                    result_char = enc(huruf, text_char[i], kunci_char[indexKunci]);
                else
                    result_char = dec(huruf, text_char[i], kunci_char[indexKunci]);
                
                if(indexKunci == kunci_char.length - 1)
                    indexKunci = 0;
                else
                    indexKunci++;
                result.append(result_char);
            }
            else
                result.append(text_char[i]);
            
        }
        return result.toString();
    }
    
    private char enc(char [] huruf,char plain_char, char kunci_char)
    {
        int plain_int = getIndexOf(huruf, plain_char);
        int kunci_int = getIndexOf(huruf, kunci_char);
        int chiper_int = (plain_int + kunci_int) % 26;
        return huruf[chiper_int];
    }
    
    private char dec(char [] huruf,char chiper_char, char kunci_char)
    {
        System.out.println("chiper_char " + chiper_char);
        int chiper_int = getIndexOf(huruf, chiper_char);
        System.out.println("chiper_int " + chiper_int);
        int kunci_int = getIndexOf(huruf, kunci_char);
        System.out.println("kunci_int " + kunci_int);
        int plain_int = (chiper_int - kunci_int) % 26;
        if(plain_int < 0)
            plain_int += 26;
        System.out.println("plain_int " + huruf[plain_int]);
        return huruf[plain_int];
    }
    
    /**
     * Mencari index karakter a pada array of karakter daftarKarakter
     * @param daftarKarakter
     * @param a
     * @return 
     */
    private int getIndexOf(char [] daftarKarakter, char a)
    {
        int result = -1;
        for(int i = 0; i < daftarKarakter.length; i++)
        {
            if(daftarKarakter[i] == a)
            {
                result = i;
            }
        }
        return result;
    }
    
    public String vigenere_ascii(String plain)
    {
        <commit_msg>fixing banyaknya huruf dalam enc dec<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kriptic;

import java.util.ArrayList;

/**
 *
 * @author akhfa
 */
public class Enkripsi {
    String text;
    String kunci;
    
    public Enkripsi(String _text, String _kunci)
    {
        this.text = _text;
        this.kunci = _kunci;
    }
    
    public void setPlain(String _text)
    {
        this.text = _text;
    }
    
    public void setKunci(String _kunci)
    {
        this.kunci = _kunci;
    }
    
    public String vigenere(boolean encrypt)
    {
        char [] huruf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        
        // Hilangkan spasi dan ubah jadi uppercase
        this.text = this.text.toUpperCase();
        this.kunci = this.kunci.toUpperCase();
        
        char [] text_char = this.text.toCharArray();
        char [] kunci_char = this.kunci.toCharArray();
        StringBuilder result = new StringBuilder(text_char.length);
        char result_char = '\0';
        
        int indexKunci = 0;
        for(int i = 0; i < text_char.length; i++)
        {
            if(getIndexOf(huruf, text_char[i]) != -1)
            {
                System.out.println("getIndex " + getIndexOf(huruf, text_char[i]));
                if(encrypt)
                    result_char = enc(huruf, text_char[i], kunci_char[indexKunci]);
                else
                    result_char = dec(huruf, text_char[i], kunci_char[indexKunci]);
                
                if(indexKunci == kunci_char.length - 1)
                    indexKunci = 0;
                else
                    indexKunci++;
                result.append(result_char);
            }
            else
                result.append(text_char[i]);
            
        }
        return result.toString();
    }
    
    private char enc(char [] huruf,char plain_char, char kunci_char)
    {
        int plain_int = getIndexOf(huruf, plain_char);
        int kunci_int = getIndexOf(huruf, kunci_char);
        int chiper_int = (plain_int + kunci_int) % huruf.length;
        return huruf[chiper_int];
    }
    
    private char dec(char [] huruf,char chiper_char, char kunci_char)
    {
        int chiper_int = getIndexOf(huruf, chiper_char);
        int kunci_int = getIndexOf(huruf, kunci_char);
        int plain_int = (chiper_int - kunci_int) % huruf.length;
        if(plain_int < 0)
            plain_int += huruf.length;
        return huruf[plain_int];
    }
    
    /**
     * Mencari index karakter a pada array of karakter daftarKarakter
     * @param daftarKarakter
     * @param a
     * @return 
     */
    private int getIndexOf(char [] daftarKarakter, char a)
    {
        int result = -1;
        for(int i = 0; i < daftarKarakter.length; i++)
        {
            if(daftarKarakter[i] == a)
            {
                result = i;
            }
        }
        return result;
    }
    
    public String vigenere_ascii(String plain)
    {
        
{'fraction_non_alphanumeric': 0.09962698165993161, 'fraction_numerical': 0.003730183400683867, 'mean_word_length': 1.751175716117999, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6580902', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2031, 'n_words': 443}