FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
AES4ObjC-Java-JavaScript/Java/src/AESCipher.java at master · kinrt/AES4ObjC-Java-JavaScript · GitHub
kinrt
AES4ObjC-Java-JavaScript
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
AES4ObjC-Java-JavaScript
/
Java
/
src
/
AESCipher.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
62 lines (48 loc) · 3.03 KB
Breadcrumbs
AES4ObjC-Java-JavaScript
/
Java
/
src
/
AESCipher.java
Copy path
File metadata and controls
executable file
·
62 lines (48 loc) · 3.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
51
52
53
54
55
56
57
58
59
60
61
62
//
// https://github.com/WelkinXie/AESCipher-Java
//
import
java
.
security
.
InvalidAlgorithmParameterException
;
import
java
.
security
.
InvalidKeyException
;
import
java
.
security
.
NoSuchAlgorithmException
;
import
java
.
util
.
Base64
;
import
java
.
util
.
Base64
.
Decoder
;
import
java
.
util
.
Base64
.
Encoder
;
import
javax
.
crypto
.
BadPaddingException
;
import
javax
.
crypto
.
Cipher
;
import
javax
.
crypto
.
IllegalBlockSizeException
;
import
javax
.
crypto
.
NoSuchPaddingException
;
import
javax
.
crypto
.
spec
.
IvParameterSpec
;
import
javax
.
crypto
.
spec
.
SecretKeySpec
;
import
java
.
io
.
UnsupportedEncodingException
;
public
class
AESCipher
{
private
static
final
String
IV_STRING
=
"A-16-Byte-String"
;
private
static
final
String
charset
=
"UTF-8"
;
public
static
String
aesEncryptString
(
String
content
,
String
key
)
throws
InvalidKeyException
,
NoSuchAlgorithmException
,
NoSuchPaddingException
,
InvalidAlgorithmParameterException
,
IllegalBlockSizeException
,
BadPaddingException
,
UnsupportedEncodingException
{
byte
[]
contentBytes
=
content
.
getBytes
(
charset
);
byte
[]
keyBytes
=
key
.
getBytes
(
charset
);
byte
[]
encryptedBytes
=
aesEncryptBytes
(
contentBytes
,
keyBytes
);
Encoder
encoder
=
Base64
.
getEncoder
();
return
encoder
.
encodeToString
(
encryptedBytes
);
}
public
static
String
aesDecryptString
(
String
content
,
String
key
)
throws
InvalidKeyException
,
NoSuchAlgorithmException
,
NoSuchPaddingException
,
InvalidAlgorithmParameterException
,
IllegalBlockSizeException
,
BadPaddingException
,
UnsupportedEncodingException
{
Decoder
decoder
=
Base64
.
getDecoder
();
byte
[]
encryptedBytes
=
decoder
.
decode
(
content
);
byte
[]
keyBytes
=
key
.
getBytes
(
charset
);
byte
[]
decryptedBytes
=
aesDecryptBytes
(
encryptedBytes
,
keyBytes
);
return
new
String
(
decryptedBytes
,
charset
);
}
public
static
byte
[]
aesEncryptBytes
(
byte
[]
contentBytes
,
byte
[]
keyBytes
)
throws
NoSuchAlgorithmException
,
NoSuchPaddingException
,
InvalidKeyException
,
InvalidAlgorithmParameterException
,
IllegalBlockSizeException
,
BadPaddingException
,
UnsupportedEncodingException
{
return
cipherOperation
(
contentBytes
,
keyBytes
,
Cipher
.
ENCRYPT_MODE
);
}
public
static
byte
[]
aesDecryptBytes
(
byte
[]
contentBytes
,
byte
[]
keyBytes
)
throws
NoSuchAlgorithmException
,
NoSuchPaddingException
,
InvalidKeyException
,
InvalidAlgorithmParameterException
,
IllegalBlockSizeException
,
BadPaddingException
,
UnsupportedEncodingException
{
return
cipherOperation
(
contentBytes
,
keyBytes
,
Cipher
.
DECRYPT_MODE
);
}
private
static
byte
[]
cipherOperation
(
byte
[]
contentBytes
,
byte
[]
keyBytes
,
int
mode
)
throws
UnsupportedEncodingException
,
NoSuchAlgorithmException
,
NoSuchPaddingException
,
InvalidKeyException
,
InvalidAlgorithmParameterException
,
IllegalBlockSizeException
,
BadPaddingException
{
SecretKeySpec
secretKey
=
new
SecretKeySpec
(
keyBytes
,
"AES"
);
byte
[]
initParam
=
IV_STRING
.
getBytes
(
charset
);
IvParameterSpec
ivParameterSpec
=
new
IvParameterSpec
(
initParam
);
Cipher
cipher
=
Cipher
.
getInstance
(
"AES/CBC/PKCS5Padding"
);
cipher
.
init
(
mode
,
secretKey
,
ivParameterSpec
);
return
cipher
.
doFinal
(
contentBytes
);
}
}
Back
|
FazBrowse Home
|
New Git URL