what will be the exact code of below vb code in java because i am not getting the same encrypted value -
' encrypts string using tripple des private function tripledesencrypt(byval str string) tripledesencryptresult
' 3des encryption ' generate key/iv pair local 3des encrpytion on xmlcard ' create new 3des cryptoprovider , generate key/iv pair encryption dim m_cryptoprovider new tripledescryptoserviceprovider m_cryptoprovider.generateiv() m_cryptoprovider.generatekey() dim stream system.io.memorystream = new system.io.memorystream dim cryptostream cryptostream = _ new cryptostream(stream, m_cryptoprovider.createencryptor, _ cryptostreammode.write) dim input() byte = system.text.encoding.default.getbytes(str) cryptostream.write(input, 0, input.length) cryptostream.flushfinalblock() ' convert base64, it'll xml-friendly dim encryptedcardstring = system.convert.tobase64string(stream.toarray()) cryptostream.close() cryptostream.dispose() return new tripledesencryptresult(encryptedcardstring, m_cryptoprovider.key, m_cryptoprovider.iv) end function
below code in java 3des encryption , decryption. please try code. hope you
private static final string unicode_format = "utf8"; public static final string desede_encryption_scheme = "desede"; //"desede/ecb/nopadding"; private keyspec ks; private secretkeyfactory skf; private cipher cipher; byte[] arraybytes; private string myencryptionkey; private string myencryptionscheme; secretkey key; public passwordencryption_trippledes() throws exception { myencryptionkey = "thisisspartathisisanilku"; myencryptionscheme = desede_encryption_scheme; arraybytes = myencryptionkey.getbytes(unicode_format); ks = new desedekeyspec(arraybytes); skf = secretkeyfactory.getinstance(myencryptionscheme); cipher = cipher.getinstance(myencryptionscheme); key = skf.generatesecret(ks); } public string encrypt(string unencryptedstring) { string encryptedstring = null; try { cipher.init(cipher.encrypt_mode, key); byte[] plaintext = unencryptedstring.getbytes(unicode_format); byte[] encryptedtext = cipher.dofinal(plaintext); encryptedstring = new string(base64.encodebase64(encryptedtext)); } catch (exception e) { e.printstacktrace(); } return encryptedstring; } public string decrypt(string encryptedstring) { string decryptedtext=null; try { cipher.init(cipher.decrypt_mode, key); byte[] encryptedtext = base64.decodebase64(encryptedstring); byte[] plaintext = cipher.dofinal(encryptedtext); decryptedtext= new string(plaintext); } catch (exception e) { e.printstacktrace(); } return decryptedtext; } public static void main(string args []) throws exception { passwordencryption_trippledes td= new passwordencryption_trippledes(); string target="data encyption"; string encrypted=td.encrypt(target); string decrypted=td.decrypt(encrypted); system.out.println("string encrypt: "+ target); system.out.println("encrypted string:" + encrypted); system.out.println("decrypted string:" + decrypted); }
Comments
Post a Comment