Can this Encryption/Decryption Code work on Windows phone 7 - encryption

Hi
Have use this code before. Find it useful on Web. But Dont know how to convert it for wp7. Will some1 take a shot at it?
script language="JavaScript"
len=0;
function CalcKey()
{
len=0;
var temp=document.Encrypt.Key.value;
for(i=0;i<temp.length;i++)
{
len=len+temp.charCodeAt(i);
}
if(len==0)
{
alert('Please Enter the appropriate Key');
document.Encrypt.Key.focus();
}
return len;
}
function Encryption()
{
CalcKey();
document.Encrypt.Encrypted.value="";
var txt=document.Encrypt.normal.value;
var net="";
var fin=0;
if(len>0)
{
if(txt.length>0)
{
for(i=0;i<txt.length;i++)
{
fin=txt.charCodeAt(i)+len;
if(fin>99)
{
net=net+fin;
}
else
{
net=net+'0'+fin;
}
}
document.Encrypt.Encrypted.value=net;
document.Encrypt.normal.value="";
}
else
{
alert('Please Enter the Text to be Encrypted');
document.Encrypt.normal.focus();
}
}
}
function Decryption()
{
var txt=document.Encrypt.Encrypted.value;
var j=3;
var temp1;
var res="";
CalcKey();
if(len>0)
{
if(txt.length>0)
{
for(i=0;i<txt.length;i+=3)
{
var temp=txt.substring(i,j);
temp1=(parseInt(temp)-len);
var t=unescape('%'+temp1.toString(16));
if(t=='%d' || t=='%a')
{
res=res+' ';
}
else
{
res=res+t
}
j+=3;
}
document.Encrypt.normal.value=res;
document.Encrypt.Encrypted.value="";
}
else
{
alert('Please Enter the Encrypted Text');
document.Encrypt.Encrypted.focus();
}
}
}

It looks like this code simply escapes/unescapes text from a form like "HelloWorld" to some kind of integer shifted by its current position.
You can definitely port this algorithm across to C# - you'll need to use
some kind of ((int)c).ToString() in order to write
Char.ParseInt to read bits of the string back in
Alternatively there are existing encryption methods you can use - http://robtiffany.com/windows-phone-7/dont-forget-to-encrypt-your-windows-phone-7-data

It seems that all you need is a simple cipher or form of symmetric encryption.
Have a look at the AesManaged encryption algorithm.

Related

Hiding fields in Quick View form with JS not working

I need to hide few fields in a Quick View Form based on an Option Set (Choice) selection in that Quick View form. However it is not working, so am sharing the code I used for the same here. In my code, I am trying to hide certain fields if the option selected is not equal to a particular value...
function hideFields(executionContext) {
var formContext = executionContext.getFormContext();
var quickViewControl = formContext.ui.quickForms.get("General");
if (quickViewControl != undefined) {
if (quickViewControl.isLoaded()) {
var orgtypevalue = quickViewControl.getControl("new_organizationtype").getValue();
if (orgtypevalue != 248870006) {
quickViewControl.getControl("new_recipienttype").setVisible(false);
quickViewControl.getControl("new_businesstype").setVisible(false);
quickViewControl.getControl("new_businesstypecode").setVisible(false);
quickViewControl.getControl("new_businesstypecharacteristicstypecode").setVisible(false);
return;
}
else {
// Wait for some time and check again
setTimeout(getAttributeValue, 10, executionContext);
}
}
else {
console.log("No data to display in the quick view control.");
return;
}
else {
quickViewControl.getControl("new_recipienttype").setVisible(true);
quickViewControl.getControl("new_businesstype").setVisible(true);
quickViewControl.getControl("new_businesstypecode").setVisible(true);
quickViewControl.getControl("new_businesstypecharacteristicstypecode").setVisible(true);
return;
}
}
}
I need to know where am I wrong. Any help will be appreciated.
Thanks!
you need to update the following
first if version 9 I am updating this
"var quickViewControl = formContext.ui.quickForms.get("General");"
to "var quickViewControl = formContext._ui._quickForms.get("General");"
&
"var orgtypevalue = quickViewControl.getControl("new_organizationtype").getValue();"
to
"var orgtypevalue = quickViewControl.getAttribute("new_organizationtype").getValue();"
& update else with message to wait and call the function again like this
"setTimeout(hideFields, 10, executionContext);"
and add else for the If of "quickViewControl != undefined"
Kindly find the updated code:
function hideFields(executionContext) {
var formContext = executionContext.getFormContext();
var quickViewControl = formContext._ui._quickForms.get("General");
if (quickViewControl != undefined) {
if (quickViewControl.isLoaded()) {
var orgtypevalue = quickViewControl.getAttribute("new_organizationtype").getValue();
if (orgtypevalue != 248870006) {
quickViewControl.getControl("new_recipienttype").setVisible(false);
quickViewControl.getControl("new_businesstype").setVisible(false);
quickViewControl.getControl("new_businesstypecode").setVisible(false);
quickViewControl.getControl("new_businesstypecharacteristicstypecode").setVisible(false);
return;
}
else {
// Wait for some time and check again
setTimeout(hideFields, 10, executionContext);
}
}
else {
//console.log("No data to display in the quick view control.");
//return;
setTimeout(hideFields, 10, executionContext);
}
}
}

TornadoFX:proper way to bind model

I was taking a look at this :
tornadofx
and tried to expand on it with database connection and little more options, (not all of them make sense, but its just playing in a sandbox).
Even though table can be directly edited and the data will persist in database, i did try to do edit through text fields too. actual table editing would happen through different view and not table itself, as i said its just example.
Database used is Jetbrains Exposed.
object Categories : IntIdTable() {
val name = varchar("name", 64).uniqueIndex()
val description = varchar("description", 128)
}
class Category(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Category>(Categories)
var name by Categories.name
var description by Categories.description
override fun toString(): String {
return "Category(name=\"$name\", description=\"$description\")"
}
}
now controller looks something like this, functions are just rudimentary and picked as an example.
typealias ModelToDirtyState = Map.Entry<CategoryModel, TableColumnDirtyState<CategoryModel>>
class CategoryModel() : ItemViewModel<Category>() {
val name: SimpleStringProperty = bind(Category::name)
val description: SimpleStringProperty = bind(Category::description)
}
class DBController : Controller() {
val categories: ObservableList<CategoryModel> by lazy {
transaction {
SchemaUtils.create(Categories)
Category.all().map {
CategoryModel().apply {
item = it
}
}.observable()
}
}
init {
Database.connect(
"jdbc:mysql://localhost:3306/test", driver = "com.mysql.cj.jdbc.Driver",
user = "test", password = "test"
)
TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
}
fun deleteCategory(model: CategoryModel) {
runAsync {
transaction {
model.item.delete()
}
}
categories.remove(model)
}
fun updateCategory(model: CategoryModel) {
transaction {
Categories.update {
model.commit()
}
}
}
fun commitDirty(modelDirtyMappings: Sequence<ModelToDirtyState>) {
transaction {
modelDirtyMappings.filter { it.value.isDirty }.forEach {
it.key.commit()
println(it.key)// commit value to database
it.value.commit() // clear dirty state
}
}
}
Just to quickly comment on controller, delete method works as "intended" however the update one does not, it does not work in sense that after using delete item is remove both from database and tableview(underlying list) itself, and when i do update its not, now i know the reason, i call remove manually on both database and list, now for update perhaps i could do change listener, or maybe tornadofx can do this for me, i just cant set it up to do it. Following code will make things clearer i think.
class CategoryEditor : View("Categories") {
val categoryModel: CategoryModel by inject()
val dbController: DBController by inject()
var categoryTable: TableViewEditModel<CategoryModel> by singleAssign()
var categories: ObservableList<CategoryModel> by singleAssign()
override val root = borderpane {
categories = dbController.categories
center = vbox {
buttonbar {
button("Commit") {
action {
dbController.commitDirty(categoryTable.items.asSequence())
}
}
button("Roll;back") {
action {
categoryTable.rollback()
}
}
// This model only works when i use categorytable.tableview.selected item, if i use categoryModel, list gets updated but not the view itself
// Question #1 how to use just categoryModel variable without need to use categorytable.tableview.selecteditem
button("Delete ") {
action {
val model = categoryTable.tableView.selectedItem
when (model) {
null -> return#action
else -> dbController.deleteCategory(model)
}
}
}
//And here no matter what i did i could not make the view update
button("Update") {
action {
when (categoryModel) {
null -> return#action
else -> dbController.updateCategory(categoryModel)
}
categoryTable.tableView.refresh()
}
}
}
tableview<CategoryModel> {
categoryTable = editModel
items = categories
enableCellEditing()
enableDirtyTracking()
onUserSelect() {
//open a dialog
}
//DOES WORK
categoryModel.rebindOnChange(this) { selectedItem ->
item = selectedItem?.item ?: CategoryModel().item
}
// Question #2. why bindSelected does not work, and i have to do it like above
//DOES NOT WORK
// bindSelected(categoryModel)
//
column("Name", CategoryModel::name).makeEditable()
column("Description", CategoryModel::description).makeEditable()
}
}
right = form {
fieldset {
field("Name") {
textfield(categoryModel.name)
}
}
fieldset {
field("Description") {
textfield(categoryModel.description)
}
}
button("ADD CATEGORY") {
action {
dbController.addCategory(categoryModel.name.value, categoryModel.description.value)
}
}
}
}
}
I apologize for huge amount of code, also in last code snipped i left questions in form of comments where i fail to achive desired results.
I am sure i am not properly binding code, i just dont see why, also i sometimes use one variable to update data, my declared one "categoryModel" and sometimes i use tableview.selecteditem, it just seems hacky and i cant seem to grasp way.
Thank you!

javascript validation for name,contact numbers

I have created 2 pages login and signup..On signup form I have used javascript for nonempty fields. Now I want to add validation for characters only, digits and date.
How to write these additional validations in my function. Please help. My code is:
<script type="text/javascript">
function Validateform() {
var Firstname = document.getElementById("txtfirst").value;
if (Firstname == "First Name" || Firstname == ""){
alert("Firstname must be filled out");
return false;
}
}
</script>
and I have called this function on onClientClick event of signup button.
Code to check alpha characters:
function onlyAlpha(inputtext){
var acceptedChars= /^[A-Za-z]+$/;
if(inputtext.value.match(acceptedChars)){
alert('ok');
return true;
} else {
alert('input alphabet characters only');
return false;
}
}
you can call this function on onblur on onClick with inputtext parameter as u wish to validate and the same way u can apply digits validation too.
There are n number of methods to validate name. Let me share my view
Method 1
First Name:
lname=document.getElementById('lname').value;
namelen=/^([a-zA-Z0-9]{5,15})+$/;
if(!namelen.test(lname))
{
alert('Enter a name');
return false;
}
else
{
document.getElementById('splname').innerHTML="";
}
returnValue;
}
Method 2 - To color the background
namelen=document.getElementById('lname').value.length;
lname=document.getElementById('lname').value;
if((namelen=<5 || namelen >=12) || !isNaN(lname))
{
document.getElementById('splname').innerHTML="Enter a name";
document.getElementById('lname').style.background="red";
document.getElementById('lname').focus();
}
else
{
document.getElementById('splname').innerHTML="";
document.getElementById('lname').style.background="green";
}
returnValue;
}
Method 3
fname=document.getElementById('fname').value;
namelen=/^([a-zA-Z0-9]{5,15})+$/;
if(!namelen.test(fname))
{
document.getElementById('spfname').innerHTML="Enter a name";
return false;
}
else
{
document.getElementById('spfname').innerHTML="";
}

I want to block certain characters in javascript validation?

I want to block ][#{}'". But somehow I'm not able to put it in my code. What should I declare in place of val. Also when I put val = /^[0-9]+$/;. It blocks numbers when I put them alone. But when I concatenate a number with an alphabet, it gets accepted. For ex- abc123 gets accepted whereas 123 does not.
function Allvalidate()
{
var input;
var controlId = document.getElementById("<%=TextBox1.ClientID %>");
input = controlId.value;
var val = //????//
if (input == "")
{
alert("Please Enter a Value" + "\n");
return false;
}
else if (val.test(input))
{
alert("It does not accept these characters" + "\n");
return false;
}
else
{
return true;
}
}
</script>
$(document).keydown(function (e) {
if (e.which == [here you can write the key code of the characters that you want allow]) {
// TO DO Your Logic
}
});
Here I have used e.which and e.keycode because of browser compatibility [ firefox and IE may give the different code]
I hope this will work . .

incrementing a $_GET variable

How do I get this $_GET['ago'] variable to increment by 1
public function __construct($future=false) {
$GLOBALS['twd_helper']=&$this;
}
public function query_string($add, $remove=null) {
$qs_data = array();
parse_str($_SERVER['QUERY_STRING'], $qs_data);
$qs_data = array_merge($qs_data, $add);
if ($remove) {
foreach($remove as $key) {
if (isset($qs_data[$key])) {
unset($qs_data[$key]);
}
}
}
return http_build_query($qs_data);
}
public function filter_overnight () {
if (isset($_GET['tod']) && $_GET['tod'] == 'overnight') {
$overnight = $_GET['overnight'];
}
if (($_GET['ago'])) {
$_GET['ago'] ? $_GET['ago']++ : $_GET['ago'] = 0;
}
}
I want to change my URL to increase by 1 each time its clicked
guide?ago=1
guide?ago=2
guide?ago=3
etc
In the html, just do this:
Link Text
Your filter_overnight function is a bit odd too - I think it could be made more readable:
if ($_GET['ago'])
{
$_GET['ago'] = (int)$_GET['ago'] + 1;
}
else
{
$_GET['ago'] = 0;
}
Or with the ternary:
$_GET['ago'] = ($_GET['ago']) ? (int)$_GET['ago'] + 1 : 0
Without seeing the whole code, I'm not sure why filter_overnight is incrementing $_GET['ago'], but at a glance it appears to put an undocumented side effect into the function by changing a global value (which would also render the increment in the <a> pointless). The superglobals in PHP can be dangerous things - I find it best to treat them as immutable and copy values out of them vs working on them directly.

Resources