Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Encyption Decryption


    Hello ,
    I am trying to write my own encryption - decryption method , and as I am new in programming I have some troubles based on the way I have to do this. I know it is possible with javascript but I want to learn if I can do it with php.
    What exactly I mean ...

    Supposly I have a form :
    Code:
    <form name="test" method="post" action="#">
    <textarea name="text" cols="50" rows="4" id="detail"></textarea>
    <input type="submit" name="Submit" value="Submit"> 
    </form>
    My idea is based on bit encryption method
    for example a= 0001 , b = 0010 etc .

    The current script works perfect if I 'll submit only 'a' or 'b' :

    Code:
    <?php
    
    $encrypt= $_POST['text'];
    $a='0001';
    $b='0010';
    
    if (  $encrypt =='a' ) {
    $encrypt= $a;
    }
    if (  $encrypt =='b' ) {
    $encrypt= $b;
    }
    
    echo $encrypt;
    
    echo '</br>';
    
    $decrypt = $encrypt;
    
    if ($decrypt=='0001'){
    $decrypt= 'a';
    }
    if ( $decrypt=='0010'){
    $decrypt= 'b' ;
    }
    
    echo $decrypt;
    
    ?>

    But if I will submit 'ab' it won't print '00010010' but 'ab'

    I am looking for a way to make this works even I will submit 'ab'

    Is that possible ????
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595
    This should get you started but you will need to figure out the 2nd set of 'if's when 'ab' is specified.
    PHP Code:
    $bits="";
    if (  
    $encrypt =='a' ) {
       
    $bits.= "$a";
    }
    if (  
    $encrypt =='b' ) {
       
    $bits.= "$b";
    }
    echo 
    $bits;
    $decrypt="$bits"
    Note the use of [ PHP ] tags rather than [ CODE ] tags for PHP.

    P.S. This is not encryption so you might need to better explain what you are trying to accomplish.
    Last edited by gw1500se; November 16th, 2012 at 01:20 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    This is not encryption by any stretch of the word, so if you're using this for security then you're doing it wrong.

    If you mean "encoding" or some other word then we can just move on.

    Your code checks for 'a' and it checks for 'b'. The whole string is checked. Since your whole string is 'ab' and not 'a' nor 'b', none of your checks succeed.

    PHP has two functions that make this task trivial:

    decbin - gets the binary value of a decimal number
    ord - gets the decimal number of a character on the ascii chart.


    PHP Code:
    $input "abc";
    foreach ( 
    str_split($input) as $letter ) {
      echo 
    decbin(ord($letter));

    This translates any input into a binary string. The opposite can be obtained with bindec() and chr() (as well as another argument to str_split).

    If you were looking to make your own custom translations for each letter, take out the inner line of my loop and replace it with a switch to print the right data.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Hmmm.... as I understood , the problem is unpossible to be solved in this way . My thought was to try this without using php functions or extentions in order to control the result . But maybe the only way is to write php extention ...

    Thanks for reply
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13
    Originally Posted by ManiacDan
    This is not encryption by any stretch of the word, so if you're using this for security then you're doing it wrong.
    This is off topic, but isn't this similar to using a Caesar cipher? Why wouldn't this be encryption? Maybe not good for security for a site, but if you were tasked with writing your own encryption scheme for a cryptography class ....
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by ManiacDan
    This is not encryption by any stretch of the word, so if you're using this for security then you're doing it wrong.
    If you mean "encoding" or some other word then we can just move on.
    Excuse me for the mistake , I didn't knew that the words 'encryption' and 'encoding' have different meaning.



    Originally Posted by msteudel
    This is off topic, but isn't this similar to using a Caesar cipher? Why wouldn't this be encryption? Maybe not good for security for a site, but if you were tasked with writing your own encryption scheme for a cryptography class ....
    I am exactly interesting in cryptography , and especially how we can use it in php programming. The main reason is;


    If we will use a php function to encode a text , I agree that it is unsecure because I suppose first of all it is known and recognizable. But if we'll write our own cryptographic method to encode a text , it maybe will make it more secure . The example of a=0001 and b= 0010 is very simple to make it easier to present what I mean . The cryptographic method will be more complicated .

    php has the benefit that browsers can't read it like javascript . I 'm not about to write a cryptographyc method for ''national security'' , I 'm just trying to learn how we can use cryptography in php by html form , but without using known methods and functions.

    The main problem is that if I had to use only 'a' and 'b' and 'ab' and 'ba' as possibilities to encode it would be much easyer , but the possible compinations of letters and words in a sentence, makes it unpossible do define them .

    In this frames I was just wondering if there is a possibility to separate the letters of input words in order to encode them ..
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13
    Doing things for the sake of learning is great. But there's a saying, "security through obscurity is not security". Meaning just because you came up with your own encryption scheme that nobody has every heard of doesn't mean that somebody won't be able to hack it quickly. It's better to use something that everyone has heard of and agrees is secure.

    Here's an interesting article on password enecryption:

    http://codahale.com/how-to-safely-store-a-password/
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by msteudel

    Here's an interesting article on password enecryption:..
    It is very easy to hash a password using for example md5() but if we 'll use md5() for text , how we 'll decode it ????? That's the problem of hash method (If I don't make mistake) . Maybe it is 100% secure but it's also unreadable ..
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13
    Originally Posted by MaryG
    It is very easy to hash a password using for example md5() but if we 'll use md5() for text , how we 'll decode it ????? That's the problem of hash method (If I don't make mistake) . Maybe it is 100% secure but it's also unreadable ..
    See this post:
    http://stackoverflow.com/a/647651
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13
    Depending on what you are using md5 has for, if you are using it for passwords it's not secure. It's very easy to brute force md5 hash.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595
    Apples and oranges. MD5 is not designed to be decoded so it is unreadable on purpose. If you want to encode/decode things the you need to look for a different tool designed for that purpose such as key pair cryptography.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    I will be more specific . The problem is not such complicated for numbers. So , let's suppose I want to encode-decode a telephone number . I can do this in a way like this :

    PHP Code:
    <?php

    $tel
    $_POST['text'];
    $encode= ( $tel *236352413153374 ) + 42847625224 ;

    if ( 
    $tel ) {
    echo 
    $encode ;
    }

    echo 
    '</br>' ;

    $decode = ( $encode 42847625224 ) / 236352413153374 
    echo 
    $decode ;

    ?>

    <form name="test" method="post" action="#">
    <textarea name="text" cols="50" rows="4" id="detail"></textarea>
    <input type="submit" name="Submit" value="Submit"> 
    </form>
    The result for example of number '263524133'
    is 6.2284564758744E+22
    ( I suppose that it is not really easy to break it .. )



    But as letters are not numbers , it makes it complicated or as I understood unpossible doing sth similar with text ....
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595
    Originally Posted by MaryG
    I am exactly interesting in cryptography , and especially how we can use it in php programming.
    Now you are back to encoding/decoding. Obfuscation is a different topic yet again but you are being successful with me.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  26. #14
  27. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Ok, lots of things here...

    This is off topic, but isn't this similar to using a Caesar cipher?
    This is, at the very best, a substitution cypher. It's no different than rot13 and falls into the most basic cypher case. Sure, my mom couldn't break it, but it would take me 10 minutes. Not good enough to qualify as "encryption," especially on a programming forum. When we talk about encryption here, we mean for real. Speaking of which...

    If we will use a php function to encode a text , I agree that it is unsecure because I suppose first of all it is known and recognizable.
    Not quite sure what you're trying to say here. If you use a real encryption library like mcrypt your data is as secure as you can get it. People know exactly how those encryption routines work, that's why they're so strong. I know the math, and I still can't break it, that's the whole point.

    I 'm just trying to learn how we can use cryptography in php by html form , but without using known methods and functions.
    Again, not sure what this is supposed to mean, but if you're trying to secure communication between your user's browser and your server, install an SSL certificate and that's all. You don't need to encrypt anything, you just need to use https.


    In this frames I was just wondering if there is a possibility to separate the letters of input words in order to encode them ..
    My code did this, and my post included instructions on how to use it.

    It is very easy to hash a password using for example md5() but if we 'll use md5() for text , how we 'll decode it ????? That's the problem of hash method (If I don't make mistake) . Maybe it is 100% secure but it's also unreadable ..
    A hash is one-way. Encryption is two-way and secret. Encoding is two-way and not secret. These distinctions are very important.

    MaryG, all you're doing is obfuscating your data. You're changing it in some way that only you know how, and then changing it back. This is not encryption. Encryption is so much more than that. If you want to simply mix up and shuffle your data, that's fine, but do not believe you're doing anything about its security in any meaningful way.
    Last edited by ManiacDan; November 16th, 2012 at 03:37 PM.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595
    On further reflection I guess technically you are encrypting rather than encoding. However, what is obscure is what your point is with respect to the goal. Your technique could work for text if you were to encode it first so that your string was only numbers. But you would also need to break it up such that when you encrypt, the resulting number does not overflow.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo