AES_DECRYPT Getting the value from a blob in typescript

时间:2018-06-04 17:37:40

标签: mysql typescript blob aes angular5

Coding an Angular 5/typescript client calling a node.js webserver talking to a MySQL DB. Had a successfully working call for names:

SELECT *
FROM stories JOIN users
ON stories.author_id=users.id
WHERE stories.story_id = 1;

If I need to encrypt the last names of authors at a field level in the DB, I could use AES_ENCRYPT and AES_DECRYPT. This changes the call to this. I used dlname here to distinguish the decrypted value from the lname field.

SELECT *, AES_DECRYPT(lname, UNHEX(SHA2('some phrase',512))) AS dlname
FROM stories JOIN users
ON stories.author_id=users.id
WHERE stories.story_id = 1;

This works in dev environment using phpMyAdmin, the dlname is fine on the return and shows as text. But when pushed to a prod env with MySQL and call it, the typescript (Angular 5 client) shows dlname as a blob [object Object]. I checked it with MySQL Workbench and it also shows it as a blob, which I can right-click on and then "Open value in viewer" and I see the last name just fine.

My question is, how do I write the typescript code to get the actual lname value out of the blob? The pertinent lines of code are...

this.http.get<StoryRes>(sRootURI + '/getStory/' + sDocID)
    .subscribe(data => {
      this.sAuthor = data[0].fname + ' ' + data[0].dlname;

In this case, the dlname shows in the console as [object Object].

1 个答案:

答案 0 :(得分:0)

我从BLOB data returned in MySQL using AES_DECRYPT with ORDER clause中的一些帮助中找到了答案。在AES_DECRYPT函数周围添加CAST(... AS CHAR)...

SELECT *, CAST(AES_DECRYPT(lname, UNHEX(SHA2('some phrase',512))) AS CHAR) as dlname...

在客户端,只需确保调用新字段名称dlname而不是实际名称lname。客户端不需要任何其他内容,将按照预期以文本形式发布。