Having a C# encryption decryption methods , wanted there copy in JAVA -
private static string encrypt(string plaintext) { byte[] keybytes = encoding.utf8.getbytes(_secretkey); byte[] hashedkeybytes = new sha256cryptoserviceprovider().computehash(keybytes); var secretkeyhashstring = string.concat(hashedkeybytes.select(hb => hb.tostring("x2"))); byte[] cryptokeyhash = new sha256cryptoserviceprovider().computehash(encoding.utf8.getbytes(secretkeyhashstring)); byte[] cryptokey = cryptokeyhash.take(16).toarray(); var aes = new aescryptoserviceprovider() { padding = paddingmode.pkcs7, key = cryptokey, mode = ciphermode.cbc, iv = _initialisationvector }; rijndaelmanaged aesenc = new rijndaelmanaged(); byte[] plaintextbytes = encoding.utf8.getbytes(plaintext); var encryptor = aes.createencryptor(); byte[] encryptedbytes = encryptor.transformfinalblock(plaintextbytes, 0, plaintextbytes.length); var encryptedstring = convert.tobase64string(encryptedbytes); return encryptedstring; } private static string decrypt(string encryptedtext) { byte[] keybytes = encoding.utf8.getbytes(_secretkey); byte[] hashedkeybytes = new sha256cryptoserviceprovider().computehash(keybytes); var secretkeyhashstring = string.concat(hashedkeybytes.select(hb => hb.tostring("x2"))); byte[] cryptokeyhash = new sha256cryptoserviceprovider().computehash(encoding.utf8.getbytes(secretkeyhashstring)); byte[] cryptokey = cryptokeyhash.take(16).toarray(); byte[] initialisationvector = { 0, 133, 36, 86, 84, 150, 188, 164, 28, 210, 112, 158, 141, 87, 11, 227 }; var aes = new aescryptoserviceprovider() { padding = paddingmode.pkcs7, key = cryptokey, mode = ciphermode.cbc, iv = _initialisationvector }; var decryptor = aes.createdecryptor(); var encryptedbytes = convert.frombase64string(encryptedtext); var decryptedbytes = decryptor.transformfinalblock(encryptedbytes, 0, encryptedbytes.length); var decryptedstring = encoding.utf8.getstring(decryptedbytes); return decryptedstring; }
java provides own encryption/decryption interface http://www.codejava.net/coding/file-encryption-and-decryption-simple-example
Comments
Post a Comment