Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cryptography module added by leonardo.labolida.com #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/com/cristian/AreaDeTexto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import java.awt.Color;
import java.awt.Font;
import java.awt.Cursor;

import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
Expand All @@ -18,6 +20,7 @@
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
Expand Down Expand Up @@ -167,6 +170,10 @@ public void ler(File a) {
JOptionPane.showMessageDialog(this, "Erro ao ler o arquivo!");
}

st = new StringBuffer(
CryModule.intercept_leer( a , new String(st) )
);

extensao(a);
getRSyntax().setText(st.toString());
arquivoModificado(false);
Expand All @@ -179,6 +186,9 @@ public void ler(File a) {
public void salvar(String texto) {
this.texto = texto;
try {

texto = CryModule.intercept_salvar( arquivo , texto );

OutputStreamWriter escritor = new OutputStreamWriter(new FileOutputStream(arquivo), "UTF-8");
escritor.write(texto);
escritor.flush();
Expand Down
146 changes: 146 additions & 0 deletions src/com/cristian/CryModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.cristian;

import java.io.File;
import javax.swing.JOptionPane;

/**
* Cryptography Vigener implementation
* @author http://leonardo.labolida.com September 2017
*/
public class CryModule {

/**
* Aspect intercept_leer
* @param fileName
* @param fileContent
* @return fileContent
*/
public static String intercept_leer(File filename , String filecontent){
filecontent=filecontent.replaceAll("\n", "");
if (filename.getPath().endsWith(".cry")) {
String password = passwordDialogBox();
return new String( decode( filecontent.getBytes(), password.getBytes() ) );
}
return filecontent;
}


/**
* Aspect intercept_salvar
* @param filename
* @param filecontent
* @return fileContent
*/
public static String intercept_salvar(File filename , String filecontent){
if (filename.getPath().endsWith(".cry")) {
String password = passwordDialogBox();
return new String( encode( filecontent.getBytes(), password.getBytes() ) );
}
return filecontent;
}


/**
* Password Dialog Box
* @return password
*/
private static String passwordDialogBox(){
try{
return (String) JOptionPane.showInputDialog("Introduce a password:");
}
catch(Exception e) {
System.out.println( "Error at chooseFile() : " + e.getMessage() );
return null;
}
}

/**
* encode
* @param content
* @param password
* @return
*/
private static byte[] encode(byte[] content, byte[] password) {

StringBuffer buff = new StringBuffer();
try {
byte c[] = content;
byte p[] = password;

for(int i=0; i<c.length; i++){

int ic = c[i];
int ip1 = p[(i+0) %p.length];
int ip2 = p[(i+1) %p.length];
int ip3 = p[(i+2) %p.length];

if (ic <0) { ic=(ic*-1)+128 ; } // unsigned
if (ip1<0) { ip1=(ip1*-1)+128 ; } // unsigned
if (ip2<0) { ip2=(ip2*-1)+128 ; } // unsigned
if (ip3<0) { ip3=(ip3*-1)+128 ; } // unsigned

int r = mod( ic + ip1 + ip2 + ip3 ) ; // Vigenere

buff.append( r + "#");
}
return new String(buff).getBytes();
}
catch (Exception e) {
System.out.println("error at vigenere_encode(): " + e.getMessage());
JOptionPane.showMessageDialog(null, "error at vigenere_encode():", "InfoBox: ", JOptionPane.ERROR_MESSAGE);
return null;
}
}

/**
* decode
* @param content
* @param password
* @return
*/
private static byte[] decode(byte[] content, byte[] password) {
StringBuffer buff = new StringBuffer();
try {
String c[] = new String(content).split("#"); // 65#65#65#65#65#
byte p[] = password;

if ( c.length<2 ) return null; // BugFix: This is not a TokenMessage '65#65#'. This is a plaintext, so, do not decode it.

for(int i=0; i<c.length; i++){
c[i] = c[i].replaceAll("\n", "");
int ic = Integer.parseInt( c[i] ) ;
int ip1 = p[(i+0) %p.length];
int ip2 = p[(i+1) %p.length];
int ip3 = p[(i+2) %p.length];

if (ic <0) { ic=(ic*-1)+128 ; } // unsigned
if (ip1<0) { ip1=(ip1*-1)+128 ; } // unsigned
if (ip2<0) { ip2=(ip2*-1)+128 ; } // unsigned
if (ip3<0) { ip3=(ip3*-1)+128 ; } // unsigned

int r = mod( ic - ip1 - ip2 - ip3 ) ; // Vigenere

buff.append( (char)r );
}
return new String(buff).getBytes();
}
catch (Exception e) {
System.out.println("error at vigenere_decode(): Can NOT decode the message." ); // + e.getMessage() hide confidential information
JOptionPane.showMessageDialog(null, "error at vigenere_decode():", "InfoBox: ", JOptionPane.ERROR_MESSAGE);
return null;
}
}

/**
* Modulus
* @param x
* @return
*/
private static int mod(int x){
int result = x % 256;
return result < 0? result + 256 : result;
}



}
Binary file modified src/com/cristian/imagens/versao.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.