加密、编码相关知识汇总

ascii

js

1
2
3
4
var str = "A";
str.charCodeAt(); // 65

String.fromCharCode(65); // 'A'

python

1
2
3
4
s = 'A'
print(ord(s)) #65

print(chr(65)) # A

base64

js

1
2
3
window.btoa("python123") //cHl0aG9uMTIz

window.atob("cHl0aG9uMTIz") //python123

python

1
2
3
4
5
import base64

base64.b64encode(b'python123') # b'cHl0aG9uMTIz'

base64.b64decode(b'cHl0aG9uMTIz') # b'python123'

AES

js

https://github.com/brix/crypto-js 基于这个库

参考 https://cryptojs.gitbook.io/docs/#ciphers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var CryptoJS = require("crypto-js");
// 参考https://cryptojs.gitbook.io/docs/#ciphers

var password = 'dahsjhfjakhfjksdsssss1sx' //密码
var iv = 'fdshgjdsngjknaaa'


var encrypted = CryptoJS.AES.encrypt("helloworld", password);
var decrypted = CryptoJS.AES.decrypt(encrypted, password);
console.log('encrypted: ', encrypted.toString())
console.log('decrypted: ', decrypted.toString(CryptoJS.enc.Utf8))
// encrypted: U2FsdGVkX1 + mjJEOaXrsYp + PtlZvC0P4HIZZh6LYAmM =
// decrypted: helloworld

var encrypted1 = CryptoJS.AES.encrypt("helloworld", password, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
var decrypted1 = CryptoJS.AES.decrypt(encrypted1, password, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
console.log('encrypted1: ', encrypted1.toString())
console.log('decrypted1: ', decrypted1.toString(CryptoJS.enc.Utf8))

// encrypted1: U2FsdGVkX1 + bg / GfdQ4dHlCKpGj15ZsC6ewZru327lY =
// decrypted1: helloworld

CryptoJS supports the following modes:

  • CBC (the default)
  • CFB
  • CTR
  • OFB
  • ECB

And CryptoJS supports the following padding schemes:

  • Pkcs7 (the default)
  • Iso97971
  • AnsiX923
  • Iso10126
  • ZeroPadding
  • NoPadding

python

https://pypi.org/project/pycrypto/ 基于这个库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from Crypto.Cipher import AES

password = 'dahsjhfjakhfjksdsssss1sx'
iv = 'fdshgjdsngjknaaa'

##这边填充方式没有自带的方法,需要自己写
# pad = lambda s: s + (16 - len(s)%16) * chr(0) # ZeroPadding
pad = lambda s: s + (16 - len(s)%16) * chr(16 - len(s)%16) # Pkcs7

obj = AES.new(password,AES.MODE_CBC,iv)
encrypted = obj.encrypt(pad("helloworld").encode("utf8"))
print(encrypted)
# b'k\xb8\xad\x1c\xde\xa3O1n-\xf9\x99\xbc\x16\xb4\xd7'

obj2 = AES.new(password, AES.MODE_CBC, iv)
print(obj2.decrypt(encrypted)) #结果也需要手动去掉填充...
# b'helloworld\x06\x06\x06\x06\x06\x06'

RSA

js

使用的js库 是 https://github.com/travist/jsencrypt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDSLffg4ztiN4J7eSWhw3kIrrsM
5IMnkYMAxJgWDaIXwo7mJRpCdYkQtxLPnbE+zFklL1BLS923MFtAz5uLpIkf0LiV
YoaAdGMMVfEfusnW8GcWPMse/0jMZkb9RdSI7othUETJNYadUdBQ2tZoGryHJ6N2
DU7RVvt+6KiysCGbXQIDAQAB
-----END PUBLIC KEY-----`

var priveteKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDSLffg4ztiN4J7eSWhw3kIrrsM5IMnkYMAxJgWDaIXwo7mJRpC
dYkQtxLPnbE+zFklL1BLS923MFtAz5uLpIkf0LiVYoaAdGMMVfEfusnW8GcWPMse
/0jMZkb9RdSI7othUETJNYadUdBQ2tZoGryHJ6N2DU7RVvt+6KiysCGbXQIDAQAB
AoGBAMvW2Ti6KLWwsFOfwueH9LJ/A9oVJT4S2cK85+M7haEViBGcmkMqSqxzYUMJ
5QTKmwp9QJpGEdnAhD/vGSPthy4ZfaLLSDqjHgrSAVLTUp8ol1/n5V0G40EjvRWc
l4FKx8yHDcsOhkk4LcBDJnUCkZW5t+td8eASewtTMmFHDV3hAkEA859JvI881KoM
u5hCYBdIkSTsmjZdz68LTxDF3eB9Wgdsk4FS+OYkujl6+pch1fXIgjycs8V5eb6H
9QS8SHE/+wJBANzbs/IX2TcIPU6T4AKMt9QI/+lA/baLD6aaznxs8uvlVQ1XF6Y2
G91imkM0YgobSvUgleswWvjfJPicoVWeOocCQDTUoiTl2wczKTe1OQ+lfPMc0sJO
b+GN0CIBPSd0ezp+yajjLp8TKXvI40ZcZ9YCSs0TX9gMRt+D1e/oavw6sukCQCEd
5I6hmtWQq7iznbxkI1BtBQqRzzlnArLFtb6ci5Rpr+YvMAGhTW5V0bHQEf9X9lZp
S3GXr5xvwVhyDBMNhdMCQQCgVI/9jnc5WW7M9vMxNjxIJ1/LD0G58A31pj0SM0rA
UlRz95QGWJ93+SA2fzM45NgjbTD7IOZ5O7GfBSEjSok5
-----END RSA PRIVATE KEY-----`

//下面的代码参考https://github.com/travist/jsencrypt/issues/147#issuecomment-534359519
//其实实际使用应该是在js环境下...这个代码只是参考
global.navigator = {
appName: 'nodejs'
}; // fake the navigator object
global.window = {}; // fake the window object
const JSEncrypt = require('jsencrypt').default;

const crypt = new JSEncrypt({
default_key_size: 512
});
// console.log(priveteKey, publicKey)

const rawStr = 'helloworld'

const n1cypt = new JSEncrypt();
n1cypt.setPublicKey(publicKey);
const encrptStr = n1cypt.encrypt(rawStr);

const n2cypt = new JSEncrypt();
n2cypt.setPrivateKey(priveteKey);
const decryptStr = n2cypt.decrypt(encrptStr);

console.log(rawStr, '\n', encrptStr, '\n', decryptStr);
//helloworld
// S4phrZr2XESkWCzEkj2MsjeBTAMdzxrGSOpoSHOiDKlJDWliRil5OFIk03OTh9f4U0yr8+8biLNhkpFwDR8CbvXRuhrf8/vRfAtpDsfWyFE7VXeEchp9r/YNgTHjPosnzZSxMGdx7+QTDZ4p2uIx0pgCXm5GzySPU3D/lyZLBgI=
// helloworld

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#参考 https://stackoverflow.com/questions/21327491/using-pycrypto-how-to-import-a-rsa-public-key-and-use-it-to-encrypt-a-string

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5

private_key = """-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDSLffg4ztiN4J7eSWhw3kIrrsM5IMnkYMAxJgWDaIXwo7mJRpC
dYkQtxLPnbE+zFklL1BLS923MFtAz5uLpIkf0LiVYoaAdGMMVfEfusnW8GcWPMse
/0jMZkb9RdSI7othUETJNYadUdBQ2tZoGryHJ6N2DU7RVvt+6KiysCGbXQIDAQAB
AoGBAMvW2Ti6KLWwsFOfwueH9LJ/A9oVJT4S2cK85+M7haEViBGcmkMqSqxzYUMJ
5QTKmwp9QJpGEdnAhD/vGSPthy4ZfaLLSDqjHgrSAVLTUp8ol1/n5V0G40EjvRWc
l4FKx8yHDcsOhkk4LcBDJnUCkZW5t+td8eASewtTMmFHDV3hAkEA859JvI881KoM
u5hCYBdIkSTsmjZdz68LTxDF3eB9Wgdsk4FS+OYkujl6+pch1fXIgjycs8V5eb6H
9QS8SHE/+wJBANzbs/IX2TcIPU6T4AKMt9QI/+lA/baLD6aaznxs8uvlVQ1XF6Y2
G91imkM0YgobSvUgleswWvjfJPicoVWeOocCQDTUoiTl2wczKTe1OQ+lfPMc0sJO
b+GN0CIBPSd0ezp+yajjLp8TKXvI40ZcZ9YCSs0TX9gMRt+D1e/oavw6sukCQCEd
5I6hmtWQq7iznbxkI1BtBQqRzzlnArLFtb6ci5Rpr+YvMAGhTW5V0bHQEf9X9lZp
S3GXr5xvwVhyDBMNhdMCQQCgVI/9jnc5WW7M9vMxNjxIJ1/LD0G58A31pj0SM0rA
UlRz95QGWJ93+SA2fzM45NgjbTD7IOZ5O7GfBSEjSok5
-----END RSA PRIVATE KEY-----"""

public_key = """-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDSLffg4ztiN4J7eSWhw3kIrrsM
5IMnkYMAxJgWDaIXwo7mJRpCdYkQtxLPnbE+zFklL1BLS923MFtAz5uLpIkf0LiV
YoaAdGMMVfEfusnW8GcWPMse/0jMZkb9RdSI7othUETJNYadUdBQ2tZoGryHJ6N2
DU7RVvt+6KiysCGbXQIDAQAB
-----END PUBLIC KEY-----"""

msg = "helloworld"

keyPub = RSA.importKey(public_key)
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(msg.encode()) # 公钥加密
print("cipher text->", cipher_text)


keyPriv = RSA.importKey(private_key)
cipher = Cipher_PKCS1_v1_5.new(keyPriv)
decrypt_text = cipher.decrypt(cipher_text, None).decode() # 私钥解密
print("decrypted msg->", decrypt_text)
assert msg == decrypt_text # check that

推荐文章