base64 in Nodejs and Java
From : https://www.hacksparrow.com/nodejs/base64-encoding-decoding-in-node-js.html
let secretBase64 = Buffer.from(“JavaScript”).toString('base64');
// SmF2YVNjcmlwdA==
let secretBase64 = Buffer.from(“JavaScript”).toString('base64');
// SmF2YVNjcmlwdA==
var b = new Buffer('JavaScript'); // Buffer
var s = b.toString('base64');
// SmF2YVNjcmlwdA==
var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();
// JavaScript// -------------------
var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString('hex');
// 4a617661536372697074
var b = new Buffer('4a617661536372697074', 'hex')
var s = b.toString('utf8');
// JavaScript//----------------------
$node
>require('crypto').randomBytes(64).toString('hex')
f3a302c14cd2f57bc6972a0d599857dbde877fbf03b38dd592b9635bc7f30299c9d5dfe44e5abb615737cf40a45958c8283b5d1132c4e7e0d8e66610044be1f4
https://expressjs.com/en/api.html
var fs = require('fs'); // function to encode file data to base64 encoded string function base64_encode(file) { // read binary data var bitmap = fs.readFileSync(file); // convert binary data to base64 encoded string return new Buffer(bitmap).toString('base64'); } // function to create file from base64 encoded string function base64_decode(base64str, file) { // create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded var bitmap = new Buffer(base64str, 'base64'); // write buffer to file fs.writeFileSync(file, bitmap); console.log('******** File created from base64 encoded string ********'); } // convert image to base64 encoded string var base64str = base64_encode('kitten.jpg'); console.log(base64str); // convert base64 string back to image base64_decode(base64str, 'copy.jpg'); https://www.cnblogs.com/zclub/p/11412255.html
#java.util.Base64
Base64.getEncoder(),Base64.getDecoder()->RFC4648
Base64.getUrlEncoder(),Base64.getUrlDecoder()->RFC4648_URLSAFE
Base64.getMimeEncoder(),Base64.getMimeDecoder()->RFC2045
Reference: https://www.ietf.org/rfc/rfc4648.txt
https://www.ietf.org/rfc/rfc2045.txt
评论
发表评论