Key generator method? - key-generator

I really feel stupid for posting this but since i got no answers on my question and i am still junior programmer i will post this :
//List of keys
byte[] Key0 = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; //mode 10 = 0
byte[] Key1 = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; // i + 2
byte[] Key2 = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; // i mode 2 = 0
byte[] Key3 = { 66, 77, 88, 99, 111, 222, 110, 112, 114, 115 }; // mode 11 = 0
byte[] Key4 = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121 }; //x^2
byte[] Key5 = { 6,17,34,57,86,121,162,209 }; //3x^2+2x+1
byte[] Key6 = { 77,78,79,80,81,82,83,84,85,86,87 }; // only in range
//Add all keys to the list
List<byte[]> oKeysList = new List<byte[]>();
oKeysList.Add(Key0);
oKeysList.Add(Key1);
oKeysList.Add(Key2);
oKeysList.Add(Key3);
oKeysList.Add(Key4);
oKeysList.Add(Key5);
oKeysList.Add(Key6);
Random oRandom = new Random();
//Generate random key index to be used in the encryption
int ListSelectedIndex = oRandom.Next(0, oKeysList.Count);
byte[] GeneratedKey = oKeysList[ListSelectedIndex];
//Generate 3 random number from the selected key and concate the key index to it
byte[] GeneratedBytes = new byte[4];
for (int i = 0; i < 3; i++)
{
GeneratedBytes[i] = GeneratedKey[oRandom.Next(0,GeneratedKey.Length)];
}
//Add the list of key index
GeneratedBytes[3] = (byte)ListSelectedIndex;
//Return the genreated bytes
return GeneratedBytes;
As you see i generate this 4 byte array along with a 8 bytes generated with from RNG Cryptography, and when i want to check my serial i take the last 4 bytes and use the mathematical relations between them, I want to generate many serials and be able to check if they are valid or not. I know its probably pretty old method with a very bad security so please if any one could help me or add anything to my code or suggest anything new i would be really thankful.

I am not sure that anyone will analyze your code here. I suggest the following: do not make software protection too complex. It won't make a significant affect on piracy, but 100% will affect people, who purchased your software. Keep your software protection mechanisms as simple as possible.

Related

Reading duration of MP3 file from App script

I have a MP3 file in google drive. Is there a way I can get duraton of that MP3 file using google app script?
This page shows how to extract that information from an mp3 file by looking at the byte values.
So I wrote a short example and tested it. It seems to be working fine with all mp3 files I tried so far but there is no guarantee it will always work.
The getPlayTime function returns the play time in seconds.
function getPlayTime(file) {
var bitratesV1 = [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320],
bitratesV2 = [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160];
var bytes = file.getBlob().getBytes();
for(var pos = 0;pos < bytes.length; pos++) {
if(bytes[pos] === -1 && pos < bytes.length - 3 && (bytes[pos+1]&0xF0) === 0xF0) {
var isMpegVersion2 = (bytes[pos+1]&8) !== 8,
isLayer3 = (bytes[pos+1]&6) === 2,
bitRate = ((bytes[pos+2]&0xF0) >>> 4)&0xF;
if(!isLayer3) continue;
if(isMpegVersion2) bitRate = bitratesV2[bitRate];
else bitRate = bitratesV1[bitRate];
var playTime = bytes.length*8/(1000 * bitRate);
return playTime;
}
}
}
function test() {
var file = DriveApp.getFilesByName("music.mp3").next();
var playTime = getPlayTime(file);
Logger.log(playTime);
}
edit:
Here is a hopefully more accurate but also much slower version
function getRunTime(file) {
var playTime = 0, numFrames = 0;
var bitratesV1 = [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320],
bitratesV2 = [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160];
var bytes = file.getBlob().getBytes();
for(var pos = 0;pos < bytes.length; pos++) {
if(bytes[pos] === -1 && pos < bytes.length - 3 && (bytes[pos+1]&0xF0) === 0xF0) {
var isMpegVersion2 = (bytes[pos+1]&8) !== 8,
isLayer3 = (bytes[pos+1]&6) === 2,
bitRate = ((bytes[pos+2]&0xF0) >>> 4)&0xF;
if(!isLayer3) continue;
if(isMpegVersion2) bitRate = bitratesV2[bitRate];
else bitRate = bitratesV1[bitRate];
var pt = bytes.length*8/(1000 * bitRate);
if(!isNaN(pt) && isFinite(pt)) {
playTime += pt;
numFrames++;
}
}
}
return playTime/numFrames;
}

is it possible to shuffle that three arrays everytime the game starts?

i created three arrays and each of them has their own sets of questions in it. is it possible to shuffle that three arrays everytime the game starts? so that the user will have to answer different sets of question everytime he clicks for a new game.
See for example:
Randomize or shuffle an array
as3 random array - randomize array - actionscript 3
The second one has a nice cheeky little use of Array.sort():
var arr:Array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
function randomize (a:*, b:*):int
{
return (Math.random() > .5) ? 1 : -1;
}
trace(arr.sort(randomize));
To shuffle the 3 arrays you could create 3 other arrays that will serve to hold the shuffled values.
var arr1:Array = [1, 2, 3, 4, 5];
var sorted1:Array = new Array(arr1.length);
var arr2:Array = [1, 2, 3, 4, 5];
var sorted2:Array = new Array(arr2.length);
var arr3:Array = [1, 2, 3, 4, 5];
var sorted3:Array = new Array(arr3.length);
randomPos:Number = 0;
you could then create a function that takes the value from one array and place it into a new shuffled array.
function shuffleArray(x, y){
for(var i:int = 0; i < y.length; i++){
randomPos = int(Math.random() * x.length);
y[i] = x.splice(randomPos, 1)[0];
}
}
Then you would call the suffleArray function on each set of arrays:
shuffleArray(arr1, sorted1);
shuffleArray(arr2, sorted2);
shuffleArray(arr3, sorted3);
I hope this helps.

Convert input text to 2d array

Im trying to make a level editor for the game.
Now I can create a new map (using mouse) and click "generate" button to trace map array
(string). After that I can simple copy the code from the output
and use it to create a new level.
Lets say I have a class called NewLevel.as
I create a new array and paste code from output window, so I have 2d array.
Then adding tiles to stage using for loops.
var array:Array =
// code below is what I get in output window
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7],
[7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7]
];
for (var row:int = 0; row < array.length; row++)
{
for (var column:int = 0; column < array[row].length; column++)
{
var tile = new Tile();
addChild(tile);
tile.x = column * tile.width;
tile.y = row * tile.height;
tile.gotoAndStop(array[row][column] +1);
}
}
It works without problems,this gives me the map I created using level editor.
but what I want is that players input their "map code" and load the map they created.
I guess you have seen that in many games.
I have a textarea so users can input their string,
how can I convert their input to 2d array and load it (as you see in example)?
It should be 2d array.
I also added event listener to textarea
textarea.addEventListener(Event.CHANGE, changes);
function changes(e:Event):void
{
// convert input text to 2d array to build a new map
// Do not know how to get input to use with JSON
var myStr = levelTextarea.text;
var a2:Array = JSON.parse(myStr) as Array;
trace( a2 );
}
You can use JSON for this kind of job, this class is available in Flash Player 11.
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var a:Array = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7],
[7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7]
];
var txt:String = JSON.stringify( a )
trace( txt );
var a2:Array = JSON.parse( txt ) as Array;
trace( a2 );
}
}
}

Dot product implementation in as3

I am implementing a simple dot product algorithm into actionscript 3.0 codes. Here is the basic example.
(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58
I have a simple code here.
public var array1:Array = [1, 2, 3]; // 4, 10, 18
public var array2:Array = [4, 5, 6];
public var answer:Number = 0;
public function Algorithm()
{
multiply();
}
public function multiply()
{
var temp:Number = 0 ;
while (temp < array1.length)
{
answer = array1[temp] * array2[temp];
temp++;
}
trace(answer += answer);
}
But when I trace it..instead of 32, it goes 36... looks like it is adding 4 again for the last answer.
Its bugging me.
You are overwriting answer each time the array loops. The only value that is being stored is the last (3*6 = 18). In your trace you are effectively doubling that, giving you 36 every time. Try this:
answer = answer + ( array1[temp] * array2[temp] );
Then just trace answer at the end.

Cleanly merge two arrays in ActionScript (3.0)?

What's a nice way to merge two sorted arrays in ActionScript (specifically ActionScript 3.0)? The resulting array should be sorted and without duplicates.
To merge (concatenate) arrays, use .concat().
Below are two examples of how you can concatenate arrays and remove duplicates at the same time.
More convenient way: (you can use ArrayUtil.createUniqueCopy() from as3corelib)
// from as3corelib:
import com.adobe.utils.ArrayUtil;
var a1:Array = ["a", "b", "c"];
var a2:Array = ["c", "b", "x", "y"];
var c:Array = ArrayUtil.createUniqueCopy(a1.concat(a2)); // result: ["a", "b", "c", "x", "y"]
Slightly faster way: (you can loop through the arrays yourself and use Array.indexOf() to check for duplicates)
var a1:Array = ["a", "b", "c"];
var a2:Array = ["c", "b", "x", "y"];
var a3:Array = ["a", "x", "x", "y", "z"];
var c:Array = arrConcatUnique(a1, a2, a3); // result: ["a", "b", "c", "x", "y", "z"]
private function arrConcatUnique(...args):Array
{
var retArr:Array = new Array();
for each (var arg:* in args)
{
if (arg is Array)
{
for each (var value:* in arg)
{
if (retArr.indexOf(value) == -1)
retArr.push(value);
}
}
}
return retArr;
}
This is kind of an simple algorithm to write. I would be surprised if there were a more direct way to do this in Actionscript.
function merge(a1:Array, a2:Array):Array {
var result:Array = [];
var i1:int = 0, i2:int = 0;
while (i1 < a1.length && i2 < a2.length) {
if (a1[i1] < a2[i2]) {
result.push(a1[i1]);
i1++;
} else if (a2[i2] < a1[i1]) {
result.push(a2[i2]);
i2++;
} else {
result.push(a1[i1]);
i1++;
i2++;
}
}
while (i1 < a1.length) result.push(a1[i1++]);
while (i2 < a2.length) result.push(a2[i2++]);
return result;
}
Using Array.indexOf to detect duplicates is going to be painfully slow if you have a List containing a large number of elements; a far quicker way of removing duplciates would be to throw the contents of the Array into a Set after concatenating them.
// Combine the two Arrays.
const combined : Array = a.concat(b);
// Convert them to a Set; this will knock out all duplicates.
const set : Object = {}; // use a Dictionary if combined contains complex types.
const len : uint = combined.length;
for (var i : uint = 0; i < len; i++) {
set[combined[i]] = true;
}
// Extract all values from the Set to produce the final result.
const result : Array = [];
for (var prop : * in set) {
result.push[prop];
}
If your program makes heavy use of Collections then if may be prudent to make use of one of the many AS3 Collections frameworks out there which provide a simple interface for manipulating data and will always take the optimal approach when it comes to the implementation.
AS3Commons Collections
Polygonal DS
function remDuplicates(_array:Array):void{
for (var i:int = 0; i < _array.length;++i) {
var index:int = _array.indexOf(_array[i]);
if (index != -1 && index != i) {
_array.splice(i--, 1);
}
}
}
Then for the "merge" use concat.
exemple :
var testArray:Array = [1, 1, 1, 5, 4, 5, 5, 4, 7, 2, 3, 3, 6, 5, 8, 5, 4, 2, 4, 5, 1, 2, 3, 65, 5, 5, 5, 5, 8, 4, 7];
var testArray2:Array = [1, 1, 1, 5, 4, 5, 5, 4, 7, 2, 3, 3, 6, 5, 8, 5, 4, 2, 4, 5, 1, 2, 3, 65, 5, 5, 5, 5, 8, 4, 7];
testArray.concat(testArray2);
trace(testArray);
remDuplicates(testArray);
trace(testArray);
Please follow the below step to get your answer:
Concat two array using "Concat" Methos.
New Array (concated) sort using "Sort" method which provided as API in Array Class
Make user defined function to remove duplicates (see below functions)
> function removeDuplicates(p_arr:Array):Array {
var ansArr:Array = new Array();
var len:uint = p_arr.length;
var i:uint = 0;
var j:uint = 0;
ansArr[j] = p_arr[i];
i++;
j++;
while(i<len)
{
if(ansArr[j] != p_arr[i])
{
ansArr[j] = p_arr[i];
j++;
}
i++;
}
return ansArr;
}
Returned "ansArr" will be sorted and without duplicate merged array of two array.