Created
November 12, 2020 19:11
-
-
Save codewithshehraz/60d2cbad9ef031f0851c6d5927348343 to your computer and use it in GitHub Desktop.
Encrypt in java and Decrypt in Node or vice versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import "./styles.css"; | |
import CryptoJS from "crypto-js"; | |
export default function App() { | |
const [encryptedBase64Key, setEncryptedBase64Key] = React.useState(""); | |
const [encryptedCipherText, setEncryptedCipherText] = React.useState(""); | |
const [decryptedCipherText, setDecryptedCipherText] = React.useState(""); | |
const decrypt = () => { | |
//var encryptedBase64Key = "aXRzaG91bGRiZTE2Y2hhcg=="; | |
var parsedBase64Key = CryptoJS.enc.Base64.parse(encryptedBase64Key); | |
//var encryptedCipherText = "wbsl6Tegfc7mexCHUYPS+dON3BWfgU+tD1nkiHiZ4LyrHlUXlWsfeInWMDaZO39z"; // or encryptedData; | |
var decryptedData = CryptoJS.AES.decrypt( | |
encryptedCipherText, | |
parsedBase64Key, | |
{ | |
mode: CryptoJS.mode.ECB, | |
padding: CryptoJS.pad.Pkcs7 | |
} | |
); | |
// console.log( “DecryptedData = “ + decryptedData ); | |
// this is the decrypted data as a string | |
var decryptedText = decryptedData.toString(CryptoJS.enc.Utf8); | |
setDecryptedCipherText(decryptedText); | |
console.log("DecryptedText = " + decryptedText); | |
}; | |
const input = { | |
color: "red", | |
minHeight: "300px", | |
width: "100%" | |
}; | |
return ( | |
<div className="App"> | |
<h1>Decypher Text</h1> | |
<h2> | |
Please Enter Encrypted Base 64 Key and Cipher Text to Decrypt Text | |
</h2> | |
<input | |
className={input} | |
placeholder="Key" | |
onChange={(event) => { | |
setEncryptedBase64Key(event.target.value); | |
}} | |
value={encryptedBase64Key} | |
type="text" | |
/> | |
<br /> | |
<input | |
className={input} | |
placeholder="Cypher Text" | |
onChange={(event) => { | |
setEncryptedCipherText(event.target.value); | |
}} | |
value={encryptedCipherText} | |
type="text" | |
/> | |
<br /> | |
<input type="button" value="submit" onClick={decrypt} /> | |
<br /> | |
{decryptedCipherText === "" ? ( | |
"" | |
) : ( | |
<span className="result">{decryptedCipherText}</span> | |
)} | |
</div> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.Key; | |
import java.util.Base64; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.SecretKeySpec; | |
public class Main { | |
private static final String ALGO = "AES"; // Default uses ECB PKCS5Padding | |
public static String encrypt(String Data, String secret) throws Exception { | |
Key key = generateKey(secret); | |
Cipher c = Cipher.getInstance(ALGO); | |
c.init(Cipher.ENCRYPT_MODE, key); | |
byte[] encVal = c.doFinal(Data.getBytes()); | |
String encryptedValue = Base64.getEncoder().encodeToString(encVal); | |
return encryptedValue; | |
} | |
public static String decrypt(String strToDecrypt, String secret) { | |
try { | |
Key key = generateKey(secret); | |
Cipher cipher = Cipher.getInstance(ALGO); | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); | |
} catch (Exception e) { | |
System.out.println("Error while decrypting: " + e.toString()); | |
} | |
return null; | |
} | |
private static Key generateKey(String secret) throws Exception { | |
byte[] decoded = Base64.getDecoder().decode(secret.getBytes()); | |
Key key = new SecretKeySpec(decoded, ALGO); | |
return key; | |
} | |
public static String decodeKey(String str) { | |
byte[] decoded = Base64.getDecoder().decode(str.getBytes()); | |
return new String(decoded); | |
} | |
public static String encodeKey(String str) { | |
byte[] encoded = Base64.getEncoder().encode(str.getBytes()); | |
return new String(encoded); | |
} | |
public static void main(String a[]) throws Exception { | |
/* | |
* Secret Key must be in the form of 16 byte like, | |
* | |
* private static final byte[] secretKey = new byte[] { ‘m’, ‘u’, ‘s’, ‘t’, ‘b’, | |
* ‘e’, ‘1’, ‘6’, ‘b’, ‘y’, ‘t’,’e’, ‘s’, ‘k’, ‘e’, ‘y’}; | |
* | |
* below is the direct 16byte string we can use | |
*/ | |
String secretKey = "itshouldbe16char"; | |
String encodedBase64Key = encodeKey(secretKey); | |
System.out.println("EncodedBase64Key = " + encodedBase64Key); // This need to be share between client and server | |
// To check actual key from encoded base 64 secretKey | |
// String toDecodeBase64Key = decodeKey(encodedBase64Key); | |
// System.out.println("toDecodeBase64Key = "+toDecodeBase64Key); | |
String toEncrypt = "MY DATA TO ENCRYPT 111-111-111 [email protected]"; | |
System.out.println("Plain text = " + toEncrypt); | |
// AES Encryption based on above secretKey | |
String encrStr = Main.encrypt(toEncrypt, encodedBase64Key); | |
System.out.println("Cipher Text: Encryption of str = " + encrStr); | |
// AES Decryption based on above secretKey | |
String decrStr = Main.decrypt(encrStr, encodedBase64Key); | |
System.out.println("Decryption of str = " + decrStr); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://repl.it/@ShehrazKhan/Encrypt-in-Java-Decrypt-in-Nodejs
https://codesandbox.io/s/decrypt-aes-in-node-encrypted-in-java-vph6w?file=/src/App.js:0-2039