Bitshift Encrypt and Decrypt with Circular shift in Python - encryption

Edit: I figured it out. Here is the code I used.
def bitshiftEncrypt(word):
newWord = ""
for i in word:
shift = '{:07b}'.format(ord(i)+1)
newShift = shift[1:]
newShift += shift[0]
newWord += chr(int(newShift,2))
print(newWord)
def bitshiftDecrypt(word):
newWord = ""
for i in word:
shift = '{:07b}'.format(ord(i))
newShift = shift[len(str(shift))-1]
newShift += shift[:-1:1]
newWord += str(chr(int(newShift,2)-1))
print(newWord)
Thanks for the help!

bin() will give you the shortest possible representation. This will screw you over in various ways.
3>> bin(3)
'0b11'
3>> '{:07b}'.format(3)
'0000011'

Related

Classic ASP Convert Latin Characters to Unicode Escape Strings

I need a Classic ASP function that will take a string such as Jämshög and convert it to J\u00e4msh\u00f6gso that all the accented characters become their equivalent unicode escape codes.
I am sending this data in a JSON string to an API that requires all special characters to use unicode escape codes.
I've been searching for what seems like hours to come up with a solution and I haven't managed to come close. Any help would be greatly appreciated.
Take a look at the function from aspjson below. It also handles non-unicode characters that must to be escaped such as quote, tab, line-feed etc. Luckily no dependencies, so works stand-alone too.
Function jsEncode(str)
Dim charmap(127), haystack()
charmap(8) = "\b"
charmap(9) = "\t"
charmap(10) = "\n"
charmap(12) = "\f"
charmap(13) = "\r"
charmap(34) = "\"""
charmap(47) = "\/"
charmap(92) = "\\"
Dim strlen : strlen = Len(str) - 1
ReDim haystack(strlen)
Dim i, charcode
For i = 0 To strlen
haystack(i) = Mid(str, i + 1, 1)
charcode = AscW(haystack(i)) And 65535
If charcode < 127 Then
If Not IsEmpty(charmap(charcode)) Then
haystack(i) = charmap(charcode)
ElseIf charcode < 32 Then
haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
End If
Else
haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
End If
Next
jsEncode = Join(haystack, "")
End Function

Encryption sorted...Decryption is close to finished - python 3.4.1

I can encrypt fine. That works will numbers, spaces and upper and lower case. However, when I set my code to decrypt the encrypted message, the code, for some reason, thinks that the decrypted message is empty. It prints an empty space. I'm not explaining it very well, but if you run the code you will understand what I am on about. If you have any ideas, please let me know.
alphabetL = 'abcdefghijklmnopqrstuvwxyz'
alphabetC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
space = ' '
Ll = len(alphabetL)
Lc = len(alphabetC)
Ln = len(number)
Lall = Ll + Lc + Ln
Question1 = input("Hello, please insert the message you want encrypted: ")
key1 = int(input("Please insert the key you want used [Keep between 1 and 26]: "))
cipher = ''
cipher2 = ''
for A in Question1:
if A in alphabetL:
cipher += alphabetL[(alphabetL.index(A)+key1)%Ll]
elif A in alphabetC:
cipher += alphabetC[(alphabetC.index(A)+key1)%Lc]
elif A in number:
cipher += number[(number.index(A)+key1)%Ln]
elif A in space:
cipher += space
else:
print ("Error, please use abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
print (cipher)
Question2 = input("Would you like to decrypt the message? [Y/N]: ")
if Question2 == "Y":
for A in cipher2:
cipher2 += cipher[(cipher(A)-key1)%(len(cipher))]
print (cipher2)

Should Generic URL Decoding Functions Handle "+" Chars or Just "%20" etc. Chars?

I am wondering if a generic URL Decoding function should handle the "+" character (space) in addition to all of the e.g. "%20" etc. encodings?
There is no specific use case as of yet.
Is there any spec that would be appropriate to reference here?
I am doing it in VBScript (but that is not relevant to my question I believe) and I have two versions, one which would handle the "+" by replacing it with a "" (space) ...
Public Function decode(s)
s = replace(s, "+", " ")
For i = 1 To Len(s)
If Mid(s, i, 1) = "%" Then
decode = decode & Chr("&H" & Mid(s, i+1, 2))
i = i + 2
Else
decode = decode & Mid(s, i, 1)
End If
Next
End Function
...and one which does not:
Public Function decode(s)
For i = 1 To Len(s)
If Mid(s, i, 1) = "%" Then
decode = decode & Chr("&H" & Mid(s, i+1, 2))
i = i + 2
Else
decode = decode & Mid(s, i, 1)
End If
Next
End Function
If it's supposed to be generic: no. The role of "+" is very specific to HTML forms and has nothing to do with generic URI handling.

Add numbers to string

I have a string that looks like this:
&s=Chicago,IL&s=Memphis,TN&s=Akron,OH&s=Plainfield,IN&s=Dallas,TX&s=Miami,FL&s=Orlando,FL&s=Valdosta,GA&s=Milwaukee,WI
sometimes it will be bigger and sometimes it will be smaller.
I would like to assign numbers to every &s= so the above example would look like this:
&s1=Chicago,IL&s2=Memphis,TN&s3=Akron,OH&s4=Plainfield,IN&s5=Dallas,TX&s6=Miami,FL&s7=Orlando,FL&s8=Valdosta,GA&s9=Milwaukee,WI
But I don't know how to do that, any help ?....
THANKS :-)
I tried something like this but it's not working???
dim AllLocations
AllLocations="&s=Chicago,IL&s=Memphis,TN&s=Akron,OH&s=Plainfield,IN&s=Dallas,TX&s=Miami,FL&s=Orlando,FL&s=Valdosta,GA&s=Milwaukee,WI"
dim i
For i=1 to TotalLocations
AllLocations=Replace(AllLocations,"&s=","&s" & i & "=")
Next
Response.Write(AllLocations)
THIS IS IT!
dim AllLocations
AllLocations="&s=Chicago,IL&s=Memphis,TN&s=Akron,OH&s=Plainfield,IN&s=Dallas,TX&s=Miami,FL&s=Orlando,FL&s=Valdosta,GA&s=Milwaukee,WI"
' You need to add TotalLocations and have it count how many &s's you have so that the For i 1 to knows where to stop.
dim i,ArrayOfValues,v,ovo
ArrayOfValues=Split(AllLocations,"&")
For i = 1 To TotalLocations
ovo=Replace(ArrayOfValues(i),"s=","&s" & i & "=")
Response.write ovo
Next
You can do it like this:
string s = "&s=Chicago,IL&s=Memphis,TN&s=Akron,OH&s=Plainfield,IN&s=Dallas,TX&s=Miami,FL&s=Orlando,FL&s=Valdosta,GA&s=Milwaukee,WI";
int i = 1;
var list = s.Split(',');
var result = list.Select(x => x.Replace("&s", "&s" + i++));
s = String.Join(",", result);
Here is the answer:
var res = "&s=Chicago,IL&s=Memphis,TN&s=Akron,OH&s=Plainfield,IN&s=Dallas,TX&s=Miami,FL&s=Orlando,FL&s=Valdosta,GA&s=Milwaukee,WI";emphis,TN&s=Akron,OH&s=Plainfield,IN&s=Dallas,TX&s=Miami,FL&s=Orlando,FL&s=Valdosta,GA&s=Milwaukee,WI";
var res = str.split("=");
for (var i=0; i < res.length; i++) {
res[i]=res[i]+i;
}
I have also write a pen for you, here is the Link

How do I find out if the first character of a string is a number in VB.NET?

How do I check to see if the first character of a string is a number in VB.NET?
I know that the Java way of doing it is:
char c = string.charAt(0);
isDigit = (c >= '0' && c <= '9');
But I'm unsure as to how to go about it for VB.NET.
Thanks in advance for any help.
Here's a scratch program that gives you the answer, essentially the "IsNumeric" function:
Sub Main()
Dim sValue As String = "1Abc"
Dim sValueAsArray = sValue.ToCharArray()
If IsNumeric(sValueAsArray(0)) Then
Console.WriteLine("First character is numeric")
Else
Console.WriteLine("First character is not numeric")
End If
Console.ReadLine()
End Sub
Public Function StartsWithDigit(ByVal s As String) As Boolean
Return (Not String.IsNullOrEmpty(s)) AndAlso Char.IsDigit(s(0))
End Function
Public Function StartsWithDigit(ByVal s As String) As Boolean
Return s Like "#*"
End Function
If I were you I will use
Dim bIsNumeric = IsNumeric(sValue.Substring(0,1))
and not
Dim sValueAsArray = sValue.ToCharArray()
It does not matter what you use, both will yield the same result,
but having said that; Dim sValueAsArray = sValue.ToCharArray() will use more memory & Dim bIsNumeric = IsNumeric(sValue.Substring(0,1)) will use less resources. though both of them are negligible
It is more of a suggestion of programming practice than anything else.
Char.IsNumber(c)
More details here: https://msdn.microsoft.com/en-us/library/yk2b3t2y(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
The VB.Net code that is equivalent to your Java code can be done using following lines
Dim c = sText(0)
bIsDigit = (c >= "0" AndAlso c <= "9")
where
Dim bIsDigit As Boolean
Dim sText as String = "2 aeroplanes" 'example for test
But, there exist also other solutions
bIsDigit = Char.IsDigit(c)
bIsDigit = Char.IsNumber(c)
bIsDigit = Information.IsNumeric(c)
and when sText is an empty string, you can also use one of following lines
Dim c = Mid(sText, 1, 1)
Dim c = (sText & "-")(0)
Dim c = Strings.Left(sText, 1)
Dim c As Char = sText
But, for me, the best solution is
bIsDigit = Char.IsDigit(Mid(sText, 1, 1))
or
bIsDigit = Char.IsDigit(sText(0))
if you are sure that sText is not empty.
And the shorter (but tricky) solution is
bIsDigit = Char.IsDigit(sText)
In this last line, first character of sText is implicitely converted to Char.

Resources