HOWTO Encrypt and Decrypt BitShares Memos in Either JavaScript or Java
utopian-io·@alexpmorris·
0.000 HBDHOWTO Encrypt and Decrypt BitShares Memos in Either JavaScript or Java
For the projects I've been working on, I needed to make sure I could encrypt and decrypt graphene memos in either JavaScript or Java. As I've often discovered in blockchain land, finding the correct solutions "out of the box" proved to be a bit elusive. For STEEM, memos are almost always unencrypted, so unless there's an alternative use case, it was more important for me to focus on encrypting/decrypting BitShares memos, which are generally always encrypted. For STEEM, the steemjs library makes it easy to do this in JavaScript, using the following functions: ```var encoded = steem.memo.encode(privateWifAccountFrom, publicWifAccountTo, `#This is my private message`);``` ```var decoded = steem.memo.decode(privateWifAccountFromOrTo, encoded);``` However, I could not find any equivalent solution in Java. Further complicating matters, it seems that the JavaScript version used by steemjs serializes the underlying memo object for encryption and decryption, as opposed to the plain and simple JSON representation of a memo *(as is the case with BitShares)*. While one could always change this to encode or decode a simple JSON string, for example, by simply swapping it out for my BitShares memo solution, it still wouldn't help if I ever needed to decode a memo encrypted using the original steemjs library *(which also uses the same format as the STEEM condenser front-end)*. Regardless, if anyone's come across or managed to tackle this problem and come up with a nice Java solution, please share! I'd be very interested to see it. # Back to BitShares Memos Given I needed to decrypt BitShares memos anyway, and that solution could be used for any other purpose that would come up as well, I focused my efforts in that direction. While it certainly took a bit of legwork for me to find all the pieces, I was unable to find any functions in either JavaScript or Java that were as easy to use as the steemjs version. In fact, the `bitsharesjs` library for the browser doesn't even expose the Aes library class that would be needed to encrypt and decrypt memos *(the Aes library could be accessed from nodejs though)*. Perhaps, at some point they will consider to include this code into the "reference" BitShares JavaScript library as well. After I figured out everything that was going on, I rolled both sides into simple classes that anyone can use. My JavaScript version currently resides in my cross-chain <a href="https://github.com/ripplerm/steem-lib">steem-lib</a> *(a terrific STEEM library by @ripplerm)* fork that I expect to open-source in the near future. In the meantime, the key source files you'll need to recreate the solution can be found in my <a href="https://github.com/alexpmorris/crypto-playpen">crypto-playpen github repo</a>. # JavaScript Implementation Moving right along, here is the key source code for the JavaScript version: https://media.discordapp.net/attachments/472476039734362112/482053685032255507/unknown.png Complete JavaScript class in crypto-playpen: <a href="https://github.com/alexpmorris/crypto-playpen/blob/master/steem-lib-btsAes.js">steem-lib-btsAes.js</a> The `usePrefix` parameter allows you to set the public key prefix *(ie. 'STM', 'BTS', 'GLS', etc)*. If you don't require that functionality, you can simply remove that extra code and go with the default used by your toPrivateKey / toPublicKey implementation *(such as the one in bitsharesjs)*. Here's an example of what the code looks like in action from Chrome Console: https://cdn.discordapp.com/attachments/472476039734362112/482056639483084800/unknown.png # Java Implementation Next, here is the Java implementation of the same functions:  Complete Java class in crypto-playpen: <a href="https://github.com/alexpmorris/crypto-playpen/blob/master/GrapheneMemo.java">GrapheneMemo.java</a> *Note: `asBase64` should be set to false for compatibility, and is meant for other potential use-cases.* In conclusion, for those of you who may have been searching for this solution, I hope this saves you at least a few hours worth of digging, as well as a few potential headaches along the way. And once again, you can find all this code in my <a href="https://github.com/alexpmorris/crypto-playpen">github crypto-playpen repo</a>. <center>*As always, I appreciate your upvote, your follow and all your comments!*</center>
👍 fuzzyvest, alexpmorris, mjhomb, michellectv, domo, ampm, happyphoenix, leprechaun, smartdeveloper, taylor.swift, icmarkets, kenistyles, stackin, luigi-tecnologo, alexis555, vitkolesnik, abcvivir, goldenpari, polbot, offoodandart, theywillkillyou, minnowsupport, st3llar, greenorange, secret.service, grzesiekb, iauns, matt-a, blhz, scaredycatguide, scorer, tomino, futurethinker, juecoree, tombstone, henryjames, vortac, xenetics, chuckyfucky, steempower, lindahas, essenceofworld, chinwei89,