Array Offset Encryption - php

Right now I am developing a cryptography app and I have a problem with this one
$stralphabet=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
As you can see the string above is a string of alphabet. I am making a caesar shift encryption, the algorithm is simple.
Count the number of string and then substitute each letter based from the position, so for example:
string is "AB" after encryption it would be "CD"
The problem is if I have the string YZ" it gives me an error
Notice: Undefined offset: 26 in C:\xampp\htdocs\cryptographer\encrypt.php on line 19
Notice: Undefined offset: 27 in C:\xampp\htdocs\cryptographer\encrypt.php on line 19
Can you help me with this one?

You apparently are accessing $stralphabel without checking for edge case (i.e. $stralp[$i+1]). You should add some checks or use modulo operator, so instead of just:
$stralp[$i+1]
you'd have
$stralp[($i+1) % XXX]
where XXX is number of entries in your stralp array.
PS: this is not even close to cryptography.

Your array has 26 letters indexed from 0 to 25, so something is wrong with your cycle end condition.

Related

How to get int to dec and dec to hex using php?

This is hex c4 and dec is 196
When string length is 50 one of my project returning \xc4
If length is 51 then getting hex \xc5
length 55 = \xc9
length 56 = \xca
length 61 = \xcf
length 62 = \xd0
Continuously \xd9 then \xda to continuously \xdf then changing xd to xe again from \xe0 to \xe9 and \xea to \xef
How it possible to get length 50 to hex c4 or dec 196?
I can convert dec to bin and bin to hex using below code:
$binary = decbin(50); //dec to binary
echo dechex(bindec($binary)); //binary to hex
I think you're possibly overthinking this: forget about the hexadecimal for a while, and note that you've already discovered that you need to convert 50 to 196. The simplest way to do that is simply to add 146. So dechex(50 + 146) gives you the value you need.
The rest of the sequence is, as I think you've already worked out, just hexadecimal values going upwards from there, so from what you've said, there's nothing more clever to do than add that offset to each value. Why that offset? I have no idea, because I have no idea what you're using this for.
Meanwhile, converting to binary and back isn't doing anything - it's like writing a word backwards, and then writing it back the right way again. You can just pass the number into dechex directly.

Incorrect PHP calculation

Can someone give me an explanation on why this is happening in PHP:
echo (0.29*100)%100 // result 28
It's probably very simple but logically I do not see any explanation. Probably how PHP works in the background.
I was trying to get the first two decimal positions of a number when I ran into this case. Result should be 29 naturally.
If I round the multiplication the result is fine:
echo (round(0.29*100))%100 // result 29
If you run that code
echo (0.29*100)%100;
in in PHP8.1.1 the error message gives you a clue
PHP 8.1.1
Deprecated: Implicit conversion from float 28.999999999999996 to int loses precision in D:\PHP-SOURCE\Testing\tst.php on line 14
Call Stack:
0.0001 393688 1. {main}() D:\PHP-SOURCE\Testing\tst.php:0
28

Notice: Use of undefined constant ‏ - assumed '‏' in C:\xampp\htdocs\aa.php on line 5 vs Notice a non well former numeric value

I tried running this simple php code in 2 different machines and i get different results for some reason.
the code:
<?php
$a = 5;
$b = "22ff67";
$s = $a - $b;
echo $s;‏
?>
in machine 1 i get the error:
Notice a non well former numeric value encountered in .... on line 4
17
and in machine 2 i get:
-17
Notice: Use of undefined constant ‏ - assumed '‏' in
C:\xampp\htdocs\aa.php on line 5
i run the code using XAMPP apache server...
what could be the reason for the different results?
the output is -17 because it tries to subtract 22 from 5 but because 22 is part of a string it gives the error? (it takes only 22 because it's the number before the first letter in the string (in strings which has signs other than '.', 'e','E' it takes the first number up to a sign other than those)
Thanks.

unserialize(): Error at offset 181 of 849 bytes

This works -
$serialized = 'a:23:{s:6:"fields";a:5:{s:21:"display_name_creditor";s:1:"1";s:24:"display_name_constituent";s:1:"1";s:14:"email_creditor";s:1:"1";s:14:"phone_creditor";s:1:"1";s:12:"total_amount";s:1:"1";}s:5:"id_op";s:2:"in";s:8:"id_value";a:0:{}s:21:"receive_date_relative";s:1:"0";s:17:"receive_date_from";s:0:"";s:15:"receive_date_to";s:0:"";s:25:"contribution_status_id_op";s:2:"in";s:28:"contribution_status_id_value";a:1:{i:0;s:1:"1";}s:16:"total_amount_min";s:0:"";s:16:"total_amount_max";s:0:"";s:15:"total_amount_op";s:3:"lte";s:18:"total_amount_value";s:0:"";s:6:"gid_op";s:2:"in";s:9:"gid_value";a:0:{}s:8:"tagid_op";s:2:"in";s:11:"tagid_value";a:0:{}s:11:"description";s:20:"Soft Credit details.";s:13:"email_subject";s:0:"";s:8:"email_to";s:0:"";s:8:"email_cc";s:0:"";s:10:"permission";s:21:"access CiviContribute";s:6:"groups";s:0:"";s:9:"domain_id";i:1;}';
$result = unserialize($serialized);
This does not -
$serialized = 'a:23:{s:6:"fields";a:5:{s:21:"display_name_creditor";s:1:"1";s:24:"display_name_constituent";s:1:"1";s:14:"email_creditor";s:1:"1";s:14:"phone_creditor";s:1:"1";s:12:"amount";s:1:"1";}s:5:"id_op";s:2:"in";s:8:"id_value";a:0:{}s:21:"receive_date_relative";s:1:"0";s:17:"receive_date_from";s:0:"";s:15:"receive_date_to";s:0:"";s:25:"contribution_status_id_op";s:2:"in";s:28:"contribution_status_id_value";a:1:{i:0;s:1:"1";}s:16:"total_amount_min";s:0:"";s:16:"total_amount_max";s:0:"";s:15:"total_amount_op";s:3:"lte";s:18:"total_amount_value";s:0:"";s:6:"gid_op";s:2:"in";s:9:"gid_value";a:0:{}s:8:"tagid_op";s:2:"in";s:11:"tagid_value";a:0:{}s:11:"description";s:20:"Soft Credit details.";s:13:"email_subject";s:0:"";s:8:"email_to";s:0:"";s:8:"email_cc";s:0:"";s:10:"permission";s:21:"access CiviContribute";s:6:"groups";s:0:"";s:9:"domain_id";i:1;}'
I have just changed the total_amount field to amount and I receive this error --
unserialize(): Error at offset 181 of 849 bytes
I've checked the solution provided at unserialize() [function.unserialize]: Error at offset & http://davidwalsh.name/php-serialize-unserialize-issues, but nothing worked.
Also tried using base64_encode/decode before serialize/unserialize
Any hints ?
The second one is invalid the reason being that field s:12:"amount" states that this value is a string of length 12. However amount only has 6 bytes. so you want to use s:6:"amount"
That being said its probably easier to unserialize then make a change then serialize the new value.

How to make PHP error when using an non-integer key for array-accessing a string

In PHP, you can access characters in a string with the array syntax:
$foo = 'abc';
echo $foo[2]; // echos 'c'
I recently spent way too long debugging why $foo['id'] wasn't giving me the expected result. It turned out that $foo was a string instead of an associative array. PHP seemed to be casting 'id' to the integer 0, without giving any notice:
$foo = 'abc';
echo $foo['id']; // echos 'a', without notice
PHP throws a nice warning when you do this with real arrays:
$foo = array('a', 'b', 'c');
$echo $foo['id']; // Notice: Undefined index: id in php shell code on line 1
How can I make (or why can't) PHP throw an "Undefined index" notice instead of casting a string index to 0?
Unfortunately, there's nothing you can do, short of patching PHP.
However, if you do want to patch PHP, this is a possible patch (against trunk):
Index: Zend/zend_execute.c
===================================================================
--- Zend/zend_execute.c (revision 316974)
+++ Zend/zend_execute.c (working copy)
## -1268,7 +1268,7 ##
case IS_DOUBLE:
case IS_NULL:
case IS_BOOL:
- /* do nothing */
+ zend_error(E_NOTICE, "String offset is not an integer");
break;
default:
zend_error(E_WARNING, "Illegal offset type");
Then:
$ ~/php/php-t/bin/php -d error_reporting=-1 -r '$a="foo"; echo $a["bar"];'
Notice: String offset is not an integer in Command line code on line 1
f
because [0] is ALWAYS the first character, if it's a string. You can check array with
if (is_array($array_or_string)){
//it's an arra
}
This is a "feature":
Writing to an out of range offset pads the string with spaces.
Non-integer types are converted to integer. Illegal offset type emits
E_NOTICE. Negative offset emits E_NOTICE in write but reads empty
string. Only the first character of an assigned string is used.
Assigning empty string assigns NULL byte.
And the string 'id' converts to integer zero:
The value is given by the initial portion of the string. If the string
starts with valid numeric data, this will be the value used.
Otherwise, the value will be 0 (zero). Valid numeric data is an
optional sign, followed by one or more digits (optionally containing a
decimal point), followed by an optional exponent. The exponent is an
'e' or 'E' followed by one or more digits.
You can't change this. Sorry!
PHP 5.4 now shows an error!
$foo = 'abc';
echo $foo['id'];
PHP Warning: Illegal string offset 'id' in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0

Categories