Pokazywanie postów oznaczonych etykietą kodowanie. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą kodowanie. Pokaż wszystkie posty

24 lipca 2009

kodowanie i dekodowanie znaków Base64[JAVA]

Poniższa klasa koduje i dekoduje Stringa algorytmem Base64:

package webster.jdownloader;

/**
* A Base64 Encoder/Decoder.
*
* <p>
* This class is used to encode and decode data in Base64 
format as described in RFC 1521.
*
* <p>
* This is "Open Source" software and released under
 the <a href="http://www.gnu.org/licenses/lgpl.html">
GNU/LGPL</a> license.<br>
* It is provided "as is" without warranty of any kind.<br>
* Copyright 2003: Christian d'Heureuse, 
Inventec Informatik AG, Switzerland.<br>
* Home page: <a 
href="http://www.source-code.biz">www.source-code.biz</a><br>
*
* <p>
* Version history:<br>
* 2003-07-22 Christian d'Heureuse (chdh): Module created.<br>
* 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br>
* 2006-11-21 chdh:<br>
*  &nbsp; Method encode(String) renamed to encodeString(String).<br>
*  &nbsp; Method decode(String) renamed to decodeString(String).<br>
*  &nbsp; New method encode(byte[],int) added.<br>
*  &nbsp; New method decode(String) added.<br>
*/

public class Base64 {

// Mapping table from 6-bit nibbles to Base64 characters.
private static char[]    map1 = new char[64];
static {
int i=0;
for (char c='A'; c<='Z'; c++) map1[i++] = c;
for (char c='a'; c<='z'; c++) map1[i++] = c;
for (char c='0'; c<='9'; c++) map1[i++] = c;
map1[i++] = '+'; map1[i++] = '/'; }

// Mapping table from Base64 characters to 6-bit nibbles.
private static byte[]    map2 = new byte[128];
static {
for (int i=0; i<map2.length; i++) map2[i] = -1;
for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }

/**
* Encodes a string into Base64 format.
* No blanks or line breaks are inserted.
* @param s  a String to be encoded.
* @return   A String with the Base64 encoded data.
*/

30 sierpnia 2007

Kodowanie ciągu stringa[JAVA]

Poniżej znajduje się listing klasy:

//klasa kodująca stringa
public static String codeString(String tekst,String haslo_hash,boolean flaga) {
char tmp_chr = '\1';
int byte_out;
int byte_pass;
int byte_in;
int len_pass=haslo_hash.length();
for(int i=0;i < tekst.length();i++) {
byte_in=ord(tekst.charAt(i));
byte_pass=ord(haslo_hash.charAt(i%len_pass));
if (flaga) {
byte_out=(byte_in+byte_pass)%256;
}else {
byte_out=(256+(byte_in-byte_pass))%256;
}
tmp_chr+=chr(byte_out);
}
String tmp_str = "//";
tmp_str=tmp_str.valueOf(tmp_chr);
return tmp_str;
}

//funkcja zwracająca wartość integer kodu ascii
private static int ord(char c) {
return (int)c;
}
//funkcja zwracająca wartość znakową funkcji
private static char chr(int byte_out) {
return ((char)byte_out);
}

09 lipca 2007

Kodowanie ciągu znaków [DELPHI]

Funkcja:
function koduj(tekst: string; password: string; flag_code: boolean):string; // funkcja z trzema parametrami zwracająca string
var i, len_pass: integer;
tmp_str: string;
byte_in, byte_out, byte_pass: byte; //deklaracja zmiennych
begin
len_pass := Length(password); //długość klucza
for i:=1 to Length(tekst) do //pętla "obiegająca" kodowany tekst
begin
byte_in := Ord(tekst[i]); //pobranie kolejnej litery kodowanego tekstu i zamiana na bajt
byte_pass := Ord(password[i mod len_pass]); //pobranie kolejnej litery klucza i zamiana na bajt
if flag_code then //sprawdzenie flagi (czy ma być kodowanie czy dekodowanie)
byte_out := (byte_in + byte_pass) mod 256 //zakodowanie bajtu tekstu bajtem klucza i podzielenie tego modulo przez 256
else
byte_out := (256 + (byte_in - byte_pass)) mod 256; //rozkodowanie bajtu tekstu zakodowanego bajtem klucza...
tmp_str:= tmp_str + Chr(byte_out); //doklejenie roz/zakodowanego bajtu do zmiennej tymczasowej tmp_str (z zamianą zmiennej byte na char)
end;
result := tmp_str; //wyprowadzenie rezultatu funkcji
end;

Użycie funkcji:

Dekoduj:

edit1.Text:=koduj(edit2.Text,'haslo_do_szyfrowania',false);


Koduj:

edit2.Text:=koduj(edit1.Text,'haslo_do_szyfrowania',true);