c# - Custom string to 128-bit string -
i trying force custom string length between 0 , 15 128-bit string, can use aescryptoserviceprovider
key.
i have tried fiddling around multiple strategies, , have ended following:
if (stringtoconvert.length > 16) { stringbuilder sb = new stringbuilder(); char[] cha = stringtoconvert.tochararray(); int chamaxlength = cha.length; (int = 0; < 16; i++) { if (i <= chamaxlength) { sb.append(cha[i]); } } }
i need string 16 characters long (16*8 = 128).
i stuck it, , need helping hand go through obsticle.
apologize in advance if may seem simple.
example:
asd
become
asdasdasdasdasda
stringbuilder b = new stringbuilder(); (int = 0; < 8; i++) { b.append(stringtoconvert[i % stringtoconvert.length]); } stringtoconvert = b.tostring(); byte[] key = encoding.unicode.getbytes(stringtoconvert);//key size 16 bytes = 128 bits
even better (without stringbuilder
):
byte[] key = new byte[16]; (int = 0; < 16; i+=2) { byte[] unicodebytes = bitconverter.getbytes(stringtoconvert[i % stringtoconvert.length]); array.copy(unicodebytes, 0, key, i, 2); }
Comments
Post a Comment