Passing List<int> values to AES - flutter

I need some help please. I have been trying to figure out how to Decrypt an AES encrypted string using encrypt.dart.
My first problem is that I can get the Key and the IV in List<int> but cannot work out how to pass this to the decrypter.
The decrypter requires 32 byte length and the IV requires 16 byte lengths but when I convert either the myKey or the myIV to Hex string or String(Disaster), they land up being double the lengths. I dont know how to pass a List<int> to the decryptors and have tried converting them to hex string.
List<int> intList = [189, 249, 66, 29, 132, 72, 230, 0, 235, 175, 236, 175, 246, 114, 21, 20, 79, 4, 174, 195, 121, 156, 172, 10, 50, 64, 94, 96, 112, 85, 204, 149];
String hexString = utf8.decode(intList);
hexString = hexString.runes.map((int rune) => rune.toRadixString(16).padLeft(2, '0').toUpperCase()).join();
but this again just doubles the length.
So this is where my limitation is:
List<int> myKey = [84, 124, 176, 174, 139, 253, 34, 86, 205, 149, 187, 232, 133, 67, 56, 212, 180, 63, 73, 222, 33, 183, 190, 41, 62, 23, 94, 233, 133, 46, 162, 219];x11
List<int> myIV = [183, 104, 193, 48, 247, 178, 239, 48, 249, 41, 56, 33, 255, 150, 74, 110]
final key = enc.Key.fromUtf8(*myKey*); //32 length
final iv = enc.IV.fromUtf8(*myIV*); //16 length
// Encryption
String kelime = 'The quick brown fox jumps over the lazy dog';
final encrypter = enc.Encrypter(enc.AES(key, mode: enc.AESMode.ctr, padding: null));
final encrypted = encrypter.encrypt(kelime, iv: iv);
final ciphertext = encrypted.base64;
print(ciphertext);
// Decryption
final decrypter = enc.Encrypter(enc.AES(key, mode: enc.AESMode.ctr, padding: null));
final decrypted = decrypter.decryptBytes(enc.Encrypted.fromBase64(ciphertext), iv: iv);
final decryptedData = utf8.decode(decrypted);
print(decryptedData);
Any Help would be greatly appreciated
Thanks
Robby

Use the unnamed Key constructor that takes a byte array.
final key = enc.Key(Uint8List.fromList(myKey));

Related

how to convert Decimal numbers to ASCII?

Hello i want to convert a decimal number to ASCII so this the code i parse my string to int then i call the function String.fromCharCode to convert the int to decimal . But it doesn't work any help ! thanks
String payload ="47, 50, 48, 50, 50, 59, 49, 52, 58, 51, 52, 59, 48, 55, 47, 58, 5";
print (payload);
for (var i = 0; i < payload.length; i++)
{
print (payload[i]);
data = String.fromCharCode(int. parse(payload[i])) + data ;
}
print(data);
Try this one:
for(int i=65; i<=90; i++){
print("${String.fromCharCode(i)}");
}
You first need to split your String into tokens, parse those individually, and then create a String from the parsed integer values.
void main() {
String payload =
"47, 50, 48, 50, 50, 59, 49, 52, 58, 51, 52, 59, 48, 55, 47, 58, 5";
var s = String.fromCharCodes(payload.split(',').map(int.parse));
print(s);
}
I'll also point out that an ASCII value of 5 (ENQ) is very suspect.
okay so this how it works for me
String payload ="47, 50, 48, 50, 50, 59, 49, 52, 58, 51, 52, 59, 48, 55, 47, 58, 5";
final splitted = payload.split(',');
for (var i = 0; i < splitted.length ; i++)
{
print (splitted[i]);
data = String.fromCharCode(int.parse(splitted[i])) + data ;
}
print(data);

Split array by percentage

I have some powershell code that returns a load of computers to an array from a SQL query.
The query return vary in the total number of members. I shuffle the array also.
I want to take that array and split it into four or five smaller arrays by percentage. I want to define the percentages beforehand. For example, I might want to see 25%, 25%, 25%, 25%. Another time I want to see 10%, 30%, 30%, 40% or another 5%, 35%, 30%, 30%.
$phasepercents = 0.1,0.2,0.2,0.25,0.25 #Percentage of machines in each
group
# AD groups to be created, Group5 is not needed as all EUC machines will be
# added in the final round
$Phase0group = #()
$Phase1group = #()
$Phase2group = #()
$Phase3group = #()
$Phase4group = #()
$computers = my sql query with returned computers
$totalmachines = $computers.count #Total No of machine in the collection
$Machinesineachgroup = #()
$phasestartnumbers = #()
# work out the percentages
$counter = 0
foreach ($phasepercent in $phasepercents)
{
$value = $phasepercent *= $totalmachines
$value = [Math]::floor($value)
$Machinesineachgroup += $value
#WriteToLog "[INFO]`t,Machines in Phase$counter is $value"
$counter +=1
}
WriteToLog "[INFO-S]`t Total Machines in each phases $Machinesineachgroup"
this will break your collection into batch sizes based on the % in the $PercentageList and then store those batches into an array of arrays. you could also modify it to store the arrays in properties of a custom object OR into a hashtable if desired. [grin]
$MasterList = 1..100
$PercentageList = 10, 30, 20, 40
$Index = 0
$BatchList = foreach ($PL_Item in $PercentageList)
{
$BatchSize = [math]::Round($MasterList.Count * $PL_Item / 100, 0)
# the leading comma forces PoSh to NOT unroll the array
# instead, it is stored as whole
,$MasterList[$Index..($Index + $BatchSize - 1)]
$Index = $Index + $BatchSize
}
$BatchList[0] -join ', '
'=' * 30
$BatchList.ForEach({$_ -join ', '})
output ...
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
==============================
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100

"Impossible" error message in R for VennDiagram

I keep getting an error message when trying to use VennDiagram in R. Below is my posted code:
draw.quintuple.venn(area1 = 578, area2 = 519, area3 = 212, area4 = 402, area5 = 172, n12 = 366, n15 = 97, n13 =149, n14 = 284, n23 = 103, n24 = 202, n25 = 125, n35 = 31, n34= 12, n45 = 27, n123 = 80, n124 = 161, n125 = 84, n134 = 8, n135 = 25, n145 = 20, n234 = 5, n235 = 24, n245 = 21, n345 = 1, n1234 = 5, n1345 = 21, n1245 = 16, n1235 = 0, n2345 = 0, n12345 = 0, category = c("1", "2", "3", "4", "5"), lty = "blank", fill = c("skyblue", "pink1", "mediumorchid", "yellow", "orange"))
Error:
ERROR [2018-07-09 13:37:19] Impossible: a11 <- n23 - a21 - a22 - a24 -
a26 - a29 - a30 - a31 produces negative area Error in
draw.quintuple.venn(area1 = 578, area2 = 519, area3 = 212, area4 =
402, : Impossible: a11 <- n23 - a21 - a22 - a24 - a26 - a29 - a30
- a31 produces negative area
What am I doing wrong?
I double checked and made sure the values are all correct.
I do not think that this package is well documented. A look at the source code shows that the meaning of nxxxxx is not the obvious one. For instance n135 means "how many elements belong to at least groups 1, 3 and 5". When you want to draw the diagram, the package calculates how many of those n135 also belong to other groups (i. e., n1235, n1345 and n12345) and substracts them.
What seems to be happening here is that you interpret n135 as "how many elements only belong to sets 1, 3 and 5" (that would have also been my guess). If you want to use those numbers directly, you should write:
draw.quintuple.venn(area.vector = c(578, 519, 212, 402, 172, 31, 97, 284, 366, 125, 103, 149, 12, 202, 27, 1, 25, 20, 161, 84, 24, 80, 8, 5, 21, 0, 21, 16, 0, 5, 0), category = c("1", "2", "3", "4", "5"), lty = "blank", fill = c("skyblue", "pink1", "mediumorchid", "yellow", "orange"), direct.area = T)
The order of the numbers is taken directly from the source code, I have not seen it documented. Let us call a135 "how many elements only belong to sets 2, 3 and 5". With this in mind, the order would be:
a1, a2, a3, a4, a5, a35, a15, a14, a12, a25, a23, a13, a34, a24, a45, a345, a135, a145, a124, a125, a235, a123, a134, a234, a245, a2345, a1345, a1245, a1235, a1234, a12345
If you prefer to use the n135 notation, you would need to transform your data, so that n135 = a135 + a1235 + a1345 + a12345, and so forth. In your case, n135 = 25 + 0 + 21 + 0 = 36.
Although not part of the question, you can also use my nVennR package for a proportional representation. The order of the numbers is documented in the help and the vignette, and you can also enter raw sets rather than numbers:
library(nVennR)
myV <- createVennObj(nSets = 5, sNames = c('1', '2', '3', '4', '5'), sSizes = c(0, 172, 402, 27, 212, 31, 12, 1, 519, 125, 202, 21, 103, 24, 5, 0, 578, 97, 284, 20, 149, 25, 8, 21, 366, 84, 161, 16, 80, 0, 5, 0))
myV <- plotVenn(nVennObj = myV, setColors = c("skyblue", "pink1", "mediumorchid", "yellow", "orange"), borderWidth = 0)
And the result:

Invalid MIT magic cookie when connecting to X server

I'm trying to write my own code to connect to an X server. I ran xauth to get the magic cookie I needed and wrote the following code to try and test out establishing a connection:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
int main()
{
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un serv_addr;
memset((void*)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, "/tmp/.X11-unix/X0");
int servlen = 17 + sizeof(serv_addr.sun_family);
int err = connect(sockfd, (struct sockaddr*)&serv_addr, servlen);
char arr[] = {108, 0, // 'l' for little-endian
11, 0, // X version
0, 0, // X minor version
18, 0, // length of auth protocol name
16, 0, // length of auth protocol data
0, 0, // padding
77, 73, 84, 45, 77, 65, 71, 73, 67, 45, 67, 79, 79, 75, 73, 69, 45, 49, // MIT-MAGIC-COOKIE-1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223, 88, 218, 121, 215, 6, 185, 105, 137, 80, 105, 252, 49, 109, 38, 200, // data from .Xauthority
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
ssize_t bytes_written = write(sockfd, arr, sizeof(arr));
printf("%d\n", bytes_written);
uint8_t buf[5000];
ssize_t bytes_read = read(sockfd, buf, 5000);
printf("%d\n", bytes_read);
unsigned char k;
for(k = 0; k < 40; k++) {
printf("%c", buf[k]);
}
return 0;
}
The X server responds all right but it gives me an authentication failed message with "Invalid MIT-MAGIC-COOKIE-1 key" as the reason. The key I'm giving is the same as the one in my .Xauthority file (df58da79d706b969895069fc316d26c8, in case anyone wants to check!) Is there something else I'm missing?
you have too much of padding zeroes. should be:
char arr[] = {108, 0, // 'l' for little-endian
11, 0, // X version
0, 0, // X minor version
18, 0, // length of auth protocol name
16, 0, // length of auth protocol data
0, 0, // padding
77, 73, 84, 45, 77, 65, 71, 73, 67, 45, 67, 79, 79, 75, 73, 69, 45, 49, // MIT-MAGIC-COOKIE-1
0, 0, // two bytes to pad 18-byte MIT-MAGIC-COOKIE-1 to factor of 4 - 20
223, 88, 218, 121, 215, 6, 185, 105, 137, 80, 105, 252, 49, 109, 38, 200 // data from .Xauthority
}; // no need for more padding, auth data is 16 bytes long, factor of 4
From page 140 of X11 protocol:
Information sent by the client at connection setup:
1 byte-order
▶x42 MSB first
▶x6C LSB first
1 unused
2 CARD16 protocol-major-version
2 CARD16 protocol-minor-version
2 n length of authorization-protocol-name
2 d length of authorization-protocol-data
2 unused
n STRING8 authorization-protocol-name
p unused, p=pad(n)
d STRING8 authorization-protocol-data
q unused, q=pad(d)

Why doesen't the number 2 work in this for-loop?

I have a function that runs trough each element in an array.
It's hard to explain, so I'll just paste in the code here:
NSLog(#"%#", arraySub);
for (NSNumber *favoriteThing in arrayFav){
int favoriteLoop = [favoriteThing intValue] + favCount;
NSLog(#"%d", favoriteLoop);
id arrayFavObject = [array objectAtIndex:favoriteLoop];
[arrayFavObject retain];
[array removeObjectAtIndex:favoriteLoop];
[array insertObject:arrayFavObject atIndex:0];
[arrayFavObject release];
id arraySubFavObject = [arraySub objectAtIndex:favoriteLoop];
[arraySubFavObject retain];
[arraySub removeObjectAtIndex:favoriteLoop];
[arraySub insertObject:arraySubFavObject atIndex:0];
[arraySubFavObject release];
id arrayLengthFavObject = [arrayLength objectAtIndex:favoriteLoop];
[arrayLengthFavObject retain];
[arrayLength removeObjectAtIndex:favoriteLoop];
[arrayLength insertObject:arrayLengthFavObject atIndex:0];
[arrayLengthFavObject release];
}
NSLog(#"%#", arraySub);
The array arrayFav contains these numbers: 3, 8, 2, 10, 40.
Array array contains 92 strings with a name.
Array arraySub contains numbers 0 to 91, representing a filename with a title from the array array.
Array arrayLength contains 92 strings representing the size of each file from array arraySub.
Now, the first NSLog shows, as expected, the numbers 0 to 91.
The NSLog-s in the loop shows the numbers 3, 8, 2, 10, 40, also as expected.
But here's the odd part: the last NSLog shows these numbers:
40, 10, 0, 8, 3, 1, 2, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91
that is 40, 10, 0, 8, 3, and so on.
It was not supposed to be a zero in there, it was supposed to be a 2..
Do you have any idea at why this is happening or a way to fix it? Thank you.
EDIT: Code has changed. arrayFav now contains numbers.
The -intValue method returns 0 if there is something funky about the beginning of the string being converted.
Instead of using NSString to store integers, try using NSNumber to enforce storing numbers in your array, or debug what is being sent the -intValue message.
I fixed this by removing the for-loop and just search for the correct location in the arraySub.
// If the object is not really the object we want, get what we want
if (favoriteLoop != [[[arraySub objectAtIndex:favoriteLoop] description] intValue]){
favoriteLoop = [arraySub indexOfObject:[NSString stringWithFormat:#"%d", favoriteLoop]];
}